Wednesday, August 24, 2011

Understanding Java Heap Space and Memory Tuning

What lies on Heap -

  1. Instance variables (including instance level references to objects) and the Objects lie on HEAP.
  2. This includes the Objects created in local scope of any methods. In this case, reference variables to local objects are stored with method frame in a stack, but actual object lies in heap.

What lies on Stack -

  1. Local variables and methods lie on the Stack.
  2. Every thread, including the main thread, daemons threads - get their own stack.
  3. When a thread invokes a method, the JVM pushes a new frame onto that thread's Java stack.
  4. All method calls, arguments, local variables, reference variables, intermediate computations and return values if any are kept in these stack corresponding to the method invoked.
  5. The memory for stack area does not need to be contiguous.

Java Heap Memory is part of Memory allocated to JVM by Operating System.

Heap Regions

Java Heap space is divided into three regions or generation for sake of garbage collection - New Generation, Old or tenured Generation or Perm (permanent) Space. JVM allocates every generation its own memory pool. The JVM has at least one memory pool and it may create or remove memory pools during execution.

  1. Eden Space (heap - young generation):
    1. pool from which memory is INITIALLY allocated for most objects.
  2. Survivor Space (heap - young generation):
    1. pool containing objects that have survived GC of eden space.
  3. Tenured Generation (heap - old generation):
    1. pool containing objects that have existed for some time in the survivor space.
  4. Permanent Generation (non-heap - stack):
    1. holds all the reflective data of the virtual machine itself, stores class level details, loading and unloading classes (e.g. JSPs), methods, String pool.
  5. Code Cache (non-heap):
    1. HotSpot JVM also includes a "code cache" containing memory used for compilation and storage of native code.

Heap Allocation and collection (Garbage Collection)

The VM initially assigns all objects to the eden space, and most objects die there. When it performs a minor (or partial) GC, the VM moves any remaining objects from the eden space to one of the survivor spaces. The VM moves objects that live long enough in the survivor spaces to the "tenured" space in the old generation. When the tenured generation fills up, there is a full GC that is often much slower because it involves all live object

Java Heap Memory is part of Memory allocated to JVM by Operating System.


Whenever we create objects they are created inside Heap in Java.

Java Garbage collector is responsible for reclaiming memory from dead object and returning to Java Heap space.

JVM has memory manager that manages one or more memory pools. GC is also a memory manager.

You can increase or change size of Java Heap space by using JVM command line option java -Xms1G -Xmx2G MyJavaCode

You can use either JConsole or Runtime.maxMemory(), Runtime.totalMemory(), Runtime.freeMemory() to query about Heap size programmatic in Java.

java.lang.OutOfMemoryError - Means JVM ran out of heap memory that was provided by Xmx. It may resolve by increasing heap size but if it’s recurrent then look for memory leak in Java.

Tuning Heap:

Garbage collection can become a bottleneck in highly parallel systems. By understanding how GC works, it is possible to use a variety of command line options to minimize that impact.

Use the jConsole to see how the different generations of memory is behaving for your application. Do the analysis and make adjustments as mentioned below.

1) Total Heap, free available memory ratio, but within bounds.

Java heap allocation starts with min size -Xms and increases upto Xmx. At any point, it has used heap(heap actually in use), committed heap(allocated heap at that point. includes used + free), max heap(max heap that can be allocated)

-XX:MinHeapFreeRatio=40 -XX:MaxHeapFreeRatio=70 -Xms1G -Xmx2G

Since collections occur when generations fill up, throughput is inversely proportional to the amount of memory available. Total available memory is the most important knob affecting GC performance. We can set it as a percentage by the parameters -XX:MinHeapFreeRatio=<minimum> and -XX:MaxHeapFreeRatio=<maximum>, and the total size is bounded below by -Xms and above by -Xmx .

2) Heap dedicated to Young Generation

The bigger the young generation, the less often minor collections occur. However, for a bounded heap size a larger young generation implies a smaller old generation, which will increase the frequency of major collections (full GC's)

-XX:NewRatio=3 means that the ratio between the young and old generation is 1:3; in other words, the combined size of eden and the survivor spaces will be one fourth of the heap.

The parameters NewSize and MaxNewSize bound the young generation size below and above. This is useful for tuning the young generation at a finer granularity than the integral multiples allowed by NewRatio.

java -XX:NewRatio=3 myJavaCode
java -XXNewSize=2M -XXMaxNewSize=32m myJavaCode

HotSpot JVM -

There are various JVMs available, depending upon hardware and Operating system. HotSpot JVM is one that implements the Java Virtual Machine Specification, and is delivered as a shared library in the Java Runtime Environment.

On my 64 bit intel i7 MacBookPro, I have -
Virtual Machine: Java HotSpot(TM) 64-Bit Server VM version 20.1-b02-384
JIT compiler: HotSpot 64-Bit Tiered Compilers



  1. Hi Ashish,

    This is a well done summary of the Java Heap Space.

    You may also be interested in the OutOfMemoryError: Java Heap Space troubleshohoting techniques as well.


  2. Finally you made me understand the java heap memory.
    Thank you very much.

  3. Awesome work. Thanks a lot for this post and your others. please add more stuff to your blog

    1. I have read your blog its very attractive and impressive. I like it your blog.

      Digital Marketing Company in Chennai Digital Marketing Agency

  4. I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs. J2EE Training in Chennai

  5. The expansion of internet and intelligence in business process lead the way to huge volume of data. It is important to maintain and process these data to be efficient in data handling. Hadoop Training in Chennai | Big Data Training in Chennai

  6. Dear admin, The way you have explained the concept is mezmerizing. Thank you so much for sharing tis worth able content with us. The concept taken here will be useful for my future programs and i will surely implement them in my study. Keep blogging article like this.

    Best JAVA Training institute in Chennai | Best JAVA Training in Chennai | Android training in chennai

  7. Cloud has beome the common word that is being used by most of the professional these day. The reason for relying on this technology is security. Your content too lecture the same. Thanks for sharing this worth able information in here. Keep blogging article like this.

    Hadoop Training Chennai | Big Data Training | Big Data Hadoop Training in Chennai

  8. Best Java Training Institute In ChennaiThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

  9. Informative explanation on Java heap space and memory tuning!! I am always interested in studying Java and PHP training in Chennai

  10. Great blog..You have clearly explained about the concept..Step by step explanation is too good to understand..Its very useful for me to understand..Keep on sharing..
    Hadoop training in chennai

  11. yours idea is really good and innovative , these resources are really awesome thanks for sharing those information and i got more in formation about this concept.

    digital marketing training in chennai

  12. Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info! , Regards, servicenow training in hyderabad

  13. My Arcus offer java training with 100% placement. Our java training course that includes fundamentals and advance java training program with high priority jobs. java j2ee training with placement having more exposure in most of the industry nowadays in depth manner of java

    java training in chennai

  14. Very nice. I like your post. I have interest to study java.

    digital marketing training in chennai

  15. Thanks for giving important information to training seekers,Keep posting useful information,Click below to find

    Java and J2EE Training in Chennai - AmitySoft

  16. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Java Training in Chennai | Core Java Training in Chennai

    Online Java Training Java Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai


  17. Thank you for your feedback. We're glad you enjoyed the post. Feel free to share it with others you think may benefit from this information.

    Best Java Training in Chennai

  18. Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

  19. Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training

    Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai Java Online Training Java Online Training

  20. Can u give me information regarding to wily interscope for monitoring GC

  21. The blog gave me idea about java heap space Thanks for sharing it
    Hadoop Training in Chennai

  22. Hi, Really your post was very informative. Today's internet era learn Hadoop Online Training will helps you to reach your goal.Selenium Training

  23. This information is impressive; I am inspired by your post writing style & how continuously you describe this topic.
    Selenium Training in Chennai
    Selenium Training Institute in Chennai

  24. Thanks for helping me to understand Understanding Java Heap Space and Memory Tuning concepts. As a beginner in java programming your post help me a training in chennai | Best java training in chennai

  25. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in TECHNOLOGY , kindly contact us
    MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Saurabh Srivastava
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383

  26. Amazing blog about the various informative information on the programming languages.
    Java Training in Chennai

  27. Excellent post. This article is really very interesting and effective. I think its must be helpful for us. Thanks for sharing your informative. Six Sigma Exam Center in Chennai | Six Sigma Exam Center in Velachery.

  28. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information&its very useful to me...
    Android training in chennai
    Ios training in chennai


  29. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Android App Development Company

  30. I read this article. I think You put a lot of effort to create this article. I appreciate your work.

    PhD Thesis Writing Services
    Dissertation Writing Services
    Research Paper Writing Services