Java Programming - Garbage Collections - Discussion


What allows the programmer to destroy an object x?

[A]. x.delete()
[B]. x.finalize()
[C]. Runtime.getRuntime().gc()
[D]. Only the garbage collection system can destroy an object.

Answer: Option D


Option 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.

Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are:

  1. delete() - Method in class : Deletes the file or directory denoted by this abstract pathname.
  2. delete(int, int) - Method in class java.lang.StringBuffer : Removes the characters in a substring of this StringBuffer.
  3. delete(int, int) - Method in interface javax.accessibility.AccessibleEditableText : Deletes the text between two indices
  4. 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.

Option B is wrong. I found 19 finalize() methods. The most interesting, from this questions point of view, was the finalize() method in class java.lang.Object which is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method does not destroy the object to which it belongs.

Option C is wrong. But it is interesting. The Runtime class has many methods, two of which are:

  1. getRuntime() - Returns the runtime object associated with the current Java application.
  2. 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. Interesting as this is, it doesn't destroy the object.

Seshi said: (May 14, 2011)  
We can also destroy the objects by calling the finalize method.

Manish said: (Mar 2, 2015)  
Option B is correct as per the question, it is written that "programmer to destroy an object x". It is not stated that whether the object is created by new keyword or without new keyword.

If the objects are created without new keyword then garbage collector will not collect that object. In order to remove that object from memory the programmer have to invoke finalize() method.

Charudatta said: (Aug 25, 2015)  
Option B is not correct, because finalize() is only called just prior to garbage collection. It used to releasing system resources before the object destroy. finalize() is not use to destroy the specified object.

Debdeep said: (Feb 8, 2018)  

Refer this example.

class MyObject

public void finalize() throws Throwable

public class ProgramQ

public static void main(final String[] args) throws Throwable

MyObject my = new MyObject();

my = null;


Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.