Insights

Running Multiple VMs? Here’s Why You Should Turn On Kernel Samepage Merging (KSM)

Linux & Virtualisation · 2 min read

If you’re a developer running multiple virtual machines on your Linux system, memory can become a serious bottleneck fast. Even powerful machines can struggle when you're juggling different OS environments, databases, and app stacks all at once.

But there’s a built-in Linux feature that can help in the right scenarios: Kernel Samepage Merging (KSM).
It is not a magic fix, but it can reduce wasted memory when several VMs are running similar workloads.

What KSM Actually Does

When you run several VMs, they often end up loading a lot of the same data into RAM. Think identical OS libraries, duplicate processes, or cloned environments.
Without KSM, each VM can keep its own separate copy, which can waste memory.

KSM looks for identical memory pages across your VMs and can merge them into a shared copy.
When something needs to change, Linux can copy the page again using copy-on-write behaviour.

Result: potentially meaningful RAM savings in workloads that share a lot of memory, without requiring constant manual tuning.

Why Developers Should Care

More Virtual Machines, Less Hardware Strain

Instead of watching your system crawl once you spin up your fourth or fifth VM, KSM helps you keep everything running smoother. You can test, deploy, and simulate complex environments without constantly hitting memory limits.

Faster Local Testing

Waiting for cloud VMs to spin up? Burning hours reconfiguring labs?
With KSM, you can run bigger environments right on your laptop or workstation, perfect for DevOps experiments, app validation, or even running a mini Kubernetes cluster locally.

Save on Hardware and Cloud Costs

Pushing your machine harder means fewer trips to the store for RAM upgrades, and fewer bills from cloud providers when local testing is good enough.
Less waste. More value.

Keep the Risks in Perspective

Because KSM uses copy-on-write, it is generally a low-risk memory optimisation in many VM setups.
That said, it is not universally beneficial and it is still worth checking your workload, host memory pressure, and overall performance profile before treating it as a default setting.

Common Myths About KSM

"It’s risky to share memory between VMs."
Not necessarily. Copy-on-write helps keep each VM isolated, but it is still a design choice that depends on the workload and the host environment.

"KSM will make everything slower."
For many systems the overhead is modest, but not zero. The trade-off depends on how hard the host is working and how much memory sharing actually occurs.

"Only enterprise servers need KSM."
Not necessarily. Even a single developer’s workstation can benefit in some VM-heavy setups, but the gain varies depending on the workload.

How to Enable KSM

Create /etc/systemd/system/ksm.service:

[Unit]
Description=Enable Kernel Samepage Merging (KSM)
After=default.target

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c 'chown root:root /sys/kernel/mm/ksm/run && echo 1 > /sys/kernel/mm/ksm/run'
ExecStartPost=/usr/bin/sh -c 'chown root:root /sys/kernel/mm/ksm/sleep_millisecs && echo 100 > /sys/kernel/mm/ksm/sleep_millisecs'
User=root
Group=root
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Then enable and start it:

sudo systemctl daemon-reload
sudo systemctl enable --now ksm.service

Let’s make your day a little bit easier.

Whether your network is down, a workstation needs repair, or you’re auditing an enterprise system, we’re ready to assist.

Contact us