0

I am trying out a simple example to understand how to limit the memory consumption of processes.

I have created a test memory group, set the limits for both memory.limit_in_bytes and memory.memsw.limit_in_bytes to 5 MB.

I have a really simple program that allocates memory in increments of 1 MB (by reading from a random device).

I would have expected my script to reach somewhere close to 5 MB before it gets killed, but it gets killed right after it reads 1 MB.

When I raise the cgroup limits to 8 MB, it gets killed after consuming 3 MB.

I have tried with different limits, and have noticed it consistently gets killed before reaching half of the allocated limit.

Can someone please let me know if I am missing anything else?

Would greatly appreciate some guidance.

David
  • 2,101
  • 13
  • 16
  • 25
  • Note that you don't have to read from a device to allocate memory. It is sufficient to use malloc and touch a byte in each page. – user10489 Sep 22 '21 at 04:50

1 Answers1

1

cgroups memory limits also cover executable code for the program, including the executable itself and any shared libraries it needs. You can use tools like ps and top (or one of the top variants) to watch actual memory use before you set limits with cgroups, and possibly before you allow your test program to start allocating memory.

user10489
  • 4,051
  • Thank you. I suspected the libraries the program uses, and as you suggested, that seems to be the case. I also used pmap to verify the actual memory consumed, and that validates your answer here. – user1689430 Sep 22 '21 at 05:38