Density and resource utilisation are critical metrics when running high-density virtualization hosts, kernel-based virtual machines (KVM), or containerised microservices on Ubuntu Server. Multiple guest instances running identical operating system distributions or similar application runtimes frequently load redundant copies of shared libraries, binary executables, and static assets into physical host RAM.
The Linux kernel features a memory deduplication technology known as Kernel Samepage Merging (KSM). Operating as a background system daemon, KSM scans anonymous memory regions for identical content, consolidates redundant pages into a single copy-on-write (COW) memory frame, and frees duplicate physical memory back to the host system. Configuring KSM effectively allows administrators to overcommit physical RAM safely on Ubuntu servers hosting virtualised workloads.
How Kernel Samepage Merging Works
KSM was originally developed for QEMU/KVM hypervisors. When guest virtual machines boot, thousands of memory pages contain identical kernel instructions, shared objects, and base configurations.
Under normal circumstances, each virtual machine maintains separate physical pages for these identical datasets. With KSM enabled:
- The
ksmdkernel daemon scans registered virtual memory areas at scheduled intervals. - When it identifies two or more identical memory pages, it adjusts page table pointers to reference a single shared physical page marked read-only.
- The redundant memory pages are released back to the host operating system’s free memory pool.
- If a guest attempts to write to the shared page, a copy-on-write fault is triggered, and the kernel instantly allocates a private copy for that specific process without disrupting operations.
Checking KSM Support on Ubuntu Server
KSM is compiled into standard Ubuntu Server kernels by default. You can confirm that your system kernel includes KSM support by inspecting the /sys/kernel/mm/ksm/ virtual directory:
ls -l /sys/kernel/mm/ksm/
You should see control files including run, pages_to_scan, sleep_millisecs, and telemetry metrics such as pages_sharing.
Enabling and Tuning KSM Parameters
To enable the KSM scanning daemon directly via the sysfs virtual filesystem:
# Enable KSM (1 = run, 0 = stop, 2 = unmerge all pages)
sudo sh -c 'echo 1 > /sys/kernel/mm/ksm/run'
You can fine-tune how aggressively the kernel scans memory by adjusting scanning speed and sleep intervals:
# Number of pages to scan before sleeping (default: 100)
sudo sh -c 'echo 200 > /sys/kernel/mm/ksm/pages_to_scan'
# Milliseconds ksmd sleeps between scanning cycles (default: 20)
sudo sh -c 'echo 10 > /sys/kernel/mm/ksm/sleep_millisecs'
Installing and Configuring ksmtuned
Manually tuning sysfs parameters can lead to excessive CPU overhead if scanning occurs when host memory is plentiful. Ubuntu provides the ksmtuned daemon, which monitors memory pressure dynamically and starts KSM only when available memory drops below a configurable threshold.
To install and start the automated tuning service:
sudo apt update
sudo apt install ksm-control-daemon
sudo systemctl enable --now ksmtuned
You can adjust dynamic scaling thresholds inside /etc/ksmtuned.conf:
# Threshold percentage of total RAM below which KSM activates
KSM_THRES_COEF=20
# Sleep interval between memory checks
KSM_SLEEP_MSEC=50
Monitoring Memory Savings
To verify how much physical RAM KSM has reclaimed on your server, inspect the runtime telemetry files in sysfs:
# Number of shared pages actively merged
cat /sys/kernel/mm/ksm/pages_shared
# Number of duplicate pages referencing shared pages (actual memory saved)
cat /sys/kernel/mm/ksm/pages_sharing
# Calculate total megabytes saved
echo "$(( $(cat /sys/kernel/mm/ksm/pages_sharing) * 4096 / 1024 / 1024 )) MB saved"
By implementing KSM and dynamic tuning with ksmtuned, Ubuntu system administrators can maximise virtualization density while maintaining optimal performance balance.