Saturday, September 27, 2014

Java Memory Overview - Java Heap Size, Perm Gen Size and Java Stack Size.

             There are 2 quite common ‘Out of Memory’ issues. The 1st one is ‘Heap Size’ and the 2nd one is ‘PermGen Space’.For Java developer, knowledge of Heap in Java, setting size of java heap /PermGen space , dealing with java heap space OutOfMemoryError, analyzing heap dumps is very important.

Heap Space :

When a Java program starts, Java Virtual Machine gets some memory from Operating System. Java Virtual Machine or JVM uses this memory for all its need and part of this memory is call java heap memory. Heap in Java generally located at bottom of address space and move upwards. whenever we create object using new operator or by any another means object is allocated memory from Heap and When object dies or garbage collected ,memory goes back to Heap space in Java

For a heavy Java process, insufficient Heap size will cause the popular java.lang.OutOfMemoryError: Java heap space.

-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size
 
java -Xms512m -Xmx1024m HelloProgram

Perm Gen Space :

PermGen Space  is used to store classes and Meta data about classes in Java. When a class is loaded by a classloader it get stored in PermGen space, it gets unloaded only when the classloader which loaded this class got garbage collected. If any object retains reference of classloader than its not garbage collected and Perm Gen Space is not freed up. This causes memory leak in PermGen Space and eventually cause java.lang.OutOfMemoryError: PermGen space. Another important point is that when you deploy your web application a new Clasloader gets created and it loads the classes used by web application. So if Classloader doesn't get garbage collected when your web application stops you will have memory leak in tomcat.

As a performance optimization the permanent generation was created and classes were put into it.Classes are part of our JVM implementation and we should not fill up the Java heap with our data structures. Permanent Generation is allocated outside the heap size. The permanent Generation contains the following class information:

    Methods of a class.
    Names of the classes.
    Constants pool information.
    Object arrays and type arrays associated with a class.
    Internal objects used by JVM.
    Information used for optimization by the compilers.

If a large code-base project is loaded, the insufficient Perm Gen size will cause the popular Java.Lang.OutOfMemoryError: PermGen.

‘Java.Lang.OutOfMemoryError: PermGen Space’ occurs when JVM needs to load the definition of a new class and there is no enough space in PermGen. The default PermGen Space allocated is 64 MB for server mode and 32 MB for client mode. There could be 2 reasons why PermGen Space issue occurs.

The 1st reason could be your application or your server has too many classes and the existing PermGen Space is not able to accommodate all the classes.


-XX:PermSize<size> - Set initial PermGen Size.
-XX:MaxPermSize<size> - Set the maximum PermGen Size.

java -XX:PermSize=64m -XX:MaxPermSize=128m HelloProgram

And the 2nd reason could be memory leak. How the class definitions that are loaded could can become unused.

Normally in Java, classes are forever. So once the classes are loaded, they stay in memory even if that application is stopped on the server. Dynamic class generation libraries like cglib use lot of PermGen Space since they create a lot of classes dynamically. Heavy use of Proxy classes, which are created synthetically during runtime. It’s easy to create new Proxy classes when a single class definition could be reused for multiple instances.

Spring and Hibernate often makes proxies of certain classes. Such proxy classes are loaded by a classloader. The generated class definitions are never discarded causing the permanent heap space to fill up fast.

Use Profiler and Heap dump Analyzer tool to understand Java Heap space and how much memory is allocated to each object. Java Heap dump is a snapshot of Java Heap Memory at a particular time. This is very useful to analyze or troubleshoot any memory leak in Java or any Java.lang.OutOfMemoryError. You can take heap dump using tool like VisualVm mentioned at the end of the post.

For PermGen space issues, you will need identify the cause of leak and fix it. Increasing the PermGen space will not help, it will only delay the issue, since at some point the PermGen space will still be filled up.

If you are using Tomcat and are haunted by memory leaks, the latest versions of Tomcat have capability to fix the some of the memory leak issues.


Java Stack Size
:

Each thread has its own stack,. primitive types and references live on stack. No object can live on stack. If a project has a lot of threads processing, try reduce this stack size to avoid running out of memory.

-Xss = set java thread stack size

java -Xss512k HelloProgram


JAVA_OPTS To Set vm Variables:

set JAVA_OPTS = -Xss512k -Xms512m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=1024m

 
Tool for Troubleshooting :

VisualVm :

VisualVM is a visual tool integrating several command line JDK tools(like stat, jinfo, jstack, and jmap etc) and lightweight profiling capabilities. Designed for both production and development time use, it further enhances the capability of monitoring and performance analysis for the Java SE platform.

Download Link :
http://visualvm.java.net/download.html


No comments:

Post a Comment