Is it possible in Linux/Unix systems, to move a process explicitly to the memory swap? I've investigated swap* tools and kill but they can't do anything I want.

Example: Process FOO is currently running and I want to start another process BAR which won't fit into RAM and will, therefore, be swapped. I'd like to prefer BAR to run faster for now, so I want FOO to be moved to swap space first and then run BAR. After BAR quits, I'd like to unswap FOO.

  • 1
    You can renice(8) programs, but you can't move them to swap explicitly. Managing memory at that level is kernel's job, not yours. – Satō Katsura Sep 9 '17 at 14:29
  • 1
    On some systems, stopping them with kill -STOP may prompt the system to gradually move their pages to swap. Then use kill -CONT on the pid to continue its execution (it will swap back in to RAM pretty promptly then, I suppose). – Kusalananda Sep 9 '17 at 14:34

I'm not aware of any way to request that a specific part of memory be moved to swap on Linux. However, the contrary is possible. You can explicitly preload the files used by BAR to cache simply by running cat /path/to/file >/dev/null.

If BAR is already running and partially swapped out, on Linux, you can arrange to load its pages by reading from /proc/BAR_PID/mem. See How do I read from /proc/$pid/mem under Linux?

Also, if you want to make some memory available for BAR's quick consumption, you can launch a program that will allocate a lot of memory. This is not ideal because the kernel may decide to swap out some other process, or to remove useful files from the disk cache. For best results, do this before swapping in anything you want to explicitly swap in, and suspend FOO (kill -STOP FOO_PID). perl -e '$a = "a" x 1234567' allocates about 2×1234567 bytes of memory plus change.

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.