3.7.5. Optimizing Virtual Machine Memory with Kernel Same-Page Merging

To optimize memory usage by virtual machines, OpenVZ uses a feature of Linux called Kernel Same-Page Merging (KSM). The KSM daemon ksmd periodically scans memory for pages with identical content and merges those into a single page. Said page is marked as copy-on-write (COW), so when its contents are changed by a virtual machine, the kernel creates a new copy for that virtual machine.

KSM enables the host to:

  • avoid swapping due to merging of identical pages;
  • run more virtual machines;
  • overcommit virtual machine memory;
  • speed up RAM and hence certain applications and guest operating systems.

KSM can be managed by means of two services:

  • The ksm service that starts and stops the KSM kernel thread.
  • The ksmtuned service that controls and tunes the ksm using the parameters set in the /etc/ksmtuned.conf file.

You can start the ksm and ksmtuned services by executing the following commands:

service ksm start
service ksmtuned start

To check that the feature works, you can check the number of currently shared memory pages in /sys/kernel/mm/ksm/pages_sharing with virtual machines running. For example:

# cat /sys/kernel/mm/ksm/pages_sharing
3159

To stop the services, run:

service ksm stop
service ksmtuned stop

Note

Notes:

  1. It is not advisable to use the KSM daemon if CPU resources may become a bottleneck.
  2. It is recommended to avoid cross-node memory merging when KSM is in use as this may result in a significant performance drop after a lot of pages are shared. To disable merging pages across NUMA nodes, replace the contents of /sys/kernel/mm/ksm/merge_across_nodes with 0.
  3. Using KSM may affect virtual machine security. For more details, see http://kb.virtuozzo.com/en/126594.