A vserver kernel keeps track many resources used by each guest (context). Some of these relate to memory usage by the guest. You can place limits on these resources to prevent guests from using all the host memory and making the host unusable.
Two resources are particularly important in this regard:
- The Resident Set Size (
rss) is the amount of virtual memory (RAM + swap) that the context is allowed to use;
- The Address Space (
as) is the total amount of memory (pages) mapped in each process in the context.
Both are measured in pages, which are 4 kB each on Intel machines (i386). So a value of 200000 means a limit of 800,000 kB, a little less than 800 MB.
Each resource has a soft and a hard limit. The soft limit has little effect, but the hard limit does:
- If a guest exceeds the
rsshard limit, the kernel will invoke the Out-of-Memory (OOM) killer to kill some process in the guest;
- If a guest exceeds the
ashard limit, memory allocation attempts will return an error, but no process is killed.
The hard limit of the as resource refers to the total number of pages mapped in processes. The purpose of the soft limit is unknown.
Bertl explained the difference between rss and as with the following example. If two processes share 100 MB of memory, then only 100 MB worth of virtual memory pages can be used at most, so the RSS use of the guest increases by 100 MB. However, two processes are using it, so the AS use increases by 200 MB.
This makes me think that limiting AS is less useful than limiting RSS, since it doesn't directly reflect real, limited resources (RAM and swap) on the host, that deprive other virtual machines of those resources. Bertl says that AS limits can be used to give guests a "gentle" warning that they are running out of memory, but I don't know how much more gentle it is, or how to set it accurately.
For example, 100 processes each mapping a 100 GB file would consume a total of 10 GB of address space (AS), but no more than 100 MB of resources on the host. But if you set the AS limit to 10 GB, then it will not stop one process from allocating 4 GB of RAM, which could kill the host or result in that process being killed by the OOM killer.
You can set the hard limit on a particular context, effective immediately, with this command:
/usr/sbin/vlimit -c <xid> --<resource> <value>
For example, if you want to change the rss hard limit for the vserver with <xid> 49000 (which you can determine with /usr/sbin/vserver-stat), and limit it to 50,000 pages (200 MB), you could use this command:
/usr/sbin/vlimit -c 49000 --rss 50000
You can change the soft limit instead by adding the -S parameter.
Changes made with the vlimit command are effective only until the vserver is stopped. To make permanent changes, write the value to /etc/vservers/<name>/rlimits/<resource> for the soft limit, or <resource>.hard for the hard limit. You may need to create the rlimits directory. Changes to these files take effect only when the vserver is started. To make immediate and permanent changes to a running vserver, you need to run vlimit and update the rlimits file.
The safest setting, to prevent any guest from interfering with any other, is to set the total of all RSS hard limits (across all running guests) to be less than the total virtual memory (RAM and swap) on the host. It should be sufficiently less to leave room for processes running on the host, and some disk cache, perhaps 100 MB.
However, this is very conservative, since it assumes the worst case where all guests are using the maximum amount of memory at one time. In practice, you can usually get away with contended resources, i.e. allowing guests to use more than this value.