Java Garbage Collection Interview Questions

Explain garbage collection?

Or

How you can force the garbage collection?

Or

What is the purpose of garbage collection in Java, and when is it used?

Or

What is Garbage Collection and how to call it explicitly?

Or

Explain Garbage collection mechanism in Java?

Garbage collection is one of the most important features of Java. The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign null into a variable when no more in use. In Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. Garbage collection is an automatic process and can’t be forced. There is no guarantee that Garbage collection will start immediately upon request of System.gc().

What kind of thread is the Garbage collector thread?

It is a daemon thread.

Can an object’s finalize() method be invoked while it is reachable?

An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.

Does garbage collection guarantee that a program will not run out of memory?

Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

What is the purpose of finalization?

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup, before the object gets garbage collected. For example, closing an opened database Connection.

If an object is garbage collected, can it become reachable again?

Once an object is garbage collected, It can no longer become reachable again.

Can we force JVM to run garbage collection?

No. We cannot force instead we can request JVM to run a garbage collection. 

Explain which type of variables will go in heap and  which type in stack?

All the instance variables (references) and any objects (local or instance) will reside under heap and local variables (references) will go in stack.

Will Garbage Collector removes cyclic referenced objects?

Yes, cyclic referenced objects are eligible for garbage collection provided no external reference is there for both objects.

When does Garbage Collector run?

Garbage Collection is managed by Java Virtual Machine and is not in control of developer. When JVM finds low memory, it executes the Garbage Collection.

How we can request Garbage Collection request?

There are two ways to request Garbage Collector-

  1. Runtime.getRuntime().gc()
  2. System.gc()

When is an object  eligible for Garbage Collection?

Object is said to be eligible for Garbage Collection when there is no reference for the object or in other words no thread can access that variable.

What are the ways to make an object eligible for Garbage Collection?

There are different ways to make an object eligible for Garbage Collection. –

  1. Explicitly set the variable as null. For example  in below code object “p” is eligible for GC.

Person p = new Person();

p=null;

  1.  Local variables scope is till the method is executing. Once the control comes out of method, all local variables are eligible for GC without setting then to null(except the ones which has external references)
  2. Objects that are cyclic dependent and no external reference is there for these objects.

Explain finalize() method?

When an object is cleaned or removed by Garbage Collector, its finalize() methods is executed so we can say it is an opportunity for the developer to perform some leg work before object is removed from memory.

Explain high level Memory model of heap in JVM?

Heap is broadly divided into three parts –

  1. Young Generation 
  2. Old or Tenured Generation
  3. Permanent or Perm Generation.

Explain young generation of heap?

Young generation is divided in to three parts.

  1. Eden Space – Every object is created in Eden Space only.
  2. Survivor 1- When minor GC runs, the objects which survived the GC, moves to Survivor1 space.
  3. Survivor 2 – Objects which further survives are moved for Survivor 1 to Survivor 2.

What is old generation?

Objects which are old (survived certain rounds of GC) are moved from Survivor2 to old generation. GC runs in old generation is known as major GC and takes more time as old generation area is more.

What is Permanent Generation?

Permanent Generation is used to store the meta data of class, methods etc.

What is “stop the world” term is used for in Garbage Collection?

Major GC takes long and while major GC is running, application threads are paused and we can see application is un-responsive for that time. Due to this behaviour, it is called as “Stop the world” as everything gets stopped while major GC runs. Also it is important to note then if the frequency of major GC is high, application will become un-responsive.

Explain the difference between Stack and Heap memory?

The following are the differences between stack and heap memory-

  1. Heap memory is for complete application where as each thread has its own stack.
  2.  All the instance variables (references) and any objects (local or instance) will reside under heap and local variables (references) will go in stack.
  3. All threads can access heap as it is a shared memory but threads cannot access stack of another thread.
  4. Life of heap is from start to end of application where as stack lives till thread lives.
  5. When stack memory is full, it throws java.lang.StackOverFlowError error where as in case of heap is full, we get java.lang.OutOfMemoryError

Explain different switches to manage memory?

There are several switches to define the size or ratio of memory.

  1. –Xms – use this switch to define the initial size of heap memory.
  2. –Xmx - use this switch to define the maximum size of heap memory.
  3. –Xss- use this switch to define the size of stack.
  4. –Xmn- defines the size of young generation.
  5. -XX:PermGen – defines the size of permanent generation.
  6. -XX:MaxPermGen – used to define the maximum size of permanent generation.
  7. -XX:NewRatio – this switch is used to define the ratio between old and new generation.
  8. -XX:SurvivorRatio - this switch is used to define the ratio between eden and survivor space of young generation. In other words, it is used the manage the young generation space and specify how large the eden space would be.
  9. -XX:NewSize and -XX:MaxNewSize – is used to define the initial and maximum sixe of young generation.

What is the size of old vs. young generations if we define -XX:NewRatio=3?

Old generation will occupy 3/4th and young will occupy 1/4th of total heap. 

What is the size of eden vs. survivor space if we define -XX:SurvivorRatio=10?

Eden space will occupy 10/12th and each survivor will occupy 1/12th of total young generation. 

 

Like us on Facebook