How Limit Memory Usage for a Single Linux Process and Not Kill The Process

Limit memory usage for a single Linux process

There's some problems with ulimit. Here's a useful read on the topic: Limiting time and memory consumption of a program in Linux, which lead to the timeout tool, which lets you cage a process (and its forks) by time or memory consumption.

The timeout tool requires Perl 5+ and the /proc filesystem mounted. After that you copy the tool to e.g. /usr/local/bin like so:

curl https://raw.githubusercontent.com/pshved/timeout/master/timeout | \
sudo tee /usr/local/bin/timeout && sudo chmod 755 /usr/local/bin/timeout

After that, you can 'cage' your process by memory consumption as in your question like so:

timeout -m 500 pdftoppm Sample.pdf

Alternatively you could use -t <seconds> and -x <hertz> to respectively limit the process by time or CPU constraints.

The way this tool works is by checking multiple times per second if the spawned process has not oversubscribed its set boundaries. This means there actually is a small window where a process could potentially be oversubscribing before timeout notices and kills the process.

A more correct approach would hence likely involve cgroups, but that is much more involved to set up, even if you'd use Docker or runC, which among things, offer a more user-friendly abstraction around cgroups.

Is there a way to limit the memory consumed by a process on startup

You can either use ulimit at the start of your script:

ulimit -m 8000000

or if that is unavailable for some reason, fork your real_process into the background and use the remainder of your script to check periodically if it's memory usage has exceeded the 8G, and terminate.

while true; do
# check memory usage of process
# kill process if too high
sleep 60
done

How to determine the process memory limit in Linux?

ulimit is your friend. Java processes are no different than any others. But if you can't even run ulimit -a, it's hard to answer your question.



Related Topics



Leave a reply



Submit