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.

Notes

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.

http://download.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html

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.

Example:
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

References
http://download.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html
http://java.sun.com/docs/hotspot/gc/index.html

48 comments:

  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.

    Regards,
    P-H
    http://javaeesupportpatterns.blogspot.com

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

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

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

      Digital Marketing Company in Chennai Digital Marketing Agency

      Delete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    digital marketing training in chennai

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

    Java and J2EE Training in Chennai - AmitySoft

    ReplyDelete
  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

    ReplyDelete

  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

    ReplyDelete
  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

    ReplyDelete
  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

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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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 lot.java training in chennai | Best java training in chennai

    ReplyDelete
  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 http://www.maxmunus.com/contact
    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
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383
    http://www.maxmunus.com/


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

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

    ReplyDelete
  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

    ReplyDelete

  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

    ReplyDelete
  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

    ReplyDelete
  31. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  32. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.i am expecting more updated posts from your hands.
    iOS App Development Company
    iOS App Development Company

    ReplyDelete
  33. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

    ReplyDelete
  34. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

    ReplyDelete
  35. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

    ReplyDelete
  36. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Thank you for some other informative website.
    Back Office Services

    ReplyDelete
  37. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

    ReplyDelete
  38. Excellent information from your blog to read..I understand the concepts of Java heap space and memory tuning....Keep sharing...
    Java Training Institute in Chennai | DotNet Training Institute in Chennai | Web Designing Training Institute in Chennai

    ReplyDelete