finalize method is not working

class Abc

{

protectedvoid finalize()throws Throwable

{

System.out.println("Inside finalize");

super.finalize();

}

}

publicclass Finalize1

{

publicstaticvoid main(String args[])

{

Abc a=new Abc();

System.gc();

}

}

"Inside finalize" inside the finalize method is not getting printed.When the code ends won't the objects get garbage collected when the program ends.So shouldn't that statement get printed?

[1075 byte] By [psychica] at [2007-11-26 18:11:52]
# 1
What will happen with this a=null
qUesT_foR_knOwLeDgea at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 2
http://www.google.com/search?hl=en&q=Java+finalizers+not+guaranteedNote the words in the above, and pay attention:Java finalizers not guaranteed
warnerjaa at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 3
a=null will work
psychica at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 4

Try

public static void main(String[] args)

{

Abc a = new Abc();

a = null;

System.gc();

}

duckbilla at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 5
So a=null means till it has a null reference the object instance is not ready to be garbage collected?
psychica at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 6

> a=null will work

As the API for System.gc() states:

Calling the gc 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 Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Note the language: "suggests", "a best effort". This is not the same as a guarantee that the object will be collected and its finalize run.

DrLaszloJamfa at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 7

> So a=null means till it has a null reference the

> object instance is not ready to be garbage collected?

Nothing can be GCed until it is unreachable. If your reference "a" is not null (god, I hate one-letter variable names) then the object it points to is reachable.

The first half of this article explains it.

http://java.sun.com/developer/technicalArticles/ALT/RefObj/

jverda at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 8
G-d, I hate vowels.
DrLaszloJamfa at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 9
> G-d, I hate vowels.That's a good way to save $250 on WoF. ~
yawmarka at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 10
> When the code ends won't the objects get garbage collectedProbably not. Why spend a lot of time tidying up the memory when you are only going to give it back to the operating system anyway?
DrClapa at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 11

> Nothing can be GCed until it is unreachable.

Note that I'm not contradicting what the good DrLJ is saying. He's also correct.

So...

* Initially, your object could not possibly be GCed because it was reachable through the "a" reference.

* Even after setting a to null, thereby making the object unreachable, there's no guarantee it will ever be GCed with the code you have. You could create a crapload of objects and stuff them into a List until you get OutOfMemoryError. GC will run before that error occurs.

jverda at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...
# 12
OP: are you just wondering about the finalize method, or is there some code you are counting on being run?
DrLaszloJamfa at 2007-7-9 5:44:35 > top of Java-index,Java Essentials,New To Java...