4

From what I understand, the swap space can be used to swap out inactive processes to free up memory. But can a single process take advantage of swap space? For example, if my system has little memory, but a large harddrive with large swap space, can a process requiring large amounts of memory still run (albeit slowly) by taking advantage of the large disk swap space? Is any action required on my part or will Linux automatically take care of this?

4

You don't have to do anything special, apart from providing swap space and mounting it.

For processors with a MMU (memory management unit, i.e. most processors since the early 90's) the full address space can be used, although there doesn't have to be real memory (RAM) at all address range locations. If some memory location is addressed, RAM will be "attached" using a lookup mechanism. If no free RAM is available, some block is freed up by writing its contents to the swap space.

Of course the processor keeps track of what memory address range was written out, so that when some address in that range is once more needed the right block is "swapped in" (most often after freeing up some other RAM by writing its contents out).

The actual detail for each operating system differ, and the article in wikipedia on paging is a good start if you want more information.

Be forewarned with what you try, competing swapping applications easily makes your system slower by a few orders of magnitude, making it look like your machine is dead.

  • Anthon, thanks for explaining this in detail. Just to confirm, this means, in theory, I can call malloc(size) with size being almost as large as my swap+memory combined (minus system overhead of course), right? Let's say I only need to run only one program on it, and the run time is not an immediate concern. I am concerned about getting the program to compile and run - I have large hard drives that I plan to connect (and make as swap using mkswap). – Tosh May 9 '15 at 4:33
  • What I'm hoping to do is to be able to call malloc with larger sizes, taking advantage of space on large internal and external harddrives. – Tosh May 9 '15 at 4:37
  • 4
    Yes that is correct. The other thing you might want to look at is memory mapped files. Not as convenient as swap, but if you allocated everything in one go and manage the so malloced space afterwards yourself, you might be able to use mmap() and manage your data in that. – Anthon May 9 '15 at 4:41

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.