Setting memory limits

Please note that this is the OLD way to set memory limits. The new way uses the kernel's "cgroups". But neither the debian Lenny nor Squeeze kernels have the cgroup memory limits compiled in. So we use the older method, which still works.

 

Setting memory limits manually

Memory limits are stored in the file "/etc/vserver/NAME/rlimits/rss". This contains the number of 4k pages that should be assigned to this vserver. So if you want to place a limit of 256 MB on vserver then you would calculate "256 (MB) * 1024 (KB per MB) / 4 (4k pages)". since 1024/4 = 256, you multiple the number of megabytes you want to assign by 256 to get the number to put into the file. So for 256 MB you put "65536" in the "rss" file. (256 * 256 = 65536). This file is used at boot to set the limit, so if you reboot the vserver, this new limit will be used. However, you can also change memory limits on the fly using the "vlimit" tool. You need to run the command twice with slightly different syntax: vlimit --xid VSERVER_NAME --rss LIMIT vlimit --xid VSERVER_NAME -S --rss LIMIT The first command (without the "-S") creates a "Hard" limit. The "-S" command creates a "Soft" limit. The difference between the hard and soft limit is assigned as "swap" to the vserver. So if you assign a soft limit of 256M and a hard limit of 512M, then in the vserver there will be 256M of RAM and 256M of swap space. In reality, however, the host controls memory usage, so the "swap" on the vserver may well be real RAM and the "RAM" on the vserver could be swap on the host if it ran down. So in practice, it's simplest to keep those two numbers the same. So you would assign 512 as the soft and hard limit. Then in the vserver they would see 512 MB of RAM and no swap. You can see an existing memory limit by running "free -m" inside the vserver. The "total memory" is actually the imposed limit. You can also run the vlimit tool as vlimit --xid NAME -a -d to see all the limits. The "RSS" line is the memory limit and it's the only one we set at the present time. If you wish to remove the memory limit, then first delete the "rlimits" directory. Then either reboot the vserver to pick up this new value (or lack thereof in this case) or use the vlimit command with the keyword "inf" (for infinite). "inf" means no limit except the physical limit of the host. So "vlimit --xid VSERVER_NAME --rss inf" and "vlimit --xid VSERVER_NAME -S --rss inf".

 

Setting memory limits with "vs-mem.pl"

The "easy" way to view or change memory limits is to use the vs-mem.pl script. The syntax is "vs-mem.pl VSERVER_NAME" to view the memory limits and "vs-mem.pl VSERVER_NAME LIMIT" to set a memory limit of LIMIT Megabytes. (The limit is always shown and set in megabytes.) Here's viewing the limit on the vserver "lennytest".vs-mem.pl lennytest "lennytest" memory limit:    256 Megabytes. "lennytest" memory  used:    206 Megabytes. "lennytest" memory  free:    49 Megabytes. The vserver "lennytest" has a 256 meg limit on RAM. Let's raise it to 512.vs-mem.pl lennytest 512 "lennytest" memory limit:    512 Megabytes. "lennytest" memory  used:    206 Megabytes. "lennytest" memory  free:    305 Megabytes. The program shows you the new memory stats after you change the limit. If a vserver is not running, this script will still change the limit, but, of course, it won't matter to until the vserver is started. If a vserver doesn't have a limit assigned, then the "memory limit" line will say "NOT ASSIGNED". You should definitely assign one! You can remove a memory limit (counter-intuitively) by using "vs-mem.pl VSERVER_NAME 0". Here we'll remove the memory limit from the vserver "lennytest".vs-mem.pl lennytest 0 "lennytest" memory limit:    NOT ASSIGNED. "lennytest" memory used:     206 Megabytes. vs-mem.pl is a GPL'ed perl script. If you find any problems or make any improvements, please let us know so we can incorporate them.