Thursday, June 18, 2015

Clearing RAM cache and SWAP memory in Linux

If your server get hanged, RAM cache is one of the major reason,. It happens frequently on web server when we are under attack or web developer dosen't consider about performance of application. Miss configuration of servers like mysql and apache is one of the main reason.

We can see ram cache amount on htop, which is user friendly monitoring tool for Linux servers. Because of it we may loss some connections and it's interaction with server. We can do this by our own risk.

What is RAM cache ?

It is a portion of memory made of high-speed static RAM (SRAM) instead of the slower and cheaper dynamic RAM (DRAM) used for main memory. Memory caching is effective because most programs access the same data or instructions over and over.

To clear cache :

free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m

To clear pagecache change value of echo to 1 on about commend, to clear dentries and inodes change value of echo to 2 on about commend, echo 3 will clear all of three pagecache, dentries and inodes.

To clear swap:

Up-to I know that there is no straight way to this, But we can do it by disable and enable the swap, We can do this by following command,

To turn off : swapoff -a
To turn on : swapon -a 

Shell script for read line from text file

My need: Read line from the text file by shell script and append few text before and after the line. Script: #!/bin/bash file="...