$linuxjunkies
>

KSM

also: Kernel Samepage Merging

KSM (Kernel Samepage Merging) is a Linux kernel feature that reduces memory usage by identifying and merging identical memory pages across different processes into a single shared page.

KSM scans the memory pages used by processes and finds pages with identical content. When matches are found, KSM merges them into a single page that multiple processes reference, saving RAM. This is particularly valuable in virtualized environments where many VMs may run similar operating systems or applications.

KSM runs as a kernel daemon and can be controlled via the /sys/kernel/mm/ksm/ sysfs interface. You can enable it by writing to /sys/kernel/mm/ksm/run and tune its behavior with parameters like sleep_millisecs and pages_to_scan.

For example, if three virtual machines each have a 4MB kernel binary with identical content, KSM can merge them into one 4MB shared page referenced by all three, reducing total memory consumption from 12MB to roughly 4MB plus minimal overhead.

The trade-off is increased CPU overhead for page scanning, but the memory savings often justify this cost in dense virtualization scenarios.

Related terms