Complete the code to create a new object that will be eligible for garbage collection.
MyObject obj = new [1](); obj = null;new keyword.Creating an instance of MyObject and then setting the reference to null makes the object eligible for garbage collection.
Complete the code to explicitly request garbage collection.
System.[1]();exit which terminates the program.collectGarbage.The method System.gc() is used to suggest that the Java Virtual Machine performs garbage collection.
Fix the error in the code to avoid preventing garbage collection.
MyObject obj = new MyObject();
obj.[1] = obj;this which is a keyword, not a field.selfReference which is not a standard field name.Assigning obj.reference = obj; creates a self-reference which can prevent garbage collection if not handled properly.
Fill both blanks to create a weak reference that allows garbage collection.
WeakReference<MyObject> weakRef = new WeakReference<[1]>(obj); obj = [2];
Object instead of MyObject as generic type.obj to a new object instead of null.Using WeakReference allows the object to be garbage collected when no strong references exist. Setting obj = null; removes the strong reference.
Fill all three blanks to implement a finalize method that prints a message before garbage collection.
protected void [1]() throws [2] { System.out.[3]("Object is being garbage collected"); }
print instead of println.finalizeObject.The finalize() method is called by the garbage collector before reclaiming the object. It can throw Throwable. Using println prints the message with a new line.
