Clear Linux memory cache


There was a alert showing that the swap free space is low in a RHEL 5.6 box. I logged in and checked, the box has 13G RAM, but only 1G swap. First, this is not a good practice, as RedHat recommends 4G swap for RHEL5 with 4-16G RAM.

free -m
total used free shared buffers cached
Mem: 13788 13134 654 0 609 4948
-/+ buffers/cache: 7576 6212
Swap: 1023 898 125

Secondly, to fix it I need to increase the curret swap space, or add another swap file or partition. The process is pretty straight forward, here is a example:

1) Create a 512 M file: dd if=/dev/zero of=/home/swapfile bs=1024 count=524888
2) Setup the swap file: mkswap /home/swapfile
3) Add a new entry in the /etc/fstab: /home/swapfile swap swap defaults 0 0
4) Mount all swap defined in /etc/fstab: swapon -av or only mount the new one: swapon -v /home/swapfile

But I noticed that the cached RAM is high, I should be able to release that cache to increase the free RAM. Here is how to:

1) Ensure flush the cache back to disk first: sync
2) Tell kernel to drop or clean pagecache, dentries and inodes: echo 3 > /proc/sys/vm/drop_caches (this is just one time action. cat /proc/sys/vm/drop_caches tells you what the last action was taken).
3) Turn off swap to force it flush back to RAM: swapoff -av
4) Turn the swap back on: swapon -av

References:

Writing to file /proc/sys/vm/drop_caches instructs the kernel to drop or clean the page caches (pagecache), dentries (implemented through slab cache), and inodes (implemented through slab cache) and thus causing the memory to become reclaimed and available. Clean data cache pages are not freed by design. They can be easily reclaimed by the kernel if or when extra memory is needed, but in the meantime they contain useful data from disks that if needed again saves disk io.

Enabling drop_caches can cause deadlock if the system is under heavy memory and I/O load. It is not recommended to use this feature on system experiencing heavy load.

Red Hat Enterprise Linux 4.6 and 5.0 introduced /proc/sys/vm/drop_caches. There were issues in 4.6 through 5.2 releases that could cause deadlocks between kjournald and drop_pagecache if the system was under heavy io load. This issue persists within RHEL 4 releases but was fixed in RHEL 5.3 and later.

Before running the commands use sync to make sure dirty cache pages are flushed to disk first to maximize the number of data cache pages that can be reclaimed.

To free the pagecache only:
# echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes only:
# echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
# echo 3 > /proc/sys/vm/drop_caches
Please note that there is no way to tune how the cache is dropped, how much is dropped, or when it is dropped.

Red Hat Enterprise Linux 5

Amount of installed RAM Recomended swap space
4GB or less 2GB swap space
4GB – 16GB 4GB swap space
16GB – 64GB 8GB swap space
64GB – 256GB 16GB swap space

Note: A swap space of at least 100GB is recommended for systems with over 140 logical processors.

Red Hat Enterprise Linux 6, Red Hat Enterprise Linux 7, Red Hat Enterprise Open Stack Platform 3 and Red Hat Enterprise Open Stack Platform 4

Amount of installed RAM Reccomended swap space Recommended swap space if allowing for hibernation
2GB or less Twice the installed RAM 3 times the amount of RAM
> 2GB – 8GB The same amount of RAM 2 times the amount of RAM
> 8GB – 64GB At least 4GB 1.5 times the amount of RAM
> 64GB or more At least 4GB Hibernation not recommended
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s