Exists a means to limit the quantity of memory a certain process can make use of in Unix?

I require to examine a process for memory monitoring.

  • I do not have the resource, so I require to do all the screening from the OS side.
  • I intend to have the ability to claim something like limitmemory 400k -p <pid>

Exists a means to do this in unix? Any kind of usual unix energy would certainly be superb.

0
2019-05-04 06:08:46
Source Share
Answers: 5

If you simply intend to examine and also gauge the memory use of your program please consider time. You can gauge the source use of your program in several facets consisting of the regards to CPU Time and also memory use. Adhering to command will certainly offer you the memory use and also CPU time use of myProgram :

/usr/bin/time myProgram
.

(Make certain to offer the outright course to identify it from celebration integrated time command. )

If you simply intend to limit the sources of your process, i advise you to create an examination customer for this details job. Limit the sources of this customer according to your demands and also run the process by the customer. It appears that, in *nix globe, source monitoring based upon users is far more innovative than source monitoring based upon procedures.

You can examine /etc/security/limits.conf to limit the sources of a customer. Or you can make use of ulimit after visited with the to-be-limited customer.

0
2019-05-08 00:45:27
Source

To set the limit when beginning the program, usage ulimit -v 400, as shown by polemon. This establishes the limit for the shell and also all its offspring, so in a manuscript you could intend to make use of something like (ulimit -v 400; myprogram) to limit the extent.

If you require to transform the limit for a running process, there's no energy for that. You need to get the process to execute the setrlimit system call. This can usually be performed with a debugger, although it does not constantly function accurately. Below's just how you could do this with gdb (untried; 9 is the value of RLIMIT_AS on Linux ) :

gdb -n -pid $pid -batch -x /dev/stdin <<EOF
call setrlimit(9, {409600, -1})
detach
quit
EOF
.
0
2019-05-08 00:44:12
Source

I'm not really certain concerning this, yet you can additionally make use of cgroups to limit the memory usage. The benefit of cgroups is that you can regulate procedures that are currently running. Incidentally systemd will certainly make use of cgroups to regulate the system solutions.

However I've tried out a little bit and also they do not appear to function quite possibly on my Fedora 13 system.

0
2019-05-08 00:24:59
Source

There is the setrlimit() function, which permits to set up a process' restrictions in C. Write a C program to call setrlimit after that to exec the command you intend to be restricted. setrlimit can not transform various other procedures' restrictions.

The good news is a person currently created something comparable. It can be downloaded and install from freshmeat. I had a glance at the resource code and also it appears to be great. Usage rlimit at your very own discernment. Keep in mind that rlimit additionally can not transform various other procedures' restrictions.

Edit : Gilles recommended a wonderful hack with gdb : Attach to the process with gdb after that make the process call setrlimit. This would certainly probably address the trouble to limit a currently running process.

0
2019-05-08 00:13:42
Source

ulimit -v, it's a shell builtin, yet it needs to do what you desire.

I make use of that in init manuscripts occasionally :

ulimit -v 128k
command
ulimit -v unlimited
.

It appears nonetheless, that you desire means of adjusting the maximum allocatable memory while the program is running, is that proper? Possibly something like renice for adjusting the Priority.

There is, nonetheless, no such device to my expertise.

0
2019-05-07 23:55:21
Source