Enables legacy applications to leverage Linux’s memory deduplication. Only works on Linux ≥ 2.6.32.
If you have multiple processes generating similar data (like multiple applications serving different vhosts), this easy to use tool may allow you to spare memory by deduplicating it. If two pages (that is on, most systems, consecutive blocks of 4k) are identical, they will be merged into a single page to reduce memory usage.
Linux ≥ 2.6.32 features a memory-saving mechanism that works by deduplicating areas of memory that are identical in different processes (even if they were generated at runtime and after the
of their common ancestors).
This mechanism requires the application to opt-in using the
syscall. KSM Preload enables legacy applications (about any current application) to leverage this system by calling
on every heap-allocated pages.
In contrast to similar hacks, special care has been taken to make it safe to use, including with other preloaded libraries.
I did a very few experiments to evaluate the efficiency of the method. They’re not worth much, but still…
- Deduplicated: Pages which were effectively deduplicated, sparing memory.
- Unique: Pages that could not be deduplicated because there were no other copies of them on the system. The system lost CPU time trying to deduplicate them.
- Volatile: Pages that could not be deduplicated because they’re changing to often. The system lost CPU time trying to deduplicate them.
|Static pages under Siege||15289||3239||1116||~60MB|
|PhpBB under siege||25534||36687||775||~100MB|
To run your programs using ksm_preload, first download the stable version. then compile it with “make”, and use the provided ksm-wrapper :
make sudo ./ksm-wrapper echo "I am being run via ksm-preload"
In order to see if it worked, you can run the following snipet :
cd /sys/kernel/mm/ksm watch 'for f in *; do echo -n $f: ; cat $f; done'
The interpretation is the same as for the “Numbers” section above.