[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Limit memory consumption of an ad hoc process



On Mon, Jul 05, 2021 at 10:57:03PM +0700, Victor Sudakov wrote:
> Greg Wooledge wrote:
> > One way would be:
> > 
> > (ulimit -m 2097152; ./mytest)
> 
> Is the value in KB or MB? Seems KB.

Please read the documentation.

unicorn:~$ ulimit -a | grep -- -m
max memory size             (kbytes, -m) unlimited

> Anyway, the suggested method does not seem to work:
> 
> $ ulimit -m 640 ; stress-ng --vm 1 --vm-bytes 2G
> -bash: ulimit: max memory size: cannot modify limit: Operation not permitted
> stress-ng: info:  [10272] defaulting to a 86400 second (1 day, 0.00 secs) run per stressor
> stress-ng: info:  [10272] dispatching hogs: 1 vm

Have you been omitting the parentheses in your previous testing?

unicorn:~$ (ulimit -m 640; /bin/echo hi)
hi

Setting the resource limit DOWNWARD should always be allowed.  This
particular one is set to infinity by default in Debian, so the only way
setting it to 640 kbytes would be disallowed is if you had previously
set it lower than that.  Which, judging by the lack of parentheses in
your sample call, is a thing you might have done.

You don't want to nuke resources limits in your interactive shell.  That's
what the explicit subshells are for.


On Mon, Jul 05, 2021 at 11:06:31PM +0700, Victor Sudakov wrote:
> Even as root, there is no "Operation not permitted" message, but there
> is no working limit either:
> 
> 
> # ulimit -m 1048576 ; stress-ng --vm 1 --vm-bytes 8G
> stress-ng: info:  [10961] defaulting to a 86400 second (1 day, 0.00 secs) run per stressor
> stress-ng: info:  [10961] dispatching hogs: 1 vm
> 
> stress-ng is not killed. Swap is not enabled on this test host.

Perhaps -m is not the correct resource limit for what you're trying to
achieve.  You've been assuming that it is, but I told you to read
setrlimit(3) to see what each of them is and what it actually controls.

>From ulimit's output:

max memory size             (kbytes, -m) unlimited

>From bash(1):

              -m     The maximum resident set size (many systems do not  honor
                     this limit)

>From setrlimit(3):

       RLIMIT_RSS
              This is a limit (in bytes) on 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.

Perhaps you want "ulimit -v" instead?

And in any case, when you test this stuff, use the subshells.  Or be
prepared to discard and re-spawn a whole lot of terminal windows.


Reply to: