File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() method is called atmost once by GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize() method is called atmost once by GC " Watch "finalize() method is called atmost once by GC " New topic
Author

finalize() method is called atmost once by GC

Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

This statement in subject line states that GC can call finalize method either 0 or 1 times, Under what conditions does GC does not call finalize method ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

as you know finalize method is called immediately before the object is to garbage collected by the JVM. however rather than writing code to do something useful before the object is cleaned, it is possible you write code that makes object UNELIGIBLE for the garbage collector. for e.g. you could do like this


in this case object becomes uneligible for garbage collection. now the second time when this object becomes eligible for garbage collection, jvm will know that its finalize method has been called once and won't call the finalize method again and garbage collect the object


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

I got your point and in the example given abov the GC called finalize() once already and its going to ignore second time.

BUT my question is ,when do GC calls zero times(ignores calling finalize() first time itself) , justifying zero part of the statement " at most once(0 or 1) " , the above example is case of calling once.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

I may be wrong but garbage collection takes no guarantee that it would run and would garbage collect a object which is not accessible by any live thread. it does guarantee that before giving memory error , it will try hard to reclaim the memory. so it is possible that a object becomes available for garbage collection but the garbage collector itself does not run for that object, which means no finalize() would be called.
Abhilash Sharma
Ranch Hand

Joined: Dec 05, 2010
Posts: 67

When it comes to GC their is no guarantee.It may or may not run.
Consider the following-:
Object ob = new Object(); //1
Now this object has public void finalize() for sure.

Now,there 3 kind of possibilities-

1-GC run for this object which is possible if there are no active references to reach object ob which can be done by ob=null or assign ob to some another object say
ob = new Object();
Now if GC runs for the object at //1 it reclaim the memory of this object.
GC not gonna run again for this object,finalize() only/at most once per object.

This case justifies GC calls 1 time.

2-GC run for this object which inturn runs it's finalize method and if you resurrect the object in finalize method lik
ob=null;
public void finalize()
{
ob=this;
}
In this case,the object has again a valid reference and memory will not collected.

This also,justifies GC runs 1 time but nor collect memory because of resurrection.

3-GC never runs for this object

This justifies GC runs 0 times.

The point is if GC runs for a particular object whatever happen for that object it's never gonna run again.



OCPJP 6 | OCEJWCD 6
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

Anishashu Sharma wrote:When it comes to GC their is no guarantee.It may or may not run.
Consider the following-:
Object ob = new Object(); //1
Now this object has public void finalize() for sure.

Now,there 3 kind of possibilities-

1-GC run for this object which is possible if there are no active references to reach object ob which can be done by ob=null or assign ob to some another object say
ob = new Object();
Now if GC runs for the object at //1 it reclaim the memory of this object.
GC not gonna run again for this object,finalize() only/at most once per object.

This case justifies GC calls 1 time.

2-GC run for this object which inturn runs it's finalize method and if you resurrect the object in finalize method lik
ob=null;
public void finalize()
{
ob=this;
}
In this case,the object has again a valid reference and memory will not collected.

This also,justifies GC runs 1 time but nor collect memory because of resurrection.

3-GC never runs for this object

This justifies GC runs 0 times.

The point is if GC runs for a particular object whatever happen for that object it's never gonna run again.




the correct definition of finalize method is protected void finalize() and NOT public void finalize() . secondly finalize method is inherited by every class so every class has that method (you wrote that this object will have finalize for sure)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize() method is called atmost once by GC
 
Similar Threads
Is Garbage collection always called at end of main ?
finalize
finalize method
finalize() question
finalize() method.