I remember having many
VPS servers on the same machine and asking myself, "Why does each little VPS take so much RAM?" If you are or have asked that yourself, you will appreciate how
Kernel Same-Page Merging (KSM) can help! It is like a fun little trick to save on memory.
So, What Is KSM?
KSM sounds really technical, but to be honest, it is quite simple too! It scans the memory on your
VPS, and says, "Wait, these pages look the same. Should I merge them?"
How does this help?
Because same memory pages waste space and
KSM combined them into one page! Easy right?
Memory Deduplication
KSM will locate the same memory pages and merge them back together. This is quick and freeing memory!
Key benefits:
- Less consumption of RAM on your server
- More VPS on the same physical resources
- Smoother experience under heavy loads
Have you ever had two
Ubuntu VMs that seem to perform exactly the same? That’s due to them having many similar memory pages.
Better Overall Performance
When the server has saved
RAM, your apps and VMs will run with less remapping of memory into RAM. I have seen
VPS nodes that were previously sluggish become responsive with the enablement of
KSM.
When KSM Works Best
KSM does not solve all problems, but it works fantastically in specific configurations.
When using KSM, users should look to use it at the following:
- When running many VPS servers with the same OS
- When using containers with similar applications
- When creating test VMs with other clones
- When hosting companies with running many small servers
If you clone VMs with any amount of regularity you will already have seen the very similar nature of
VMs, and
KSM uses this to save memory.
Downsides Of KSM
Like any tool or setting,
KSM (Kernel Same-Page Merging) has one downside. KSM does use a bit of CPU when it processes memory. I think I only ever notice it when I am generating a new
kernel. Generally, you’ll realize that the savings in RAM is more significant than the small CPU requirement.