Thursday, December 2, 2010

Better JVM in linux : Swapiness

Recently I found interesting issue regarding JVM and Linux Swapiness. I was getting java out of memory while compiling my Java application.
I have 8GB/Linux, and I was getting Java out of memory while compiling it alone (Without any other memory intensive job), also the overall build time was rediculously high.

I googled and tried some tricks on the machine, after that my build time was significantly better and  most importantly, I did not get any out of memory from JVM.

What I did is that I set the swapiness of the system to 10 from 60.

Swapiness is a value between 0 to 100 and it's a parameter to the linux kerner to take decision on how aggressively to take processes out of the RAM to Swap Space in the Hard disk.

Swapiness with 0 tells the kernel to avoid using Swap as far as possible, but it does not mean Swap wont be used at all.

Swapiness with value 100 tells the kernel to use Swap space aggressively.

You can check swapiness of your linux system using the following command,

cat /proc/sys/vm/swappiness

The default value of swapiness in most linux distribution is 60.

To set the swapiness value temporarily in the system use the following command,

sudo sysctl vm.swappiness=10

To make a change permanent, edit the configuration file with your favorite editor:

gksudo gedit /etc/sysctl.conf

Search for vm.swappiness and change its value as desired.


You need root privilege to update the swapiness value.

Also you can use /proc/sys/vm/drop_caches to claim/free memory not required, but drop_cashes is only supported by  linux kernel >2.4.

To know the Kernel Version execute the following command,

cat /proc/version

for my machine it says,
[root@localhost bin]# cat /proc/version
Linux version ( (gcc version 4.4.4 20100630 (Red Hat 4.4.4-10) (GCC) ) #1 SMP Tue Oct 19 04:06:30 UTC 2010

We can free memory useing the following commands,

sync; echo 1 > /proc/sys/vm/drop_caches
sync; echo 2 > /proc/sys/vm/drop_caches
sync; echo 3 > /proc/sys/vm/drop_caches

Effect of the above 3 commands can be seen from the following data,

I was running  a machine with 8GB RAM,and swapiness value = 10, i checked the status of the system with TOP Command.

Initially when I was running a lot of appications along with a VMWare with 1GB memory, then I stopped the VMWare and at last I fired the drop caches commands to see the difference in memory and swap status.

Mem:   8192252k total,  4167144k used,  4025108k free,     6840k buffers
Swap:  6143996k total,  1815928k used,  4328068k free,   265880k cached

Closing VM
Mem:   8192252k total,  4260904k used,  3931348k free,    14208k buffers
Swap:  6143996k total,  1397360k used,  4746636k free,  1375056k cached

Closing VM and Executing drop_caches
Mem:   8192252k total,  3057004k used,  5135248k free,     1180k buffers
Swap:  6143996k total,  1397144k used,  4746852k free,   214112k cached

So using this two tricks I am able to compile/build my Java Application well.

No comments :

Post a Comment