GC Quiz
GC Quiz
o Epsilon
o Garbage First (G1)
o Z Garbage Collector (ZGC)
o Concurrent Mark Sweep (CMS)
Correct Op�on - D
More about the answer:
While CMS is effec�ve at memory management on JVMs with less than 32 GB of memory, G1 is the
recommended garbage collector for enterprise applica�ons hosted on Java 8 or Java 11 long-term support
releases.
Q2: Which of the following garbage collectors are not mul�-genera�onal?
o Java 11's Epsilon GC
o G1 in Java 11
o ZGC in Java 15
o CMS in Java 8
Correct Op�on - C
More about the answer:
ZGC boasts many improvements over CMS and G1, especially when large Java heaps are available. But the
current implementa�on is not genera�onal. That will likely change in future releases.
Q3: A JVM event like garbage collec�on or a Java heap dump that stops all non-essen�al threads from
running and makes applica�ons appear unresponsive is known as:
o A stop-the-presses event
o A stop-the-JVM event
o A stop-the-world event
o A stop-the-count event
Correct Op�on - C
More about the answer:
Monitoring stop-the-world events in profiling tools such as JDK Mission Control and Java Flight Recorder
is an important part of monitoring JVM performance. Minimizing the dura�on of a Java GC stop-the-world
event is a prime JVM tuning objec�ve.
Q4: The asser�on that most objects allocated by a computer program will be short-lived is known as the:
o Strong genera�onal hypothesis
o Weak genera�onal hypothesis
o Genera�onal garbage collec�on hypothesis
o Garbage collec�on hypothesis
Correct Op�on - B
More about the answer:
The weak genera�onal hypothesis asserts that most objects will be short-lived. This is true not only for
Java-based systems, but it is a theory that applies throughout computer science.
Q5: The serial garbage collector is most applicable to:
o Enterprise servers with less than 32 GB of RAM
o Enterprise servers with more than 32 GB of RAM
o Servers where GC must be fast, and pauses are not an issue
o Home PCs running Minecra� or simple programs
Correct Op�on - D
More about the answer:
The serial garbage collector does not run threads in parallel. It also does not run mark-and-sweep rou�nes
concurrently with other applica�ons hosted on the JVM. It's not designed for enterprise systems, but it
works well for Java-based applica�ons that run on a home computer.
Q6: Which of the following garbage collectors is best suited to stateless Java apps that do not allocate
objects?
o Epsilon
o G1
o ZGC
o CMS
Correct Op�on - A
More about the answer:
Also known as the "zero-garbage collector" or the "null-garbage collector," Epsilon GC does not perform
garbage collec�on at all. It is ideal for stateless applica�ons that do not perform any object alloca�ons
a�er the JVM has started.
Q7: What is the easiest way to immediately force Java garbage collec�on?
o Call System.gc() in code
o Invoke a Java heap dump
o Use the Java diagnos�c command and run jcmd GC.run
o You cannot force Java garbage collec�on
Correct Op�on - D
More about the answer:
The first three op�ons are valid ways to suggest the JVM perform a GC rou�ne, but garbage collec�on is
non-determinis�c. You can't force Java GC; you can only ask the JVM to priori�ze it. There is no guarantee
the process will actually run.
Q8: The CMS garbage collector is best suited for:
o Enterprise servers with more than 32 GB of RAM
o Enterprise servers with less than 32 GB of RAM
o Servers where GC must be fast, and pauses are not an issue
o Home PCs running Minecra� or simple programs
Correct Op�on - B
More about the answer:
CMS compacts heaps under 32 GB, but does not apply the same process for heaps over 32 GB in size. As a
result, CMS runs most efficiently with JVMs assigned 32 GB of memory or less. For larger heap sizes,
choose ZGC or G1.
Q9: Where on the Java heap does ZGC place newly allocated objects?
o The Eden space
o The Survivor space
o The Tenures space
o The Permanent space
Correct Op�on - A
More about the answer:
See ques�on no 13 for explaina�on.
Q10: In Java 8, heap-based PermGen was replaced with:
o Na�ve memory-based PermGen
o Java heap-based Metaspace
o Java heap-based MetaGen
o Na�ve memory-based Metaspace
Correct Op�on - D
More about the answer:
OutOfMemoryErrors due to limited PermGen space on the heap are a thing of the past, as objects
previously assigned to that space are now stored in Metaspace, which uses na�ve memory instead.
Q11: Which func�on is used to perform some ac�on when the object is to be destroyed?
o System.gc()
o Finalize()
o Clean()
o Sweep()
Correct Op�on - B
Q12: Which of the following has highest memory requirement?
o Class
o JVM
o Stack
o Heap
Correct Op�on - B
More about the answer:
JVM is the super set which contains heap, stack, objects, pointers, etc.
Q13: Where is a new object alloted memory?
o Young space
o Old space
o Young or Old space depending on space availability
o JVM
Correct Op�on - A
More about the answer:
A new object is always created in young space. Once young space is full, a special young collec�on is run
where objects which have lived long enough are moved to old space and memory is freed up in young
space for new objects.
Q14: Which of the following is a garbage collec�on technique?
o Cleanup model
o Mark and sweep model
o Space management model
o Sweep model
Correct Op�on – B
More about the answer:
A mark and sweep garbage collec�on consists of two phases, the mark phase and the sweep phase. In
mark phase all the objects reachable by java threads, na�ve handles and other root sources are marked
alive and others are garbage. In sweep phase, the heap is traversed to find gaps between live objects and
the gaps are marked free list used for alloca�ng memory to new objects.
Q15: What is -Xms and -Xmx while star�ng jvm?
o Ini�al; Maximum memory
o Maximum; Ini�al memory
o Maximum memory
o Ini�al memory
Correct Op�on - A
More about the answer:
JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of
memory. java -Xmx2048m -Xms256m.
Q16: Which excep�on is thrown when java is out of memory?
o MemoryFullExcep�on
o MemoryOutOfBoundsExcep�on
o OutOfMemoryError
o MemoryError
Correct Op�on - C
More about the answer:
The Xms flag has no default value, and Xmx typically has a default value of 256MB. A common use for
these flags is when you encounter a java.lang.OutOfMemoryError.
Q17: How to get prints of shared object memory maps or heap memory maps for a given process?
o Jmap
o Memorymap
o Memorypath
o jvmmap
Correct Op�on - A
More about the answer:
We can use jmap as jmap -J-d64 -heap pid.
Q18: What happens to the thread when garbage collec�on kicks off?
o The thread con�nues its opera�on
o Garbage collec�on cannot happen un�l the thread is running
o The thread is paused while garbage collec�on runs
o The thread and garbage collec�on do not interfere with each other
Correct Op�on - C
More about the answer:
The thread is paused when garbage collec�on runs which slows the applica�on performance.
Q19: Which of the below is not a Java Profiler?
o JConsole
o Eclipse Profiler
o JProfiler
o JVM
Correct Op�on – D
Q20: Which of the below is not a memory leak solu�on?
o Code changes
o JVM parameter tuning
o Process restart
o GC parameter tuning
Correct Op�on - C
More about the answer:
Process restart is not a permanent fix to memory leak problem. The problem will resurge again.
Q21: Garbage Collec�on can be controlled by program?
o True
o False
Correct Op�on - False
More about the answer:
Garbage Collec�on cannot be controlled by a program.
Q22: Which statement is true?
o Memory is reclaimed by calling Run�me.gc().
o Objects are not collected if they are accessible from live threads.
o An OutOfMemory error is only thrown if a single block of memory cannot be found that is large
enough for a par�cular requirement.
o Objects that have finalize() methods always have their finalize() methods called before the program
ends.
Correct Op�on - B
More about the answer:
If an object can be accessed from a live thread, it can’t be garbage collected.
Op�on A is wrong. Run�me.gc() asks the garbage collector to run, but the garbage collector never makes
any guarantees about when it will run or what unreachable objects it will free from memory.
Op�on C is wrong. The garbage collector runs immediately the system is out of memory before
an OutOfMemoryExcep�on is thrown by the JVM.
Op�on D is wrong. If this were the case then the garbage collector would ac�vely hang onto objects un�l
a program finishes – this goes against the purpose of the garbage collector.
Q23: Where will be the most chance of the garbage collector being invoked?
1 class HappyGarbage01
2 {
3 public sta�c void main(String args[])
4 {
5 HappyGarbage01 h = new HappyGarbage01();
6 h.methodA(); /* Line 6 */
7 }
8 Object methodA()
9 {
10 Object obj1 = new Object();
11 Object [] obj2 = new Object[1];
12 obj2[0] = obj1;
13 obj1 = null;
14 return obj2[0];
15 }
16 }
o A�er line 9
o A�er line 10
o A�er line 11
o Garbage collector never invoked in methodA()
Correct Op�on – D
More about the answer:
Op�on D is correct. Garbage collec�on takes place a�er the method has returned its reference to the
object. The method returns to line 6, there is no reference to store the return value. so garbage collec�on
takes place a�er line 6.
Op�on A is wrong. Because the reference to obj1 is stored in obj2[0]. The Object obj1 s�ll exists on the
heap and can be accessed by an ac�ve thread through the reference stored in obj2[0].
Op�on B is wrong. Because it is only one of the references to the object obj1, the other reference is
maintained in obj2[0].
Op�on C is wrong. The garbage collector will not be called here because a reference to the object is being
maintained and returned in obj2[0].
Q24: At what point is the Bar object, created on line 6, eligible for garbage collec�on?
1 class Bar { }
2 class Test
3 {
4 Bar doBar()
5 {
6 Bar b = new Bar(); /* Line 6 */
7 return b; /* Line 7 */
8 }
9 public sta�c void main (String args[])
10 {
11 Test t = new Test(); /* Line 11 */
12 Bar newBar = t.doBar(); /* Line 12 */
13 System.out.println("newBar");
14 newBar = new Bar(); /* Line 14 */
15 System.out.println("finishing"); /* Line 15 */
16 }
17 }
o a�er line 12
o a�er line 14
o a�er line 7, when doBar() completes
o a�er line 15, when main() completes
Correct Op�on – B
Op�on A is wrong. This actually protects the object from garbage collec�on.
Op�on C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in
newBar on line 12. This preserver the object created on line 6.
Op�on D is wrong. Not applicable because the object is eligible for garbage collec�on a�er line 14.
Q25: When is the Demo object eligible for garbage collec�on?
1 class Test
2 {
3 private Demo d;
4 void start()
5 {
6 d = new Demo();
7 this.takeDemo(d); /* Line 7 */
8 } /* Line 8 */
9 void takeDemo(Demo demo)
10 {
11 demo = null;
12 demo = new Demo();
13 }
14 }
o A�er line 7
o A�er line 8
o A�er the start() method completes
o When the instance running this code is made eligible for garbage collec�on.
Correct Op�on – B
More about the answer:
Op�on D is correct. By a process of elimina�on.
Op�on A is wrong. The variable d is a member of the Test class and is never directly set to null.
Op�on B is wrong. A copy of the variable d is set to null and not the actual variable d.
Op�on C is wrong. The variable d exists outside the start() method (it is a class member). So, when the
start() method finishes the variable d s�ll holds a reference.
Q26: A�er line 8 runs. how many objects are eligible for garbage collec�on?
1 public class X
2 {
3 public sta�c void main(String [] args)
4 {
5 X x = new X();
6 X x2 = m1(x); /* Line 6 */
7 X x4 = new X();
8 x2 = x4; /* Line 8 */
9 doComplexStuff();
10 }
11 sta�c X m1(X mx)
12 {
13 mx = new X();
14 return mx;
15 }
16 }
o 0
o 1
o 2
o 3
Correct Op�on – B
Q28: A�er line 11 runs, how many objects are eligible for garbage collec�on?
1 class X2
2 {
3 public X2 x;
4 public sta�c void main(String [] args)
5 {
6 X2 x2 = new X2(); /* Line 6 */
7 X2 x3 = new X2(); /* Line 7 */
8 x2.x = x3;
9 x3.x = x2;
10 x2 = new X2();
11 x3 = x2; /* Line 11 */
12 doComplexStuff();
13 }
14 }
o 0
o 1
o 2
o 3
Correct Op�on – C
o x.delete()
o x.finalize()
o Run�me.getRun�me().gc()
o Only the garbage collec�on system can destroy an object.
Correct Op�on – D
Q28: A�er which line’s execu�on will the object created at line A become eligible for garbage collec�on?
Choose one.
o Line 19
o Line20
o Line 21
o Line 22
o Line 23
o Line 24
o Not possible to predict, because it depends on the garbage collector (GC) implementa�on
Correct Op�on – Line 24
With three paths to reach the target object, clearly it cannot be collected as garbage.
Line 19 creates another local variable, c2, and assigns that to refer to the target object. There are now
four ways to reach the target object, so clearly it cannot be collected as garbage at this point, and thus
op�on A is incorrect.
Line 20 overwrites c1 with a reference to a new instance of JavaClass. The target object is s�ll reachable
via t1 (which contains two links to the object) and c2. The situa�on now looks like Figure 2.
Figure 2.The instances a�er line 2
Line 21 overwrites the c2 variable with null, but the target object is s�ll reachable through the two fields
of t1. So, op�on C is incorrect.
At line 22, the field t1.classToUnitTest is overwriten with a null pointer, but the other field, t1.classToTest,
s�ll maintains a reference, as seen in Figure 3. Therefore, the target object is s�ll reachable and cannot be
garbage collected.
Figure 3. The instances a�er line 4
o All objects that are eligible for garbage collec�on will be garbage collected by the garbage collector.
o Objects from a class with the finalize() method overridden will never be garbage collected.
o Objects with at least one reference will never be garbage collected.
o Objects instan�ated within anonymous inner classes are placed in the garbage collec�ble heap.
Correct Op�on – A
o 0
o 1
o 2
o 3
Correct Op�on – D
o Process
o Memory
o Fragmenta�on
o Alloca�on
Correct Op�on – B