On occasion, we would like to suspend memory-intensive processes on our Ubuntu and OS X servers to temporarily free up some RAM for other jobs. If all we were worried about was CPU usage, a simple Ctrl-Z
would work. However, we need to be able to free up the RAM (by writing it to disk) and then restart the process (disk -> RAM) or in other words "hibernate" a single process. Any clues on how to do this? (Preferably from CLI.) Thanks.

- 829,060

- 1,188
-
7I suppose that when a process is in a suspended state, its used memory is a candidate for been swapped to disk, if needed, and this should be done automatically from the kernel. – enzotib Oct 22 '11 at 09:50
-
1@enzotib - I have no idea if this occurs and, in theory, this sounds good. However, I just did a rough test and in practice it doesn't work. Any new processes spawned while 'RAM eaters' are suspended are extremely sluggish and I don't detect any disk activity that would be expected if RAM were being swapped to disk. – Mike Covington Oct 22 '11 at 14:37
-
1@frozenwithjoy I can confirm that this does occur. The only reasons I can think of that a suspended process wouldn't gradually be swapped out is if it's requested unswappable memory (pretty rare, and (mostly) reserved for root), or if the memory it's mapping is shared with another, active process. – Gilles 'SO- stop being evil' Oct 22 '11 at 16:36
-
1A term that might be help here is "checkpointing" (yes, a ugly verb-form). This is generally a difficult problem because the program may be using some resources that could change state while the program is asleep. Managing that problem used to be one of the distinction between Big Iron and little fiddly computers. – dmckee --- ex-moderator kitten Oct 22 '11 at 19:32
4 Answers
The term you're looking for is "Application checkpointing".
The tools I know that can do that are CryoPID and CryoPID2.
Both tools are for Linux only.
I don't know a similar tool for BSD or OS X.

- 141
There is no general facility for hibernating a single process, only the whole system.
However, if you don't care that the process image won't survive a reboot, there is a built-in facility for saving the process image to disk: swapping. Make sure you have enough swap space, and if there is memory pressure and the process is not active (for example because it is suspended), its memory will be swapped out.
If you know that the process is likely to be inactive for a long time and that you're going to need a fast response time at some point, you can force a lot of RAM to be freed now by allocating a lot of RAM in a short-duration process, e.g.
perl -e '$tmp = "a" x 999999999' # allocate about 1GB
You don't get any control on what gets swapped out, so other processes may get swapped out. On Linux, you can swap a process back in by forcibly accessing the pages it maps. The script in this answer will do that: load to memory all the pages mapped by a process (note that this includes open files; you can traverse regions selectively based on the map information to avoid swapping in data that you know you won't need, see this answer for more information).

- 829,060
-
Are you sure there's no mechanism provided by the kernel for manually swapping out specific processes, even as the superuser? I don't know of any but it seems like a reasonable thing to expect. – Sparkette Feb 11 '21 at 00:44
-
@flarn2006 That would be difficult to implement and that level of control is rarely useful, so no, it's not something you can expect. – Gilles 'SO- stop being evil' Feb 11 '21 at 08:13
-
Ah okay, well hopefully suspending a process will continue working as well to prevent thrashing as it did the first time I tried. – Sparkette Feb 14 '21 at 05:06
Make sure you have plenty of swap space. Make sure your system is set to prefer to swap inactive pages (vm.swappiness=100). Then it should be sufficient to suspend the process. The kernel will prefer to swap the inactive pages out.

- 5,414
On Mac OS X you may try to use the purge
command (provided with Xcode) to free up some (inactive) RAM.
vm_stat
purge
vm_stat

- 829,060

- 1
-
2
purge
wouldn't help here. It removes the disk cache, but that won't cause a process's memory to be swapped out, and it won't make it faster for another process to obtain RAM. – Gilles 'SO- stop being evil' Feb 08 '12 at 14:35