Over a million developers have joined DZone.

Potential Java Garbage Collection Interview Questions

DZone's Guide to

Potential Java Garbage Collection Interview Questions

What kind of interview questions can you expect about how Java uses memory?

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.


Fig: Java Heap sizes generated from http://gceasy.io

What are the Different Regions in JVM Memory?

There are 5 regions:

  1. Eden
  2. Survivor 1
  3. Survivor 2
  4. Old (or Tenured) Generation
  5. Perm Generation (until Java 7). Since Java 8, Perm Generation has been replaced with Metaspace.

Note: Eden, Survivor 1 and Survivor 2 are collectively called the Young Generation.

Can You Explain the Purpose of Each Region in Java Memory?

Eden Generation: When an object is newly constructed it’s created in the Young Generation. In most of the applications, most of the objects are short-lived objects. i.e. they will die soon. Thus they will be collected as garbage within the Young Generation itself.

Survivor: Objects that survive Minor GC are not directly promoted to the Old Generation. They are kept in the Survivor region for a certain number of Minor GC collections. Only if they survive certain number of Minor GC collections can they be promoted to the Old Generation.

Old (or Tenured) Generation: Certain objects tend to be long-lived. Example: Application Context, HTTP Sessions, Caches, Connection Pools, etc. Those long-lived objects are promoted to old generation.

Perm Generation: This is the location where JVM objects such as Classes, Methods, String Interns, etc. are created.

Metaspace: Starting with Java 8, the perm generation has been replaced with Metaspace for performance reasons.

What are the Different Types of GC?

There are 3 types of garbage collection:

  1. Minor GC
  2. Major GC
  3. Full GC

Minor GC: It’s also called as Scavenge GC. This is the GC which collects garbage from the Young Generation.

Major GC: This GC collects garbage from the Old Generation

Full GC: This GC collects garbage from all regions i.e. Young, Old, Perm, Metaspace.

When Major or Full GC run all application threads are paused. It’s called a stop-the-world event. In Minor GCs, stop-the-world events occurs, but only momentarily.

What are the Different Types of GC Algorithms?

  1. Serial
  2. Parallel
  3. CMS
  4. G1

Serial: The serial collector uses a single thread to perform all garbage collection work. It is best-suited to single processor machines, because it cannot take advantage of multiprocessor hardware. It’s enabled with the option -XX:+UseSerialGC.

Parallel: The parallel collector (also known as the throughput collector) performs minor collections in parallel, which can significantly reduce garbage collection overhead. It is intended for applications with medium-sized to large-sized data sets that are run on multiprocessors or multithreaded hardware. It’s enabled with the option -XX:+UseParallelGC.

CMS: The mostly concurrent collector performs most of its work concurrently (for example, while the application is still running) to keep garbage collection pauses short. It is designed for applications with medium-sized to large-sized data sets in which response time is more important than overall throughput because the techniques used to minimize pauses can reduce application performance. It’s enabled with the option -XX:+UseConcMarkSweepGC.

G1: G1 is the latest garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with high probability, while achieving high throughput. Whole-heap operations, such as global marking, are performed concurrently with the application threads. It’s enabled with the option -XX:+UseG1GC.

What are the Pros and Cons of Each GC Algorithm?

To see the Pros and Cons of each algorithm, refer to http://blog.tier1app.com/2015/04/19/which-gc-to-use/

What Tools are Used for Analyzing Garbage Collection Logs?

  1. http://gceasy.io
  2. IBM Pattern Modeling and Analysis Tool for Java Garbage Collector
  3. Oracle’s Visual GC, Java Mission Control

For more such interview questions please visit : http://gceasy.io

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

garbage colection ,java collection ,memory leaks ,tuning ,java ,memory

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}