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
Linux/Glibc. How to Use Fprintf in Signal Handler
How to Join 2 CSV Files with a Shell Script
Get The Name of The Directory Where a Script Is Executed
Graphics Card Memory Usage in Linux
Code Behind Rstudio Server Export Function
Get Canonical Path from Pathname
Is There Any Reference Implementation of Linux File System Filter Driver
Sysfs Interface. I Can't Export Gpio Pins in a Xilinx's Board (Zybo and Other)
Merge Multiple Lines to 1 Row with Awk(Or Familiar)
How to Use Sed to Replace a String in a File with a Shell Variable
Can 'Vim' Open a Large File in Read Only Mode as Fast as 'Less'
Open Website from Within Eclipse's Internal Browser
How to Get Started with Libsandbox
How to Do Simple Arithmetic in Sed Addresses
Symbol Lookup Error Undefined Symbol, But All Symbols Seem to Be Present