Understanding and Applying Linux Kernel Samepage Merging (KSM) for Memory Optimization
This article explains the principle, implementation, configuration, and practical performance impact of Linux Kernel Samepage Merging (KSM), showing how it reduces memory usage in containerized game services while outlining integration costs, risks, and monitoring guidelines.
Background – Modern game services often run dozens of processes that each load large data sets, causing memory usage to exceed 900 MiB per process and making memory the primary bottleneck on servers.
KSM Principle – Kernel Samepage Merging (KSM), introduced in Linux kernel 2.6.32, periodically scans physical pages, merges identical pages, and marks them copy‑on‑write (COW) to save memory.
Linux Memory Mechanism – Linux uses a paged virtual memory system where each process has a struct mm_struct describing its address space, divided into struct vm_area_struct (VMA) regions, tracked by struct task_struct.
KSM Implementation – A kernel thread ksmd runs ksm_do_scan on pages marked MMF_VM_MERGEABLE. It merges pages via cmp_and_merge_page. Scan frequency and page count are controlled by /sys/kernel/mm/ksm/pages_to_scan and /sys/kernel/mm/ksm/sleep_millisecs. Users can mark memory regions mergeable with madvise(addr, length, MADV_MERGEABLE).
Integration Cost – Applications must modify code to mark mergeable data and accept the overhead of a resident ksmd thread that consumes a small amount of CPU.
Benefits – For workloads with many identical pages (e.g., dozens of game processes), KSM can reduce memory consumption by roughly 15 GiB (≈23 %). This translates into cost savings for public‑cloud and container deployments.
Risks – Excessive VMA allocation can hit /proc/sys/vm/max_map_count, triggering the OOM Killer. Raising max_map_count without monitoring may degrade performance.
Configuration
echo 1 > /sys/kernel/mm/ksm/run # enable KSM
echo 0 > /sys/kernel/mm/ksm/run # disable KSM
echo 2 > /sys/kernel/mm/ksm/run # stop and unmerge all pagesOther tunables:
/sys/kernel/mm/ksm/sleep_millisecs # interval between scans (default 20 ms)
/sys/kernel/mm/ksm/pages_to_scan # pages processed per scan (default 100)
/sys/kernel/mm/ksm/merge_across_nodes # allow cross‑NUMA merging (0 = disallow)Monitoring parameters include /sys/kernel/mm/ksm/full_scans, /sys/kernel/mm/ksm/pages_shared, /sys/kernel/mm/ksm/pages_sharing, etc.
Application Examples
Idle test on four hosts with eight containers (32 processes each) showed memory usage dropping from ~35 GiB to ~20 GiB per host after enabling KSM.
Runtime test with high‑load containers (9600 vs 800 users) demonstrated a similar ~15 GiB reduction (≈23 %).
Conclusion – Enabling KSM is advisable for services with large, duplicated memory footprints, provided the integration effort is justified and appropriate monitoring is in place.
Acknowledgements – Thanks to Kuso for contributions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
NetEase Game Operations Platform
The NetEase Game Automated Operations Platform delivers stable services for thousands of NetEase titles, focusing on efficient ops workflows, intelligent monitoring, and virtualization.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
