Potential Java Garbage Collection Interview Questions
What kind of interview questions can you expect about how Java uses memory?
Join the DZone community and get the full member experience.
Join For FreeFig: Java Heap sizes generated from http://gceasy.io
What are the Different Regions in JVM Memory?
There are 5 regions:
- Eden
- Survivor 1
- Survivor 2
- Old (or Tenured) Generation
- 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:
- Minor GC
- Major GC
- 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?
- Serial
- Parallel
- CMS
- 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?
- http://gceasy.io
- IBM Pattern Modeling and Analysis Tool for Java Garbage Collector
- Oracle’s Visual GC, Java Mission Control
For more such interview questions please visit : http://gceasy.io
Opinions expressed by DZone contributors are their own.
Comments