Playing around with KVM virtualization on e.g. Debian Squeeze and ever wondered if there’s a way to optimize memory usage? It’s possible!
KSM (Kernel Samepage Merging) is the magic phrase. To make it short: On a recent installation (> 2.6.32) you possibly only need to:
# echo 1 > /sys/kernel/mm/ksm/run
If KSM is running, and there are pages to be merged (i.e. more than one similar VM is running), then /sys/kernel/mm/ksm/pages_shared should be non-zero. From the kernel documentation in Documentation/vm/ksm.txt:
The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/:
pages_shared - how many shared unswappable kernel pages KSM is using
pages_sharing - how many more sites are sharing them i.e. how much saved
pages_unshared - how many pages unique but repeatedly checked for merging
pages_volatile - how many pages changing too fast to be placed in a tree
full_scans - how many times all mergeable areas have been scanned
A high ratio of pages_sharing to pages_shared indicates good sharing, but
a high ratio of pages_unshared to pages_sharing indicates wasted effort.
pages_volatile embraces several different kinds of activity, but a high
proportion there would also indicate poor use of madvise MADV_MERGEABLE.