Skip to content

Instantly share code, notes, and snippets.

@mapuo
Last active September 21, 2023 20:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mapuo/17e3b253222172c1659782eb14150c3a to your computer and use it in GitHub Desktop.
Save mapuo/17e3b253222172c1659782eb14150c3a to your computer and use it in GitHub Desktop.
Enable KSM (Kernel Same-Page Merging) on boot in Debian

Enable KSM (Kernel Same-Page Merging) on boot in Debian

I have installed and run Netdata on my Debian based home server and I wanted to enable KSM after Netdata installer suggested it could save some RAM.

To enable the KSM on boot in Debian you I executed these steps:

  1. Install ksmtuned without all the QEMU dependencies:
sudo apt-get install ksmtuned --no-install-recommends
  1. I actually don't need the ksmtuned.service and I did disabled it:
sudo systemctl stop ksmtuned.service
sudo systemctl disable ksmtuned.service
  1. Finally if:
sudo cat /sys/kernel/mm/ksm/run

returns 0 restart the ksm.service:

sudo systemctl restart ksm.service
@sophieforceno
Copy link

sophieforceno commented Jun 20, 2020

How does it start on boot without the service? Anyway, even using this, with the service enabled, the setting doesn't seem to persist across reboots for me, on either of my Ubuntu (should work the same as Debian) devices. Probably unique to my systems, but I've yet to figure out the issue.

@mapuo
Copy link
Author

mapuo commented Jun 24, 2020

The service that does the work is ksm.service and it should be enabled by default.
If it isn't you can do enable it by running: sudo systemctl enable --now ksm.service.

@sophieforceno
Copy link

Thanks. I don't have that service for some reason, despite following your steps and everything appeared to proceed without an issue (though I am running Arch, not Debian). I found that not having the ksm service is not a problem. In case anyone else runs into this and wants a different solution: you can use systemd's temporary files system to enable ksm at boot time (you will need to reboot or echo 1 > /sys/kernel/mm/ksm/run to enable it, though).

Create /etc/tmpfiles.d/ksm.conf with the contents:

w /sys/kernel/mm/ksm/run - - - - 1

Also, you can verify KSM is working by checking /sys/kernel/mm/ksm/pages_shared. If the output of that is a non-zero number, you are good to go.

@AGenchev
Copy link

AGenchev commented Dec 8, 2021

Thanks I just learned about tmpfiles.d.
A network engineer managed to patch the kernel with UKSM. It seems like piece of code which performs better than KSM.

@kawaii-ghost
Copy link

Thanks. I don't have that service for some reason, despite following your steps and everything appeared to proceed without an issue (though I am running Arch, not Debian). I found that not having the ksm service is not a problem. In case anyone else runs into this and wants a different solution: you can use systemd's temporary files system to enable ksm at boot time (you will need to reboot or echo 1 > /sys/kernel/mm/ksm/run to enable it, though).

Create /etc/tmpfiles.d/ksm.conf with the contents:

w /sys/kernel/mm/ksm/run - - - - 1

Also, you can verify KSM is working by checking /sys/kernel/mm/ksm/pages_shared. If the output of that is a non-zero number, you are good to go.

My output is zero, but i've enabled it. What could be wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment