Quoting the Deployment Guide from centos.org that is,

Swap space in Linux is used when the amount of physical memory (RAM) is full.

And that Wikipedia also said that,

the term "swap" [is used] to describe both the act of moving memory pages between RAM and disk, and the region of a disk the pages are stored on.

Now, how come that at certain instances, swap space has larger data in it than in my physical memory? I believe that when the system requires more memory than is physically available, the kernel swaps out less used pages and gives memory to the current application (process) that needs the memory immediately. So the swap space must not have data stored larger than RAM.

This is where I'm coming from, please refer to the screenshot provided below.

enter image description here

migrated from stackoverflow.com Nov 1 '12 at 12:31

This question came from our site for professional and enthusiast programmers.

  • 1
    so what is your question? – John3136 Nov 1 '12 at 5:36
  • it is for serverfault or superuser – zb' Nov 1 '12 at 5:37
  • @John3136: it is stated, "how come that at certain instances, swap space has larger data in it than in my physical memory?" – chrismsawi Nov 1 '12 at 5:38
  • 1
    Your discussion of swap space is pretty much correct, so it is fair to assume that if swap has more data than physical, it is because you have allocated more data than you have physical. Without knowing anything about your code we can't tell! – John3136 Nov 1 '12 at 5:40
  • 2
    There's nothing wrong with swap consumption being more than physical memory consumption. When an application, which resides in physical memory, quits, physical memory consumption is reduced, but swap consumption remains the same. – Wu Yongzheng Nov 1 '12 at 6:11

You're misunderstanding virtual memory. Programs and the operating system always operate in virtual memory. If you malloc 10 bytes, or 10 million, or 10 billion, you are allocating virtual memory. Your code, your stack space (automatic storage, alloca, etc.), your global storage (if you have any), also all live in virtual memory. If you take the address of something, as in &myVariable, the result is a virtual address. You are ''never'' using physical memory addresses under a virtual memory OS.

The sum total of virtual address space needed by the OS plus all the programs you have running will routinely exceed the size of RAM, even on today's systems. This can even be true for a single program. So, RAM usage + "swap space" for a single process can easily exceed RAM.

Heck, the whole point of virtual memory - well, one of the important points - is to allow you to write code that needs more "memory" (whether for code, data, whatever) than you have RAM.

As others said in comments, it is up to the operating system to decide what portion of the total virtual address space (for the OS + all processes) is kept in RAM and which is out on disk (in "swap" space). If you reference some page (generally 4 Kbytes on x86/x64 systems) of your virtual address space that isn't in RAM, the OS "swaps it in" for you (we also say "pages it in" or "faults it in", as this is stimulated by a "page fault"). Sometime later the OS may decide it's short on RAM and may remove some of the oldest-referenced or least-often-referenced stuff from RAM. If that stuff was modified since it was last brought into RAM, the modified version has to be saved on disk - that's why stuff gets put in "swap space" (or in Windows terms, the pagefile).

By the way, the pagefile/swapfile/swap space is by far not the only place where virtual memory contents are held if they're not in RAM. Mapped files, which generally include every code file, are another place. Code is rarely modified while in RAM so it generally only swapped in, not out. If it must be lost from RAM it's just dropped and if needed again can be swapped back in from the same file it came from originally.

  • I can't believe nobody else has posted an answer! – Jamie Hanrahan Oct 4 '15 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.