ulimit not limiting memory usage

Solution 1:

Your example should work like you think (program gets killed after consuming too much RAM). I just did a small test on my shell server:

First I restricted my limits to be REALLY low:

ulimit -m 10
ulimit -v 10

That lead to about everything getting killed. ls, date and other small commands will be shot before they even begin.

What Linux distribution you use? Does your program use only a single process or does it spawn tons of child processes? In the latter case ulimit might not always be effective.

Solution 2:

ulimit -m no longer works. Use ulimit -v instead.

The reason is that ulimit calls setrlimit, and man setrlimit says:

RLIMIT_RSS Specifies the limit (in bytes) of the process's resident set (the number of virtual pages resident in RAM). This limit has effect only in Linux 2.4.x, x < 30, and there affects only calls to madvise(2) specifying MADV_WILLNEED.

Solution 3:

This only works in a single bash session unless you put it into your .bash_profile and won't apply for the already running processes.

What I find strange is that the:

 max memory size         (kbytes, -m) unlimited

is not present in /etc/security/limits.conf even tho it's only limits memory consumption per process not overall for 1 user account. Instead of them adding Cgroup, they should have just modify the existing unix commands to accomodate those new features.