2

I have a long-running script that launches several child processes on a Linux machine with 8GB of memory. After a few hours of running, it takes up nearly 90% of memory, and this makes other services, like SSH, unresponsive when it starts disk swapping.

What's the best way to programmatically limit the overall memory usage for my script and all child processes, without setting a specific memory limit for each process individually? Different child processes can use very different amounts of memory, so it would be very inefficient to set fixed thresholds.

Ideally, I'd like to simply specify "only use up to 75% of memory" and let the system divide that among the children as needed, to ensure I can still SSH into the machine at any given time. I first tried setting up a cron job to automatically renice sshd to the highest priority, but that's had no effect, and I'm routinely unable to SSH in, or the SSH prompt is unusably slow.

Cerin
  • 3,497
  • 17
  • 57
  • 72
  • I suppose `ulimit` is not suitable for this for two reasons. Any virtual memory limit specified would apply to each process, so the sum could go much higher. And it is really physical memory, you want to limit. The limited process could be allow to use more virtual memory as long as most of it remains on swap. I have seen Linux systems patched to achieve your desired goal, but I don't know if the patches are published. – kasperd Apr 08 '14 at 15:57
  • @kasperd, Right, I just want to ensure a certain amount of memory is reserved for critical services like SSH. I don't care if my processes need to use swap, just as long as it doesn't cause everything else to become unusable. Unfortunately, the default configuration in Linux seems to do just that. – Cerin Apr 08 '14 at 16:18

1 Answers1

2

Run the processes as a dedicated user and set it up via cgroups:

/etc/cgconfig.conf:

group limitedram {
    memory {
        memory.limit_in_bytes = 6442450944;
    }
}

and /etc/cgrules.conf:

serviceuser   memory   limitedram/

That limits the memory usage of serviceuser to 6 GB.

faker
  • 17,326
  • 2
  • 60
  • 69