How can you limit RAM consumption in a process?

Go To


How can you limit the physical memory consumption of a C program from within the source code on a linux 2.6.32 machine?

I need to determine the type of page replacement algorithm the system is using.

The problem is that without limiting the number of pages a process can have in memory, it becomes difficult to analyze the pattern of page faults to determine the page replacement algorithm.

Also, I don't have root access on the machine.

2012-04-04 18:48
by ktbiz
You may need to use system quotas to limit the amount of RAM a process may use - greg 2012-04-04 18:54


setrlimit(RLIMIT_MEMLOCK, ...).

2012-04-04 18:55
by Jerry Coffin
Could you explain more? I thought MEMLOCK only affected how many pages you could forcibly stop from being swapped out? I want pages to be swapped out, because I need to analyze the swap pattern to determine what algorithm is being used - ktbiz 2012-04-04 19:03
From which include this function comes from? man setrlmit returns nothing - karlphillip 2012-04-04 19:19
ktbiz 2012-04-04 19:21
@karl: The function name is misspelled in this answer, which likely explains why man can't find it. Try setrlimit with two 'i' s - Ben Voigt 2012-04-04 19:50
@BenVoigt: oops -- thank you. Fixed - Jerry Coffin 2012-04-04 19:52
@ktbiz: It affects how many can be locked in to memory. If you just want to limit the size of the data segment, you'd use RLIMITDATA instead. If you want to limit its total virtual memory usage, that would be RLIMITAS - Jerry Coffin 2012-04-04 19:54