Linux – suspend a process (`stress`) to simulate memory pressure minus the CPU cost

linuxmemorysignals

This is as much a question about suspending processes/SIGSTOP as it is about stress

So I'm using stress to simulate memory pressure on my system.

stress --vm X --vm-bytes YM

This works fine, but I notice that it consumes a lot of CPU.

root@ET0021B703EB23:~# ps -aux | grep stress
root     11800  0.0  0.0   2192   232 pts/4    S+   15:21   0:00 stress --vm 1 --vm-bytes 10M
root     11801 83.5  0.2  12436  2436 pts/4    R+   15:21   0:03 stress --vm 1 --vm-bytes 10M

Now CPU load isn't something I want to be concerned about in this test. I used Ctrl + Z to suspend my stress, and I now see that the CPU consumed has fallen but the memory remains, which is what I wanted.

root@ET0021B703EB23:~# ps -aux | grep stress
root      9080  0.0  0.0   2760   296 pts/2    S+   15:18   0:00 grep stress
root     17030  0.0  0.0   2192   172 pts/2    T    14:51   0:00 stress --vm 1 --vm-bytes 10M
root     17031  2.7  0.4  12436  4860 pts/2    T    14:51   0:44 stress --vm 1 --vm-bytes 10M

As I understand it, keeping a process suspended will keep it in memory. Can I therefore use this method to reliably simulate memory pressure without CPU cost?

My concern is if there is something in linux that will kill, or otherwise remove the memory impact of, a suspended process under memory pressure or something (like Android's lowmemorykiller). Does such a thing exist, or is there any reason why this wouldn't work?

Best Answer

I found this Oracle article about OOM Killer (Out Of Memory Killer) answer a half of your question, specially in 'Configuring the OOM Killer' chapter.
I extract from there two important commands (I think):

  1. Disable OOM Killer root@host:~# sysctl vm.overcommit_memory=2
  2. Exclude a process from OOM Killer root@host:~# echo -17 > /proc/<pid>/oom_adj

Other very interesting answer is 1.4 in this FAQ from stress project page, it says:

1.4 Why is my CPU getting hammered but not my RAM?

This is because stress if faily conservative in its default options. It is pretty easy to render a system temporarly unusable by forcing the virtual memory manager to thrash. So make sure you understand how much memory you have and then pass the appropriate options. On a dual-core Intel system with 3 GB of RAM a reasonable invocation is this:

stress -m 1 --vm-bytes 2G


Right, your question has not been answered yet. Let's look at stress manual ...


-c, --cpu N
    spawn N workers spinning on sqrt()

Maybe the above option could help, try to set it to zero. Oops, It doesn't work!?

After a look at the code I noticed that this option is disabled by default. And I've also noticed that --vm-hang option may be what you want.

The default action of --vm is spinning on malloc()/free(), and it's CPU intensive! --vm-hang makes stress program do a pause for seconds every time it allocates until free().

Try to use the following (consumes ~128MB of RAM):
root@host:~# stress --vm 1 --vm-bytes 128000000 --vm-hang 3600
And do a test in another terminal:
root@host:~# top

Related Question