4 ms·
Swap is most useful, when you (almost) never look at the swapped out data again. That might sound stupid, but it occurs often enough in practice: eg code or da
by eru 6y ago
Swap is most useful, when you (almost) never look at the swapped out data again.
That might sound stupid, but it occurs often enough in practice: eg code or data structures that were only used when starting off the program in question.
An optimally coded program wouldn't benefit from this. But real world programs often do.
- mytherin 6y agoI wouldn't link the usefulness of swap to the quality of programs. It has a lot to do with user behavior. If a user opens 100 tabs in a web browser but never looks at most of them, swapping unused ones to disk if they don't fit in RAM anymore is a very useful behavior (considering the alternative is killing the entire program). You could argue an optimally coded program should do this themselves, however, that is not so easy when multiple programs are running concurrently and fighting for RAM. On an 8GB machine, how much RAM should your web browser use? The answer is "it depends". If there are no other programs running, it can use all 8GB. If you are running an IDE, a chat client and a compiler in the background, it should probably be using less. The individual programs don't have enough information to make this judgement call - and making very conservative estimates will lead to much more swapping than actually required. The operating system is in charge of allocating memory for all those programs and can effectively make the judgement of what should be swapped out or not.
- xxs 6y agoIf you hit the swap, there is no disk cache. Which is problematic on its right own.
- eru 6y agoHuh? Swapping out unused stuff leaves more space for the disk cache.
- xxs 6y agodisk cache is just 'free memory', when swap is used there is no free memory. Pretty much any OS (linux, windows, macos) use the unallocated memory for disk cache. Disk swap also takes RAM as page descriptors.
- eru 6y ago> [...] when swap is used there is no free memory. Well, that's not true. In Linux you can have pages swapped out to disk and physical memory used for disk cache at the same time.