Click to See Complete Forum and Search --> : controlling memory usage


hfawzy
12-07-2003, 01:29 PM
Hello,
Here is what I'm trying to do:

I'm developing a C program whose job is to execute any C++ program.
The C++ program that will be executed have some restrictions to respect:

- execution time mustn't exceed a certain time limit
- memory usage mustn't exceed a certain value.

So my job in the C program I'm developing is to check whether the C++ program being run violate any of the restrictions above.
I managed to check the execution time and kill the process whenever the C++ program exceeds the time limit, however, I can't get to limit data memory usage. I tried using a function called setrlimit but to no avail :(

Here is the code I used:


int data_limit = 1024 * 1024 * 4;
int stack_limit = 1024 * 1024 * 2;

struct rlimit myrlimit;

rl.rlim_cur = data_limit;
rl.rlim_max = data_limit;
setrlimit(RLIMIT_DATA, &myrlimit);

rl.rlim_cur = stack_limit;
rl.rlim_max = stack_limit;
setrlimit(RLIMIT_STACK, &myrlimit);


Can you please help me regarding the second restriction?

I appreciate any help..

Thanks in advance,
Hfawzy.

jim mcnamara
12-08-2003, 07:34 AM
Poorly written (even stuff you pay for) C++ code is notorious for creating memory leaks. Sounds like your problem is more with the C++ code.

RLIMIT_RSS the set size is what you want to control. Almost all memory leaks come from allocating heap memory and then not reclaiming it.

Your call is controlling stack memory. Leave it.
Stack memory is used to preserve functions local variables, not heap memory. If you add another call using RLIMIT_RSS it will force the C++ process to page out memory instead of eating up RAM. But it is not the answer.

In general, resource hogs are hard to live with. If these C++ programs are eating your system, trying to control memory is NOT the answer, because limiting their resources results in what is called thrashing - lots of disk i/o because of paging. This impacts the whole system as well.

The real answer is to fix memory leaks.

Stuka
12-09-2003, 11:02 AM
In most cases, you can't proactively control programs in this manner. A possible solution (if practical) would be to have your C code check stats on the process (through the /proc file system, for example), and kill the process if it violates the limits.