how to destroy an object in java?

168,526

Solution 1

Answer E is correct answer. If E is not there, you will soon run out of memory (or) No correct answer.

Object should be unreachable to be eligible for GC. JVM will do multiple scans and moving objects from one generation to another generation to determine the eligibility of GC and frees the memory when the objects are not reachable.

Solution 2

To clarify why the other answers can not work:

  1. System.gc() (along with Runtime.getRuntime().gc(), which does the exact same thing) hints that you want stuff destroyed. Vaguely. The JVM is free to ignore requests to run a GC cycle, if it doesn't see the need for one. Plus, unless you've nulled out all reachable references to the object, GC won't touch it anyway. So A and B are both disqualified.

  2. Runtime.getRuntime.gc() is bad grammar. getRuntime is a function, not a variable; you need parentheses after it to call it. So B is double-disqualified.

  3. Object has no delete method. So C is disqualified.

  4. While Object does have a finalize method, it doesn't destroy anything. Only the garbage collector can actually delete an object. (And in many cases, they technically don't even bother to do that; they just don't copy it when they do the others, so it gets left behind.) All finalize does is give an object a chance to clean up before the JVM discards it. What's more, you should never ever be calling finalize directly. (As finalize is protected, the JVM won't let you call it on an arbitrary object anyway.) So D is disqualified.

  5. Besides all that, object.doAnythingAtAllEvenCommitSuicide() requires that running code have a reference to object. That alone makes it "alive" and thus ineligible for garbage collection. So C and D are double-disqualified.

Solution 3

Set to null. Then there are no references anymore and the object will become eligible for Garbage Collection. GC will automatically remove the object from the heap.

Solution 4

Here is the code:

public static void main(String argso[]) {
int big_array[] = new int[100000];

// Do some computations with big_array and get a result. 
int result = compute(big_array);

// We no longer need big_array. It will get garbage collected when there
// are no more references to it. Since big_array is a local variable,
// it refers to the array until this method returns. But this method
// doesn't return. So we've got to explicitly get rid of the reference
// ourselves, so the garbage collector knows it can reclaim the array. 
big_array = null;

// Loop forever, handling the user's input
for(;;) handle_input(result);
}
Share:
168,526
nr5
Author by

nr5

Updated on July 09, 2022

Comments

  • nr5
    nr5 almost 2 years

    I encountered this question in an interview with following options:

    How to destroy an object in java?

    a. System.gc();  
    b. Runtime.getRuntime.gc();  
    c. object.delete();  
    d. object.finalize();  
    e. Java performs gc by itself, no need to do it manually.
    
    1. The answer should be e?

    2. what if e was not there? then ? clearly c is not the answer. a and b will do gc for the whole application(question requires for one object). I think it is d because finalize() is called just prior to gc(but is it necessary that after finalize gc is invoked ?) or I am wrong ? e must be there to answer this question ?

  • Sid
    Sid over 11 years
    That is correct. There's no correct answer but E. Else Java would become C++.
  • Admin
    Admin over 11 years
    This still isn't 100% correct, objects can live for the entire time of the application even though they are "not reachable", that doesn't have as much to do with their removal as does memory pressure. The JVM doesn't try to maintain max free space, that would be a waste of resources.
  • tired and bored dev
    tired and bored dev almost 6 years
    There should be a programming language where gems like this should be legal statements -> object.doAnythingAtAllEvenCommitSuicide()