4

I am doing performance analysis on a multi-threaded program that uses all the memory that is available in the system. My OS is Ubuntu 18.04. I'm trying to limit the available memory to e.g 32GB even though my server may have 128GB of memory available. Haven't been able to find a reliable solution. Seems like ulimit is not exactly doing what I'm looking for. I can also clog up memory by another process (e.g a controllable process that will consume 64GB of RAM). But even for that purpose I'm not sure how to reliably clog up the memory.

Would appreciate your thoughts.

2
  • Ram disk will not help, but you should check cgroups *(if enabled in your kernel, it should for ubuntu). Description for systemd managed service: paranoids.at/cgroup-ubuntu-18-04-howto, for docker docker run --memory=1G …., for custom process tree with cgcreate/cgset/cgexec: dev.to/vga/… Commented Apr 1, 2020 at 1:30
  • Thanks a lot for the info on cgroups. I'm checking out the links you sent and will try it. That being said, RamDisk seems to be working. Why do you think that's not helping? Commented Apr 2, 2020 at 17:36

2 Answers 2

2

cgroups is the feature or modern linux kernels which allow you to limit resources like memory for group of processes (or for single process with threads). More about cgroups: https://en.wikipedia.org/wiki/Cgroups https://man7.org/linux/man-pages/man7/cgroups.7.html

The cgroups feature should be already enabled in your ubuntu 18.04 kernels. There are some descriptions how to use cgroups to limit memory:

# Create a group for memory named “limited_group_1” cgcreate -g "memory:limited_group_1" -t USERNAME:GROUPNAME # Specify memory limit to 1G for this group cgset -r memory.limit_in_bytes=1G "limited_group_1" # Launch the application in this group cgexec -g "memory:limited_group_1" ./YOUR_APPLICATION # If needed, we can remove the group cgdelete "memory:limited_group_1" 

https://unix.stackexchange.com/questions/44985/limit-memory-usage-for-a-single-linux-process/279175#279175 was also mentioned in https://dev.to/vga/how-to-see-and-limit-memory-consumption-of-an-application-5bfl

PS: Default memory allocators in older glibc versions (malloc, new) has awful behavior for freed regions: they are not returned back without periodic malloc_trim() library calls. You should try to link your application with libjemalloc or libtcmalloc which will replace malloc implementation of glibc with some code better in memory returning.

Sign up to request clarification or add additional context in comments.

4 Comments

So I did use cgroups to limit available memory to the program I'm running. However as I'm checking memory usage, I can see that while the allocated memory is 1G, the program is using 3G+ memory. I understand malloc might be in issue. The version I'm using is 2.27-3. Not sure if that one has the issue you mentioned.
Amir, check jemallor or tcmalloc. How did you check memory usage, was it VIRT RES SHR or something else?
I was using pmap which actually was not the best. I ended up using smem and found that the PSS region for the program was actually within the limit I set. So that worked very well.
Does not work for me: ❯ cgset -r memory.limit_in_bytes=1G "limited_group_1" cgset: cgroup modify error: Cgroup, requested group parameter does not exist ❯ sudo cgset -r memory.limit_in_bytes=1G "limited_group_1" cgset: cgroup modify error: Cgroup, requested group parameter does not exist
-1

ulimit will limit process memory, not system memory.

Add mem=32G to the kernel command line at boot if you want to accurately simulate a smaller machine.

3 Comments

Thanks for that. I was hoping not to reboot the system every time I want to run a new test with a different memory configuration. Surely that's an option that works but is there any other way? I've been reading on /dev/shm. Wondering if there is a solution there.
You could allocate a hugepages segment, that'd totally remove it from the main memory allocator
BTW, just came across RAM Disk. Have you had any experience with that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.