Skip to main content

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.

vm.swappiness=10

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 2.6.34.7-61.fc13.x86_64 (mockbuild@x86-02.phx2.fedoraproject.org) (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.

Initial
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.

Comments

Popular posts from this blog

Converting Java Map to String

Java Collections framework, String manipulation etc is something that we often encounter in Development process.
For processing collections (like checking null/empty, Intersection, Disjunction) We do have some of the very use full libraries.

Some of the Collection related libraries are Apche Commons Collections and Google  Collections(Guava).

Problem Use Case

This article explains how to convert a Java Map to String(and vice versa) using different libraries and technique.

One way is to use StringBuilder(Or String) and loop though the Map and build the String by applying some sort of separator ( for key:value and entry). Here we have to take care of the null value etc.

Without Any Library
If we want to convert the map to a String with key value separator and also individual entry seperator in the resulting String, we have to write code for that. For a simple Map, we have to iterate though the map, take care of the null values etc. Following is a sample to get String built out from Map C…

Masking Credit Card number in Java

Sometimes we need to mask crucial information like Credit Card Numbers, CVV numbers etc before storing  or logging the information. This example mask Credit Card Number (Except last 4 Digit) from a Text which contains information along with Credit Card Number.

The following example demonstrates how we can easily mask the credit card with Matcher and Pattern Classes. This Sample Code uses Matcher and Pattern.
Pattern Used in this sample is not optimized for Credit Card Numbers, this pattern will get any numerical numbers in the String Content.  Based on the Credit Card Type a more efficient and Strict RegEx can be used to mask the Credit Card.
/**Mask the Credit card number but last four digit value **/   Pattern PATTERN = Pattern.compile("[0-9]+"); String message = content; Matcher matcher = PATTERN.matcher(message); String maskingChar = "*"; StringBuilder finalMask = new StringBuilder(maskingChar); while …

Invoking EJB deployed on a remote machine

Invoking EJB deployed on a remote machineIn case we are calling remote ejb( ejb deployed on remote machines),The JNDI lookup might lookup like,Properties env = new Properties();env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");env.put(Context.PROVIDER_URL, "XX.XXX.XX.XX:1099");env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); Context ctx = new InitialContext(env);If we are calling local ejb then we can simply create InitialContext without any parameters.Like,Context ctx = new InitialContext();