AnswerBun.com

How to limit memory usage by application in linux?

Server Fault Asked by edomaur on August 7, 2020

I have a closed source software with some memory leaks problems. Is there a tool or solution to “sandboxe” processes in a fixed amount of memory without using “ulimit” (to generic, I need a per-application memory control)

4 Answers

On systemd-based distros you can also use systemd-run (which indirectly uses cgroups). For example:

systemd-run --scope -p MemoryLimit=1G firefox

Note: this gonna ask you for a password but the app gets launched as your user. Do not allow this to mislead you into thinking that the command needs to run with sudo, because that would cause the command to run under root, which hardly was your intention.

If you want to not enter the password (indeed, why would you need a password to limit memory you already own), you could use --user option, however for this to work you gonna need cgroupsv2 support enabled, which right now requires to boot with systemd.unified_cgroup_hierarchy kernel parameter.

Correct answer by Hi-Angel on August 7, 2020

Use cgroups. https://www.kernel.org/doc/Documentation/cgroups/

Note the difference between memory.limit_in_bytes and memory.memsw.limit_in_bytes. Also note memory.soft_limit_in_bytes. https://www.kernel.org/doc/Documentation/cgroups/memory.txt

Answered by Ken Sharp on August 7, 2020

'ulimit' is a 'per-application' control… per process in fact. The ulimit shell command is a shell built-in setting the limit for the shell process and its children. Put the 'ulimit' command in the script starting your application and the limit will be set for this application only.

Answered by Jacek Konieczny on August 7, 2020

You could use a process management daemon like monit to monitor the amount of memory in use by your proceses and restart it when it grows over your defined limit.

This does sound drastic, but given your application is known to leak, restarting it regularly based on its usage is just putting off the inevitable, when the process size grows larger than the smaller of, your machines physical memory, or any address space limitations imposed by your operating system.

Answered by Dave Cheney on August 7, 2020

Add your own answers!

Related Questions

Microsoft says 2TB limit is the for azure VM OS Disk, What exactly it means?

2  Asked on November 20, 2021 by dinesh-pathapati

 

Cant login to aws ec2 instance

1  Asked on November 20, 2021 by akash-gajbhiye

     

Assign multiple home directories to same user during useradd

1  Asked on November 20, 2021 by nikster2014

   

flask app served by nginx uwsgi returns 502

1  Asked on November 20, 2021 by posop

     

No messages transferred to Dominoserver?

2  Asked on November 18, 2021 by thomas-adrian

       

PHP7-FPM Docker CMD results in 502 Nginx Error

3  Asked on November 18, 2021 by jumpa

         

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir