0% found this document useful (0 votes)
21 views15 pages

GC Quiz

The document discusses garbage collection in Java and contains 23 multiple choice questions about topics like deprecated garbage collectors in Java 9, generational and non-generational garbage collectors, stop-the-world events, tuning JVM parameters like -Xms and -Xmx, and techniques for identifying memory leaks.

Uploaded by

mail.him.1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

GC Quiz

The document discusses garbage collection in Java and contains 23 multiple choice questions about topics like deprecated garbage collectors in Java 9, generational and non-generational garbage collectors, stop-the-world events, tuning JVM parameters like -Xms and -Xmx, and techniques for identifying memory leaks.

Uploaded by

mail.him.1994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Q1: Which of the following garbage collectors were deprecated in Java 9?

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

More about the answer:


Op�on B is correct. All references to the Bar object created on line 6 are destroyed when a new reference
to a new Bar object is assigned to the variable newBar on line 14. Therefore the Bar object, created on
line 6, is eligible for garbage collec�on a�er line 14.

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

More about the answer:


By the �me line 8 has run, the only object without a reference is the one generated as a result of line 6.
Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.
Q27: When is the Float object, created in line 3, eligible for garbage collec�on?

1 public Object m()


2 {
3 Object o = new Float(3.14F);
4 Object [] oa = new Object[l];
5 oa[0] = o; /* Line 5 */
6 o = null; /* Line 6 */
7 oa[0] = null; /* Line 7 */
8 return o; /* Line 8 */
9 }

o just a�er line 5


o just a�er line 6
o just a�er line 7
o just a�er line 8
Correct Op�on – C

More about the answer:


Op�on A is wrong. This simply copies the object reference into the array.
Op�on B is wrong. The reference o is set to null, but, oa[0] s�ll maintains the reference to the Float
object.
Op�on C is correct. The thread of execu�on will then not have access to the object.

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

More about the answer:


This is an example of the islands of isolated objects. By the �me line 11 has run, the objects instan�ated
in lines 6 and 7 are referring to each other, but no live thread can reach either of them.
Q29: What allows the programmer to destroy an object x?

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

More about the answer:


Op�on D is correct. When an object is no longer referenced, it may be reclaimed by the garbage collector.
If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a
last chance to clean up resources that would not otherwise be released. When a class is no longer
needed, it may be unloaded.
Op�on A is wrong. I found 4 delete() methods in all of the Java class structure. They are:
delete() - Method in class java.io.File : Deletes the file or directory denoted by this abstract pathname.
delete(int, int) - Method in class java.lang.StringBuffer : Removes the characters in a substring of this
StringBuffer.
delete(int, int) - Method in interface javax.accessibility.AccessibleEditableText : Deletes the text between
two indices
delete(int, int) - Method in class : javax.swing.text.JTextComponent.AccessibleJTextComponent; Deletes
the text between two indices
None of these destroy the object to which they belong.
Op�on B is wrong. I found 19 finalize() methods. The most interes�ng, from this ques�ons point of view,
was the finalize() method in class java.lang.Object which is called by the garbage collector on an object
when garbage collec�on determines that there are no more references to the object. This method does
not destroy the object to which it belongs.
Op�on C is wrong. But it is interes�ng. The Run�me class has many methods, two of which are:
getRun�me() - Returns the run�me object associated with the current Java applica�on.
gc() - Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort
toward recycling unused objects in order to make the memory they currently occupy available for quick
reuse. When control returns from the method call, the virtual machine has made its best effort to recycle
all discarded objects. Interes�ng as this is, it doesn't destroy the object.

Q28: A�er which line’s execu�on will the object created at line A become eligible for garbage collec�on?
Choose one.

1 abstract class Test {


2 JavaClass classToTest;
3 abstract public void doTest(JavaClass classToTest);
4 }
5 class UnitTest extends Test {
6 JavaClass classToUnitTest;
7 public UnitTest(JavaClass classToTest) {
8 this.classToTest = classToTest;
9 }
10 @Override
11 public void doTest(JavaClass classToTest) {
12 classToUnitTest = classToTest;
13 }
14 }
15 class JavaClass {}
16 JavaClass c1 = new JavaClass(); // line A
17 Test t1 = new UnitTest(c1);
18 t1.doTest(c1);
19 JavaClass c2 = c1;
20 c1 = new JavaClass();
21 c2 = null;
22 t1.doTest(null);
23 System.gc();
24 t1 = new UnitTest(c1);

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

More about the answer:


An object becomes eligible for garbage collec�on when it can’t be reached directly or indirectly from any
live thread. Threads can access objects star�ng from method-local reference variables at any point in the
ac�ve region of that thread’s stack or from any sta�c fields. In the ques�on’s sample code, only a single
user thread is shown, and there aren’t any sta�c fields. This means you need to be concerned only with
tracing reachability star�ng from method-local variables shown in the code.
Further, it’s important to dis�nguish an object eligible for garbage collec�on from garbage actually
collected.
The former can be evaluated accurately, but actual garbage collec�on is out of your control.
In fact, it’s possible that garbage collec�on could simply never recover any objects during the life of a
program.
How? That could happen if very litle memory is allocated so there’s never any pressure to recover
memory. It could also happen if the Epsilon GC is in use, which never reclaims anything. The Epsilon GC
might seem strange, but it can be used for evalua�ng the memory behavior of a program.
Before line 19, there are two local variables: c1 and t1. The JavaClass instance created at line A (let’s call
this the target object) is reachable directly from the c1 reference. It’s also reachable indirectly by
following the t1 reference to the UnitTest object, and from there through either of the two fields inside
that object. Both classToTest and classToUnitTest refer to that target object. (See Figure 1.)

Figure 1. The JavaClass and UnitTest instances before line 1

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

The target object is s�ll reachable, and so op�on D is incorrect.


Op�on E(Line 23) is a simple distrac�on. The call to System.gc() suggests that it might be beneficial if the
GC runs, but this call has no effect on what is eligible for collec�on. In fact, the call might not even cause
the GC to run. This method is considered a hint, but the actual decision about garbage is made by the
JVM—in fact, the hint can be en�rely ignored. This means op�on E must be incorrect.
A�er line 24, t1 has been reassigned, and because of this, there is no way to reach the target object from
the local variables that are in scope, as shown in Figure 4.
Figure 4. The instances a�er line 6
Even though the classToTest field in the original UnitTest instance refers to the target object, there’s no
way to reach that UnitTest object; consequently, there’s no way to reach the target object from any
reference available to the thread. Therefore, the target object is eligible for garbage collec�on. This tells
you that op�on F is correct.
Op�on G is incorrect. As discussed in rela�on to op�on E, you can never predict the execu�on of the GC
process. You can only know when an object becomes eligible for collec�on.
Conclusion: The correct op�on is F, because the JavaClass instance created at line A will be eligible for
garbage collec�on a�er line 6 has been completed.

Q29: Which statement is true?

o Programs will not run out of memory.


o Objects that are referred to by other objects will never be garbage collected.
o Objects that will never again be used are eligible for garbage collec�on.
o Objects that can be reached from a live thread will never be garbage collected.
Correct Op�on – D

More about the answer:


See further explana�ons.
Q30: Which of the below is not a memory leak solu�on?
o GC parameter tuning
o Code changes
o Process restart
o JVM 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.
Q31: What is memory leak?

More about the answer:


Memory leak is like holding a strong reference to an object although it would never be needed anymore.
Objects that are reachable but not live are considered memory leaks. Various tools help us to iden�fy
memory leaks.
Q32: Which statement is true?

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

More about the answer:


All objects are placed in the garbage collec�ble heap.
Op�on B is incorrect because finalize() has no such mys�cal powers.
Op�on C is incorrect because the garbage collector makes no guarantees.
Op�on D is incorrect because islands of isolated objects can exist.
Q33: How many objects are eligible for garbage collec�on a�er execu�on of line ?

public class Test


{
public sta�c void main(String[] args)
{
m1(); // Line
}
sta�c void m1()
{
Test t1 = new Test();
Test t2 = new Test();
}
}

o 0
o 1
o 2
o 3
Correct Op�on – D

More about the answer:


Since t1 and t2 are local objects of m1() method, so they become eligible for garbage collec�on a�er
complete execu�on of method unless any of them is returned.
Q34: Which statement is true?

o The garbage collector uses a mark and sweep algorithm.


o If object 1 refers to object 2, then object 2 can't be garbage collected.
o Calling Run�me.gc() will cause eligible objects to be garbage collected.
o If an object can be accessed from a live thread, it can't be garbage collected.
Correct Op�on – D

More about the answer:


This is a great way to think about when objects can be garbage collected.
Op�on A and B assume guarantees that the garbage collector never makes.
Op�on D is wrong because of the now famous islands of isola�on scenario.
Q35: In an opera�ng system, garbage collec�on is a(n) _____ management system.

o Process
o Memory
o Fragmenta�on
o Alloca�on
Correct Op�on – B

More about the answer:


Garbage collec�on in the opera�ng system is a dynamic memory management process

You might also like