Linux and PHP web application support and development (Bromsgrove, UK)

tel: 0845 0046746

Saving memory under Linux when using virtualisastion

If you’re running low of memory on a server, there may be scope to free up memory through use of ‘ksm‘.

(Note, that the kernel will start looking for memory to de-duplicate if KSM is turned on AND there are some processes running which have notified the kernel that they’re candidates; ‘kvm’ does the latter.)

Assuming a recent enough kernel (that’s anything since about v2.6.32), turning it on is a simple :

echo 1 > /sys/kernel/mm/ksm/run

to enable, and

echo 0 > /sys/kernel/mm/ksm/run

to disable.

There are a few tunable settings – sleep_millisecs and pages_to_scan – which you may wish to adjust if you feel KSM is gobbling up too much CPU time.We’ve arbitrarily set them to 5000 and 100 respectively. Increasing sleep_millisecs and decreasing pages_to_scan will make the algorithm less aggressive and reduce CPU usage.

Once running, memory should drop – in our case we’re saving about 1gb of memory on a host with 16gb of memory and 6 kvm instances — pages_sharing lists the number of 4kb pages shared between processes as a result of ksm —

/sys/kernel/mm/ksm/pages_sharing 285603

(about 1115mb).

Leave a Reply

Your email address will not be published. Required fields are marked *