Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to use cgroup (Control Group) to limit the memory usage of virtualbox, but it does not seem to work.

My machine is:

$ uname -a
Linux fc.bgi 2.6.40-4.fc15.i686 #1 SMP Fri Jul 29 18:54:39 UTC 2011 i686 i686 i386 GNU/Linux

I tried to get it to work like this:

  1. Creating new cgroups under memory hierarchy:

    $ cgcreate -g memory:vbox
    
  2. Setting memory.limit_in_bytes for vbox:

    $ cgset -r memory.limit_in_bytes=512M vbox
    
  3. Grouping vbox running pid to vbox:

    $ cgclassify -g memory:vbox 20015
    

Can someone explain why this is not working?

share|improve this question

migrated from stackoverflow.com Aug 21 '11 at 11:34

This question came from our site for professional and enthusiast programmers.

2 Answers 2

you could limt memory usege with /etc/security/limits.conf in this file you put :

domain type item value

where the domain is the @groupname, type is hard or soft where hard is limt that cannot be exceeded under any circumstances.

item is the item field specifies what type of item is being limited. Examples ​ include core (the size of core files), data (the size of a program’s data area), fsize (the size of files created by the user), nofile (the number of open data files), rss (the resident set size), stack (the stack size), cpu (the CPU time of a single process in minutes), nproc (the number of concurrent processes), maxlogins (the number of simultaneous logins), and priority (the process priority). The data, rss, and stack items all relate to memory consumed by a pro- gram. These and other measures of data capacity are measured in kilobytes.

and value is relate to the item field that you have chose, for example if you have chose cpu in the item field and put 2 in the value then in case the domain (you group of VirtualBox) is take more than 2 of the cpu time it will be terminated.

You can use ulimit too, but it restricted to the bash shell only.

Hope this help you in you goal, although it isn't done with the method you asked.

share|improve this answer

I'm guessing there is something wrong with your /etc/cgconfig.conf file. This setup works for me:

[root@localhost cgroup]# cat /etc/cgconfig.conf
mount {
    memory  = /cgroup/memory;
}

[root@localhost cgroup]# service cgconfig start
Starting cgconfig service:                                 [  OK  ]
[root@localhost cgroup]# ls
memory
[root@localhost cgroup]# ls memory/
cgroup.event_control  memory.limit_in_bytes        memory.memsw.max_usage_in_bytes  memory.soft_limit_in_bytes  memory.use_hierarchy
cgroup.procs          memory.max_usage_in_bytes    memory.memsw.usage_in_bytes      memory.stat                 notify_on_release
memory.failcnt        memory.memsw.failcnt         memory.move_charge_at_immigrate  memory.swappiness           release_agent
memory.force_empty    memory.memsw.limit_in_bytes  memory.oom_control               memory.usage_in_bytes       tasks
[root@localhost cgroup]# cgcreate -g memory:vbox
[root@localhost cgroup]# cgset -r memory.limit_in_bytes=512M vbox
[root@localhost cgroup]# cgclassify -g memory:vbox 11727
[root@localhost cgroup]# cat memory/vbox/tasks 
11727

Though, rather than using the cgreate and cgset commands, I'd advise you to just create a /etc/cgconfig.conf file that would include these settings so that you don't have to repeat the process after every reboot. In your case, the file would look like this:

[root@localhost cgroup]# cat /etc/cgconfig.conf
mount {
    memory  = /cgroup/memory;
}

group vbox {
    memory {
        memory.limit_in_bytes="536870912";
    }
}

Now, every time you start the cgconfig service, you'll have your vbox cgroup ready. All that's needed is to move the virtualbox's PID into the /cgroups/memory/vbox/tasks file using either cgclassify or just simply echo the number in that file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.