Java 2018 Latest Interview questions

How garbage collection works internally in Java?

-->
    • All Java objects are always created on heap in java.
    • What is GC (Garbage collection) process in java?
      • GC (Garbage collection) is the process by which JVM cleans objects(unused objects) from heap to reclaim heap space in java.
    • What is Automatic GarbageCollection in JVM heap memory in java?
      •  Automatic garbage collection is the process of Identifying objectswhich are in use in java heap memoryand which objects are not in use in java heap memory and deleting the unused objects in java heap memory.
    • How to identify objects which are in use in java heap memory?
      • Objects in use (or referenced objects) are those objects which is still needed by java program, some part of java program is still pointing to that object.
    •  Which objects are NOT in use in java heap memory?
      • Objects not in use (or unreferenced objects or unused objects) are those objects which is not needed by java program, no part of java program is pointing to that object. So, these unused objects can be cleaned in garbage collection process and memory used by an unreferenced object can be reclaimed.
    • GC (Garbage collection) process automatically clears objects from heap to reclaim heap space. You just need to specify the type of garbage collectortype you want to use at JVM startup.
      • Gc (garbage collector) calls finalize methodfor garbage collection. Finalize method is called only onceby garbage collector for an object in java.
      • Daemon threads are low priority threads which runs intermittently in background for doing garbage collection (gc) in java.
      • We can force early gc (garbage collection) in java by using following methods                       

        System.gc();
        Runtime.getRuntime().gc();
        System.runFinalization();
        Runtime.getRuntime().runFinalization();
        
    • By calling these methods JVM runs the finalize () methods of any objects pending finalization i.e. objects which have been discarded but there finalize method is yet to be run. After finalize method is executed JVM reclaims space from all the discarded objects in java. 

                 Note: Calling these methods does not guaranteethat it will immediately start performing garbage collection.

    • Finalize method execution is not assured – We must not override finalize method to write some critical codeof application because methods execution is not assured. Writing some critical code in finalize method and relying on it may make application to go horribly wrong in java.
    • Dealing with OutOfMemoryError in java.
    • WeakhashMap in java – java.util.WeakHashMap is hash table based implementation of the Map interface, with weak keys. An entry in a WeakHashMap will be automatically removedby garbage collector when its key is no longer in ordinary use.
    • Object which is set explicitly set to null becomes eligible for gc (garbage collection) in java.
      Example 

       String s=”abc”; //s is currently not eligible for gc (garbage collection) in java.
       s = null; //Now, s is currently eligible for gc (garbage collection) in java.

        Example                    

       List list =new ArrayList(); //list is currently not eligible for gc (garbage collection).
       list = null; //Now, list is currently eligible for gc (garbage collection).
    • Difference in garbage collection in C/C++ and Java (Hint : In terms of memory allocation and deallocation of objects)?
      • In java garbage collection (memory allocation and deallocation of objects) is an automaticprocess.
      • But, In C and C++ memory allocation and deallocation of objects) is a manual process.
    • All the variables declared inside block becomes eligible for gc (garbage collection) when we exit that block (As scope of those variable is only that block) in java.
      Example of garbage collection while using block in java

      package com.emexo.gc;
      
      class MyClass {
          public static void main(String[] args) {
              boolean var = false;
              if (var) { // begin block 1
                  int x = 1; // x is declared inside block
                  //..........
                  //code inside block...
                  //..........
              } // end block 1 //And now x is eligible for gc (garbage collection)
              else { // begin block 2
                  int y = 1;
                  //..........
                  //code inside block...
                  //..........
              } // end block 2 //And now y is eligible for gc (garbage collection)
          }
      }

Terms frequently used in Garbage Collection (GC) in java

  • What is Throughput in gc(garbage collection) in java ?
    • In short, Throughput is the time not spent in garbage collection (GC) ( in percent).
    • Throughput focuses on maximizing the amount of work by an application in a specific period of time. 
    • Examples of how throughput might be measured include
    • The number of transactions completed in a given time.
    • The number of jobs that a batch program can complete in an hour.
    • The number of database queries that can be completed in an hour.
  • What are pauses in gc(garbage collection) in java ?
    • Pauses is applications pauses i.e. when application doesn’t gives any response because of garbage collection (GC)

JVM Heap memory with diagram in java

Garbage Collection

  • JVM Heap memory (Hotspot heap structure)  in java consists of following elements
    • Young Generation
      • Eden
      • S0 (Survivor space 0)
      • S1 (Survivor space 1)
    • Old Generation (Tenured)
    • Permanent Generation
    • JVM Heap memory (Hotspot heap structure) is divided into three parts Young Generation, Old Generation (tenured) and Permanent Generation.
    • Young Generation is further divided into Eden, S0 (Survivor space 0) and S1 (Survivor space 1)

Garbage Collection(Minor and major garbage collection) in JVM Heap memory

     1. Young Generation (Minor garbage collection occurs in Young Generation)

  • New objects are allocated in Young generation.
  • Minor garbage collection occurs in Young Generation.
  • When minor garbage collection?
    • When the young generation fills up, this causes a minor garbage collection.
    • All the unreferenced (dead) objects are cleaned up from young generation.
  • When objects are moved from young to old generation in JVM heap?
    • Some of the objects which aren’t cleaned up survive in young generation and gets aged.  Eventually such objects are moved from young to old generation.
  • What is Stop the World Event?
    • Minor garbage collections are called Stop the World events.
    • All the non-daemon threads running in application are stopped during minor garbage collections (i.e. the application stops for while).
    • Daemon threads performs minor garbage collection. (Daemon threads are low priority threads which runs intermittently in background for doing garbage collection).

     2. Old Generation or (tenured generation) – (Major garbage collection occurs in Old Generation)

  • All Java objects are always created on heap in java.
  • What is GC (Garbage collection) process in java?
  • The Old Generation is used for storing the long surviving aged objects (Some of the objects which aren’t cleaned up survive in young generation and gets aged.  Eventually such objects are moved from young to old generation).
  • Major garbage collection occurs in Old Generation.
  • At what time (or what age) objects are moved from young to old generation in JVM heap?
    • There is some threshold set for young generation object and when that age is met, the object gets moved to the old generation during gc(garbage collection) in java.
  • What is major garbage collection in java?
    • When the old generation fills up, this causes a major garbage collection. Objects are cleaned up from old generation.
    • Major collection is much slower than minor garbage collection in jvm heap because it involves all live objects.
  • Major gc(garbage collections) in responsive applications in java?
    • Major garbage collections should be minimized for responsive applications because applications must not be stopped for long.
  • Optimizing Major gc(garbage collections) in responsive applications in java?
    • Selection of appropriate garbage collector for the old generation space affects the length of the “Stop the World” event for a major garbage collection.

     3. Permanent Generation or (Permgen) – (full garbage collection occurs in permanent generation in java)

  • Permanent generation Space contains metadata required by JVM to describe the classes and methods used in the application.
  • The permanent generation is included in a full garbage collection in java.
  • The permanent generation space is populated at runtime by JVM based on classes in use in the application.
  • The permanent generation space also contains Java SE library classes and methods in java.
  • JVM garbage collects those classes when classes are no longer required and space may be needed for other classes in java.

Most important VM (JVM) Parameters in JVM Heap memory

  • -Xms : Xms is minimum heap size which is allocated at initialization of JVM.
    • Example: java -Xms512m MyJavaProgram
    • It will set the minimum heap size of JVM to 512 megabytes.
  • -Xmx : Xmx is the maximum heap size that JVM can use.
    • Example: java -Xmx512m MyJavaProgram
    • It will set the maximum heap size of JVM to 512 megabytes.

1. Young Generation(VM Parameters for Young Generation)

  • -Xmn : -Xmn sets the size of young generation.
    java -Xmn512m MyJavaProgram
  • -XX:NewRatio : NewRatio controls the size of young generation
    • -XX:NewRatio=3 means that the ratio between the young and old/tenured generation is 1:3.
    • In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.
  • -XX:NewSize – NewSize is minimum size of young generation which is allocated at initialization of JVM
    • Note : If you have specified -XX:NewRatio than minimum size of the young generation is allocated automatically at initialization of JVM.
  • -XX:MaxNewSize – MaxNewSize is the maximum size of young generation that JVM can use.
  • -XX:SurvivorRatio :   (for survivor space)
    • SurvivorRatio can be used to tune the size of the survivor spaces, but this is often not as important for performance

2.  Old Generation (tenured) – (VM Parameters for Old Generation)

  • -XX:NewRatio : NewRatio controls the size of young and old generation.

3.  Old Generation (tenured) – (VM Parameters for Old Generation)

  • -XX:PermSize: It’s is initial value of Permanent Space which is allocated at startup of JVM.

Different Garbage collectors in detail

1.  Serial collector / Serial GC

  • Serial collector is also called Serial GC (Garbage collector) in java.
  • Serial GC (Garbage collector) is rarely used in java
  • Serial GC (Garbage collector) is designed for the single threaded environments in java.
  • In Serial GC (Garbage collector) , both minor and major garbage collections are done serially by one thread (using a single virtual CPU) in java.
  • Serial GC (Garbage collector) uses a mark-compact collection method. This method moves older memory to the beginning of the heap so that new memory allocations are made into a single continuous chunk of memory at the end of the heap. This compacting of memory makes it faster to allocate new chunks of memory to the heap in java.
  • The serial garbage collector is the default for client style machines in Java SE 5 and 6.
  • When to Use the Serial GC (garbage Collector) in java ?
    • The Serial GC is the garbage collector of choice for most applications that do not have low pause time requirements and run on client-style machines. It takes advantage of only a single virtual processor for garbage collection work in java.
    • Serial GC (garbage collector) is also popular in environments where a high number of JVMs are run on the same machine. In such environments when a JVM does a garbage collection it is better to use only one processor to minimize the interference on the remaining JVMs in java.
  • Vm (JVM) option for enabling serial GC (garbage Collector) in java?
    • -XX:+UseSerialGC
    • Example of Passing Serial GC in Command Line for starting jar
    • java -Xms256m -Xmx512m  -XX:+UseSerialGC -jar d:\MyJar.jar

2.  Throughput GC (Garbage collector) or Parallel collector in java

  • Throughput garbage collector is the default garbage collector for JVM in java.
  • Throughput garbage collector uses multiple threads to execute a minor collection and so reduces the serial execution time of the application in java.
  • The throughput garbage collector is similar to the serial garbage collector but uses multiple threads to do the minor collection in java.
  • This garbage collector uses a parallel version of the young generation garbage collector in java.
  • The tenured generation collector is the same as the serial garbage collector in java.
  • When to Use the Throughput GC (Garbage collector) in java ?
    • The Throughput garbage collector should be used when application can afford low pauses in java.
    • And application is running on host with multiple CPU’s (to derive advantage of using multiple threads for garbage collection) in java.
  • Vm (JVM) option for enabling throughput GC (Garbage collector) in java?
    • -XX:+UseParallelGC
    • Example of using throughput collector in Command Line for starting jar
    • java -Xms256m -Xms512m  -XX:+UseParallelGC -jar d:\MyJar.jar
  • With this Vm (JVM) option you get a
    • Multi-threaded young generation garbage collector in java,
    • single-threaded old generation garbage collector in java and
    • Single-threaded compaction of old generation in java.
  • Vm (JVM) option for enabling throughput collector with n number of threads in java?
    • -XX:ParallelGCThreads=<numberOfThreads>

3.  Incremental low pause garbage collector (train low pause garbage collector) in java

  • Incremental low pause garbage collector is not used these days in java. Incremental low pause collector was used in Java 4.

4.  Concurrent Mark Sweep (CMS) Collector / concurrent low pause collector in java

  • Concurrent Mark Sweep (CMS) collector collects the old/tenured generation in java.
  • Concurrent Mark Sweep (CMS) Collector minimize the pauses by doing most of the garbage collection work concurrently with the application threads in java.
  • Concurrent Mark Sweep (CMS) Collector on live objects?
  • Concurrent Mark Sweep (CMS) Collector does not copy or compact the live objects. A garbage collection is done without moving the live objects. If fragmentation becomes a problem, allocate a larger heap in java.
  • When to Use the Concurrent Low Pause Collector in java?
    • Concurrent Low Pause Collector should be used if your applications that require low garbage collection pause times in java.
    • Concurrent Low Pause Collector should be used when your application can afford to share processor resources with the garbage collector while the application is running in java.
    • Concurrent Low Pause Collector is beneficial to applications which have a relatively large set of long-lived data (a large tenured generation) and run on machines with two or more processors in java
  • Examples when to use  Concurrent Mark Sweep (CMS) collector / concurrent low pause collector should be used for ?
    • Example 1 – Desktop UI application that respond to events,
    • Example 2 – Web server responding to a request and
    • Example 3 – Database responding to queries.
  • Vm (JVM) option for enabling Concurrent Mark Sweep (CMS) Collector in java?
    • -XX:+UseConcMarkSweepGC
    • Example of using Concurrent Mark Sweep (CMS) collector / concurrent low pause collector in Command Line for starting jar
    • java -Xms256m -Xms512m  -XX:+UseConcMarkSweepGC -jar d:\MyJar.jar
  • Heap Structure for CMS garbage Collector?
    • CMS garbage collectors divides heap into three sections: young generation, old generation, and permanent generation of a fixed memory size.
    • Young Generation is further divided into Eden, S0 (Survivor space 0) and S1 (Survivor space 1).

Detailed Steps in GC (garbage collection) cycle in Concurrent Mark Sweep (CMS) Collector / concurrent low pause garbage collector in java?

  • Young Generation GC (garbage Collection) in java
    • Live objects are copied from the Eden space and survivor space to the other survivor space.
    • Any older objects that have reached their aging threshold are promoted to old generation
  • After Young generation GC (garbage Collection) in java
    • After a young GC, the Eden space and one of the survivor spaces is cleared.
    • Promoted objects (older objects that have reached their aging threshold in young GC) are available in old generation.
  • Old Generation GC (garbage Collection) with CMS in java
    • Initial mark phase – (First pause happens/ stop the world event ) – mark live/reachable objects (Example – objects on thread stack, static objects etc.) and elsewhere in the heap (Example – the young generation).
    • Concurrent marking phase – (No pause phase ) –  finds live objects while the application continues to execute.
    • Remark – (Second pause happens/ stop the world events) – It finds objects that were missed during the concurrent marking phase due to the concurrent execution of the application threads.
    • Sweep phase – do the concurrent sweep, memory is freed up.
    • Objects that were not marked in the previous phase are deallocated in place.
    • There is no compaction
    • Unmarked objects are equal to Dead Objects.
    • Reset phase – do the concurrent reset.

5.  G1 Garbage Collector (or Garbage First) in java

  • G1 garbage collector was introduced in Java 7
  • G1 garbage collector was designed to replace CMS collector (Concurrent Mark-Sweep garbage Collector).
  • G1 garbage collector is parallel, G1 garbage collector is concurrent, and G1 garbage collector is incrementally compacting low-pause garbage collector in java.
  • G1 garbage collector has much better layout from the other garbage collectors like serial, throughput and CMS garbage collectors in java.
  • G1(Garbage First) collector compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions.
  • G1(Garbage First) collector allows customizations by allowing users to specify pause times.
  • G1 Garbage Collector (or Garbage First) limits GC pause times and maximizes throughput.
  • Vm (JVM) option for enabling G1 Garbage Collector (or Garbage First) in java?
    • -XX:+UseG1GC
    • Example of using G1 Garbage Collector in Command Line for starting jar
    • java -Xms256m -Xms512m  -XX:+UseG1GC -jar d:\MyJar.jar

  G1(Garbage First) collector functioning

  • CMS garbage collectors divides heap into three sections: young generation, old generation, and permanent generation of a fixed memory size.
  • All memory objects end up in one of these three sections.
  • The G1 collector takes a different approach than CMS garbage collector in partitioning java heap memory.
  • The heap is split/partitioned into many fixed sized regions (eden, survivor, old generation regions), but there is not a fixed size for them. This provides greater flexibility in memory usage.

   When to use G1 garbage collector?

  • G1 must be used when applications that require large heaps with limited GC latency.
  • Example – Application that require
    • heaps around 5-6GB or larger and
    • pause time required below 0.5 seconds

   The G1(Garbage First) collector working Step by Step

  • G1(Garbage First) garbage collector Heap Structure
    • The heap is split/partitioned into many fixed sized regions (eden, survivor, old generation regions), but there is not a fixed size for them. This provides greater flexibility in memory usage.
    • Each region’s size is chosen by JVM at startup.
    • Generally heap is divided into 2000 regions by JVM varying in size from 1 to 32Mb.
  • G1(Garbage First) garbage collector Heap Allocation
    • As mentioned above there are following region in heap Eden, survivor and old generation region. Also, Humongous and unused regions are there in heap.
  • Young Generation in G1 garbage collector
    • Generally heap is divided into 2000 regions by JVM.
    • Minimum size of region can be 1Mb and
    • Maximum size of region can be 32Mb.
    • Regions are not required to be contiguous like CMS garbage collector.
  • Young GC in G1 garbage collector
    • Live objects are copied or moved to survivor regions.
    • If objects aging threshold is met it get promoted to old generation regions.
    • It is STW (stop the world) event. Eden size and survivor size is calculated for the next young GC.
    • The young GC is done parallely using multiple threads.
  • End of a Young GC with G1 garbage collector
    • At this stage Live objects have been evacuated (copied or moved) to survivor regions or old generation regions.
  • Old Generation Collection with G1 garbage collector
    • G1 collector is low pause collector for old generation objects.
    • Initial Mark –
      • It is STW (stop the world) event.
      • With G1, it is piggybacked on a normal young GC. Mark survivor regions (root regions) which may have references to objects in old generation.
    • Root Region Scanning –
      • Scan survivor regions for references into the old generation.
      • This happens while the application continues to run. The phase must be completed before a young GC can occur.
    • Concurrent Marking –
      • Find live objects over the entire heap.
      • This happens while the application is running.
      • This phase can be interrupted by young generation garbage collections.
    • Remark (Stop the World Event) –
      • Completes the marking of live object in the heap.
      • Uses an algorithm called snapshot-at-the-beginning (SATB) which is much faster than algorithm used in the CMS collector.
    • Cleanup (Stop the World Event and Concurrent) –
      • Performs accounting on live objects and completely free regions. (Stop the world)
      • Young generation and old generation are reclaimed at the same time
      • Old generation regions are selected based on their liveness.
      • Scrubs the Remembered Sets. (Stop the world)
      • Reset the empty regions and return them to the free list. (Concurrent)