Force process to run in swap completely

13,315

Solution 1

No process can execute in swap "completely". The code+data must be loaded into RAM for the CPU to read/write/execute it. You can push things temporarily out to swap, but if your resident set is too small, you system will swap continuously. (Even with a SSD, loading from disk is thousands of time slower than loading from RAM.)

This constant swapping will:

1) slow down your entire system, as some of the I/O bandwidth of your disk is constantly being used

2) reduce the life of your SSD, as SSDs have a finite number of write cycles before they go bad.

Your best bet is just to create a large swap area, and run your program as normal. The OS will move the infrequently used bits out to disk. If you can't get good performance on the rest of the system, then you need more RAM. You can try to use ulimit and/or cgroups to limit the amount of RAM a process uses, but this cannot "fix" your problem.

Alternately, you could re-write the server in a more efficient language. (This is a common trade-off: The programmer could spend months optimizing the runtime CPU/memory footprint, or the business could spend more money on hardware.)

Solution 2

cgroups may work for you. You can apply memory limit and once the process hits that limit, it will begin to swap.

There is a small example and explanation here.

Share:
13,315
Mikopet
Author by

Mikopet

I love to create things. I want to improve my knowledge everyday.

Updated on July 22, 2022

Comments

  • Mikopet
    Mikopet almost 2 years

    I didn't find the solution for that, so I'm not sure, is there any.

    I have a VPS, with 512MB RAM, and with SSD disks.

    My problem is: I want to run a java based server program, and I want it to take in swap completely. The -Xms for it now 384, but it's not enough.

    Is there any solution?

    p.s.: swappiness is 60. I dont want to see this process in RAM :-)

    • Ansgar Wiechers
      Ansgar Wiechers almost 11 years
      You don't want any process in swap. Swap is a fallback when for some reason not all processes can't be kept in RAM, but it seriously affects performance.
    • Adrian Panasiuk
      Adrian Panasiuk almost 11 years
      At least one processor instruction needs to be in RAM (but then you need to handle jumps to trap landing at the destination), in practice at least one virtual memory page.
  • JojOatXGME
    JojOatXGME about 6 years
    It would be convenient if you would add a small example to your answer. I build this small script for myself.
  • BjornW
    BjornW almost 6 years
    This is a better answer as it actually answers the question.