Wednesday, 23 September 2015

Java Heap and Garbage Collection

Java has the concept of a garbage collector.  When objects are no longer needed the JVM will automatically identify and clear the memory space for us.

1. What's the  negative side of Garbage Collection
  • When a garbage collection occurs it has an effect on the application performance, notably slowing it down or stopping it.  In so called “Stop the world” garbage collections the rest of the application will freeze whilst this occurs. This is can be unacceptable depending on the application requirements, although GC tuning can minimise or even remove the impact.
  • Although it’s possible to do a lot of tuning with the garbage collector, you cannot specify when or how the application performs GC.
2. How does Generational GC work?  How is the Java Heap structured?

  • All objects are stored on the Heap (while variables and methods are stored in Stack along with references to objects in the heap). 
  • Garbage Collection is the process of removing Objects which are no longer needed from the Heap and returning the space for general consumption. 
  • Almost all GCs are “generational”, where the Heap is divided into a number of generations.  This has proven significantly more optimal which is why almost all collectors use this pattern.
3. New Generation

Most applications have a high volume of short lived objects.  Analyzing all objects in an application during a GC would be slow and time consuming, so it therefore makes sense to separate the shortlived objects so that they can be quickly collected. As a result all new Objects are placed into the new generation.

4. Old Generation

Any objects that survive from survivor spaces in the New Generation are promoted to the Old Generation. The Old Generation is usually much larger than the New Generation.  When a GC occurs in old gen it is known as a full GC.  Full GCs are also stop-the-world and tend to take longer, which is why most JVM tuning occurs here.

5. PermGen

The PermGen is where the JVM stores the metadata about classes.  It no longer exists in Java 8, having been replaced with metaspace. Generally the PermGen doesn’t require any tuning above ensuring it has enough space, although it is possible to have leaks if Classes are not being unloaded properly.

6. What's the heap size limit?

The maximum theoretical heap limit for the 32-bit JVM is 4G. Due to various additional constraints such as available swap, kernel address space usage, memory fragmentation, in practice the limit can be much lower. On most modern 32-bit Windows systems the maximum heap size will range from 1.4G to 1.6G. On 64-bit operating systems running the 32-bit VM, the max heap size can be higher, approaching 4G on many Solaris systems. 

7. What flags can I use to tune the JVM and GC?
There are textbooks available on tuning the JVM for optimal Garbage Collection.  Nonetheless it’s good to know a few for the purpose of interview.
-XX:-UseConcMarkSweepGC: Use the CMS collector for the old gen.
-XX:-UseParallelGC: Use Parallel GC for New Gen
-XX:-UseParallelOldGC: Use Parallel GC for Old and New Gen.
-XX:-HeapDumpOnOutOfMemoryError: Create a thread dump when the application runs out of memory. Very useful for diagnostics.
-XX:-PrintGCDetails: Log out details of Garbage Collection.
-Xms512m: Sets the initial heap size to 512m
-Xmx1024m: Sets the maximum heap size to 1024m
-XX:NewSize and -XX:MaxNewSize: Specifically set the default and max size of the New Generation
– XX:NewRatio=3: Set the size of the Young Generation as a ratio of the size of the Old Generation.
-XX:SurvivorRatio=10: Set the size of Eden space relative to the size of a survivor space.

8. What is difference between Serial and Throughput Garbage collector?

Serial Garbage collector is a stop the world GC which stops application thread from running during both minor and major collection. Serial Garbage collector can be enabled using JVM option -XX:UseSerialGC and it's designed for Java application which doesn't have pause time requirement and have client configuration. Serial Garbage collector was also default GC in JDK 1.4 before ergonomics was introduced in JDK 1.5. Serial GC is most suited for small application with less number of thread while throughput GG is more suited for large applications. On the other hand Throughput garbage collector is parallel collector where minor and major collection happens in parallel taking full advantage of all the system resources available like multiple processor. Though both major and minor collection runs on stop-the-world fashion and introduced pause in application. Throughput Garbage collector can be enable using -XX:UseParallelGC or -XX:UseOldParallelGC. It increases overall throughput of application by minimizing time spent in Garbage collection but still has long pauses during full GC.This is a kind of Garbage collection interview questions which gives you an opportunity to show your knowledge in detail while answering. I always suggest to answer these kind of questions in detail.

9. When does an Object becomes eligible for Garbage collection in Java ?

An object becomes eligible for garbage collection when there is no live reference for that object or it can not be reached by any live thread. Cyclic reference doesn’t count as live reference and if two objects are pointing to each other and there is no live reference for any of them, than both are eligible for GC. Also Garbage collection thread is a daemon thread which will run by JVM based upon GC algorithm and when runs it collects all objects which are eligible for GC.

10. What is finalize method in Java ? When does Garbage collector calls finalize method?

Finalize method in Java also called finalizer is a method defined in java.lang.Object and called by Garbage collector before collecting any object which is eligible for GC. Finalize() method provides last chance to object to do cleanup and free any remaining resource

11. Does Garbage collection occur in permanent generation space in JVM?

PermGen maintains class Meta data and String pool.Garbage Collection does occur in PermGen space and if PermGen space is full or cross a threshold, it can trigger Full GC. This is why correct sizing of PermGen space is important to avoid frequent full GC. You can control size of PermGen space by JVM options -XX:PermGenSize and -XX:MaxPermGenSize.

12. How to you monitor garbage collection activities? 

You can monitor garbage collection activities either offline or real-time. You can use tools like JConsole and VisualVM VM with its Visual GC plug-in to monitor real time garbage collection activities and memory status of JVM or you can redirect Garbage collection output to a log file for offline analysis by using -XlogGC=<PATH> JVM parameter. Anyway you should always enable GC options like -XX:PrintGCDetails -X:verboseGC and -XX:PrintGCTimeStamps as it doesn't impact application performance much but provide useful states for performance monitoring.

13. Look at below Garbage collection output and answer following question :

       [ParNew: 1512K->64K(1512K), 0.0635032 secs]
       15604K->13569K(600345K), 0.0636056 secs]
       [Times: user=0.03 sys=0.00, real=0.06 secs]

 1. Is this output of Major Collection or Minor Collection ?
 2. Which young Generation Garbage collector is used ?
 3. What is size of Young Generation, Old Generation and total Heap Size?
 4. How much memory is freed from Garbage collection ?
 5. How much time is taken for Garbage collection ?
 6. What is current Occupancy of Young Generation ?

This Garbage collection Interview questions is completely based on GC output. Following are answers of above GC questions which will not only help you to answer these question but also help you to understand and interpret GC output.

Answer 1:  It's Minor collection because of "GC" word, In case of Major collection, you would see "Full GC".

Answer 2: This output is of multi-threaded Young Generation Garbage collector "ParNew", which is used along with CMS concurrent Garbage collector.

Answer 3: [1512K] which is written in bracket is total size of Young Generation, which include Eden and two survivor space. 1512K on left of arrow is occupancy of Yong Generation before GC and 64K is occupancy after GC. On the next line value if bracket is total heap size which is (600345K). If we subtract size of young generation to total heap size we can calculate size of Old Generation. This line also shows occupancy of heap before and after Garbage collection.

Answer 4: As answered in previous garbage collection interview question, second line shows heap occupancy before and after Garbage collection. If we subtract value of right side 13569K, to value on left side 15604K, we can get total memory freed by GC.

Answer 50.0636056 secs on second line denotes total time it took to collect dead objects during Garbage collection. It also include time taken to GC young generation which is shown in first line (0635032 secs).

Answer 6: 64K 

Read more:


No comments:

Post a Comment