Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize() method is called atmost once by GC

 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic