Linux Control Groups

Using cgroups you can limit resource usage of processes , for example if you have a web server and you want limit its memory usage to 1GB its very easy to do with cgroups.

This is also a basic building block for containers

First we need to install some tools (in my example im using Ubuntu)

cgroup and cgroup2 are file systems in linux. In the kernel code you can find it in /kernel/cgroup/cgroup.c and from user perspective if you do:

also running a mount command:

 

Simple example:

First we create a new group, we want to limit memory usage:

it will create the following folder : /sys/fs/cgroup/memory/user.slice/myg/

Now we can change permission using:

To limit the memory of the group to 20MB:

Code example of memory usage:

 

if you run it as is you will see the output until the system is out of memory

to control the memory usage we need to move it to the above cgroup:

 

you will see the process killed after the limit is reached

Freezer cgroup

Another example is freezing control group. Any task in that group can be suspended

 

 

Tagged

1 thought on “Linux Control Groups

  1. […] example to understand the concept. to implement a full container you need also to add capabilities, control groups and […]

Leave a Reply

Your email address will not be published. Required fields are marked *