Consider the following Java code: public class MyClass { public static void main(String[] args) { Object obj1
Question:
Consider the following Java code:
public class MyClass {
public static void main(String[] args) {
Object obj1 = new Object();
Object obj2 = new Object();
obj1 = obj2;
obj2 = null;
System.gc();
System.out.println("Garbage Collection done");
}
}
Assuming that the code is executed using Java 8 or later, which of the following statements is true regarding the behavior of the garbage collector?
A. The garbage collector is guaranteed to run and collect the object referenced by obj1.
B. The garbage collector may or may not run, depending on the specific JVM implementation and the system's memory usage.
C. The garbage collector will not run because the objects obj1 and obj2 are still reachable from the main method's local variable table.
D. The code will not compile because the System.gc() method is not allowed in Java 8 or later.
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser