Tuesday, September 15, 2009

what is java memory leakage?

In java all the object are freed automatically by Garbage Collector when object are not in use. but some situation Garbage Collector unable free an object. because when an object reference is stored in anther object Garbage Collector can't free that object.


Vector store = new Vector();
String notGarbageCollectedObject =new String("StringObject");

until you will remove ( store.remove(notGarbageCollectedObject )) the String object reference (notGarbageCollectedObject ) from vector Garbage Collector not deallocates that object. this causes memory leakage.

There are three main behaviors in java application when there is a memory leakage.

  1. If your application is leaking memory overtime, eventually you are going to get

java.lang.OutOfMemoryError exception.

  1. Application heap usage will grow continuously( not application size)

  2. Application will terminate automatically.

RSS value is different to heap space.

Java heap is the heap size allocated to JVM applications which takes care of the new objects being created. If the objects being created exceed the heap size, it will throw an error saying memoryOutofBound. Default heap size is 63mb. It can be modified using command -Xms32m -Xmx512m (minimum 32mb maximum 512mb).

Java uses shared libraries, thread space and other resources to execute. RSS is the amount of resident memory which can change without the application using more or less memory (it how much has been put into physical memory).

find heap size using following java API.

Runtime runtime = Runtime.getRuntime();

long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();

System.out.println("allocated memory: " +( allocatedMemory / 1024)/1024+"MB"); System.out.println("max memory: " + (maxMemory /1024)/1024+"MB"); System.out.println("total free memory: " + ((freeMemory + (maxMemory - allocatedMemory)) / 1024)/1024+"MB");


free memory: 39MB
allocated memory: 81MB
max memory: 508MB
total free memory: 466MB .

how to find java memory leakage?

by using memory profiler you can find memory leakage

links for memory profiler

JMP: (Java Memory Profiler) free memory profiler from Khelekore http://www.khelekore.org/jmp/




java 1.6 it self has tool find memory usage .

No comments:

Post a Comment