-
Notifications
You must be signed in to change notification settings - Fork 2
Memory Swapping
When benchmarking processes, looking how a process perform when swapping memory can be useful. Since the operating system determines the memory allocation policy, testing with limited memory can be cumbersome. One could try to fill the virtual memory with other processes to limit the available memory but since the operating system tries to swap inactive processes, our benchmarked process will probably remain in virtual memory. In any case, we cannot know this for certain.
Cgroups (control groups) let us control limit and control the resource usage of a group of processes in Linux.
To enable cgroups the cgroup-bin package has to be installed.
sudo apt-get install cgroup-binSome distributions (Ubuntu e.g.) has the memsw property of cgroups not enabled by default. To enable this we need to change the grub configuration. Alter the grub file in /etc/default/grub so the GRUB_CMDLINE_LINUX_DEFAULT is set to GRUB_CMDLINE_LINUX_DEFAULT="cgroup_enable=memory swapaccount=1". Make sure to run sudo update-grub and reboot the machine.
If necessary, you can increase the size of the swap file. To check the current size of the swap file grep SwapTotal /proc/meminfo.
- Turn off swapping
sudo swapoff -a - Resize the swap file
sudo dd if=/dev/zero of=/swapfile bs=1G count=8. This example creates a file of 8GB. - Mark file as swapfile
sudo mkswap /swapfile - Activate the swapfile
sudo swapon /swapfile - Enable swapping
sudo swapon -a
To constrain the process in its resources we create a new cgroup and set the memory limits for both the virtual memory and the swap memory.
# Create new cgroup
sudo cgcreate -g memory:force-swap
# Set virtual memory limit to 2 GB
sudo cgset -r memory.limit_in_bytes=2G force-swap
# Set allowed swapped memory to 8GB
sudo cgset -r memory.memsw.limit_in_bytes=8G force-swap
# Show the configuration
sudo cgget -g memory:force-swap | grep bytes
# Run procress within cgroup
sudo cgexec -g memory:force-swap <program> <program arguments>
# Delete group if needed
sudo cgdelete -g memory:force-swapThe system monitor can be used the check if the cgroup is working correctly and actually is forcing the program to use swap memory.
When a program tries to allocate more memory than is available in the swap file, the OOM killer (if enabled) will still send a SIGKILL and ends the program.