Limit memory on the Centos server

danieltveriya

New Member
Joined
Oct 31, 2022
Messages
3
Reaction score
0
Credits
24
Hi all,
i need to limit memory to services expect a few specific services.
This limitation need to be to all users.
I not founding any solution for that.
Thanks for the help.
 


There are a few ways to do this. Sometimes you can set this in the application itself.
For example most Java apps have something like a jvm.options config file that lets you set the heap size.
The next most popular way to do this, is with containers, such as Podman or Docker.

You can also do this with ulimit.

su - someuser
ulimit -Sv 1000000


You can also set filesize limits.
ulimit -Sf 2000

The downside to doing it this way, is that every user can change their settings if they want to, (and know how).

So the method I personally use most of the time is systemd.

# This sets max memory for this app to 1G
systemd-run --scope -p MemoryLimit=1000M ./someProgram.bin
# This sets max CPU usage for this app to 10%
systemd-run --scope -p CPUQuota=10% ./anotherProgram.sh
# This does both on a same line.
systemd-run --scope -p MemoryLimit=1000M -p CPUQuota=10% ./myCoolProgram

You can do this in your systemd service file. Of course you have to be running a new enough version of Linux to support systemd. That way "normal" users can't make changes to your service startup file.

If you want to limit CPU resources, you could also look at "nice"

 
There are a few ways to do this. Sometimes you can set this in the application itself.
For example most Java apps have something like a jvm.options config file that lets you set the heap size.
The next most popular way to do this, is with containers, such as Podman or Docker.

You can also do this with ulimit.

su - someuser
ulimit -Sv 1000000


You can also set filesize limits.
ulimit -Sf 2000

The downside to doing it this way, is that every user can change their settings if they want to, (and know how).

So the method I personally use most of the time is systemd.

# This sets max memory for this app to 1G
systemd-run --scope -p MemoryLimit=1000M ./someProgram.bin
# This sets max CPU usage for this app to 10%
systemd-run --scope -p CPUQuota=10% ./anotherProgram.sh
# This does both on a same line.
systemd-run --scope -p MemoryLimit=1000M -p CPUQuota=10% ./myCoolProgram

You can do this in your systemd service file. Of course you have to be running a new enough version of Linux to support systemd. That way "normal" users can't make changes to your service startup file.

If you want to limit CPU resources, you could also look at "nice"

thanks for your replay.
Maybe I will describe better my scenario.
I have a lot of users sending some jobs to cenots vm`s. I need to kill this jobs are over then 4GB ram but allow all other process with out any impact on the System process.
I need this on Machine level and not for per user.
 

Staff online

Members online


Top