how to destroy an object in java?
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:
System.gc()
(along withRuntime.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.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.Object
has nodelete
method. So C is disqualified.While
Object
does have afinalize
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.) Allfinalize
does is give an object a chance to clean up before the JVM discards it. What's more, you should never ever be callingfinalize
directly. (Asfinalize
is protected, the JVM won't let you call it on an arbitrary object anyway.) So D is disqualified.Besides all that,
object.doAnythingAtAllEvenCommitSuicide()
requires that running code have a reference toobject
. 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);
}
nr5
Updated on July 09, 2022Comments
-
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.
The answer should be e?
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 over 11 yearsThat is correct. There's no correct answer but E. Else Java would become C++.
-
Admin over 11 yearsThis 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 almost 6 yearsThere should be a programming language where gems like this should be legal statements -> object.doAnythingAtAllEvenCommitSuicide()