Main menu

Java heap and memory issues in JVM

As a system admin i less care about what inside the code, but when it comes to performance people always look at me. I often get the error java.lang.OutOfMemoryError, and any noob can understand that JAVA is out of memory. Here i first met with JAVA Heap memory. Analyzing the heap memory is very important for performance. Profiling and heap analysis is a better way  to find the Java heap and memory issues in JVM

What is JAVA Heap memory?

JAVA execute each application in a virtual environment call JVM (Java Virtual Machine). When we run a JAVA application JAVA get memory from System and assign it to JVM. This memory is called JAVA heap memory. JVM share this memory for all Virtual machine threads or Applications.
When we create an object in JAVA a memory is allocated to Object from heap memory and when object dies it get back to Heap Memory.

Configure Heap size in JAVA

Default heap size of most of JAVA versions is 128M. Most of small application run without any problem with it but if your applications getting java.lang.OutOfMemoryError issues sure you need to tune your JVM settings. But it can be modify via JVM setting..
-Xms : Heap initial or minimum size
-Xmx : Heap maximum size

Java heap and memory issues in JVM

When JVM starts it claim the initial memory size defined by -Xms. As application runs for a while many new objects created and destroyed. Garbage collector reclaim the memory from dead objects periodically. When new object is created JVM expand to assign memory to it and it expand maximum heap size defined by -Xmx parameter. If all heap memory is full and there isn’t any space left to grow the heap memory, JVM try to run garbage collector  to free some memory or JVM throws java.lang.OutOfMemoryError error and application dies.

How to monitor JAVA help memory for memory leakage.

If you feel that your application having the memory leakage you can use any profiler or heap analyzer to find memory leakage. Jconsole and other tools comes with default in JDK6 or newer version.
Jconsole : You can use this GUI utility to connect local or Remote process to analyze the heap status.
jmap: You can use this command to generate the heap dump and analyse this dump later.
jhat: You can analyze the heap dump with jhat.

There are some other tool available worth looking