Kernel Samepage Merging
KSM is a memory-saving de-duplication feature, that merges anonymous (private) pages (not pagecache ones). Although it started this way, KSM is currently not suitable only for Virtual Machine use, but rather, it can be useful to any application which generates many instances of the same data
KSM only operates on those areas of address space which an application has advised to be likely candidates for merging, by using the madvise(2) system call: int madvise(addr, length, MADV_MERGEABLE).
The app may call int madvise(addr, length, MADV_UNMERGEABLE) to cancel that advice and restore unshared pages: whereupon KSM unmerges whatever it merged in that range. Note: this unmerging call may suddenly require more memory than is available - possibly failing with EAGAIN, but more probably arousing the Out-Of-Memory killer.
Merged in linux 2.6.32. One needs to enable it via CONFIG_KSM=y
You can verify KSM is in action, by checking for the existence of some of its /sys files, under /sys/kernel/mm/ksm/
|pages_shared||how many shared pages are being used|
|pages_sharing||how many more sites are sharing them i.e. how much saved|
|pages_unshared||how many pages unique but repeatedly checked for merging|
|pages_volatile||how many pages changing too fast to be placed in a tree|
|full_scans||how many times all mergeable areas have been scanned|