• Post Reply Bookmark Topic Watch Topic
  • New Topic

How finalize() method works in garbage collection?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know finalize() is only run when the object is eligible for garbage collection, but I really don't understand the concept that finalize() could only run zero or one time. And with the static variable referring to the object, the object might never be eligible for garbage collection but what if at some point it is set to null?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The finalize() method is *not* run as soon as an object is eligible for GC. The method is called during GC.... basically, during GC, if an object is deemed unreachable, and the finalize() method has not been called previously, then it will be called. After the method is called, it is checked again for reachability.

Henry
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivanshu Dhawan wrote:Can you please explain me with some sample code the use of static variable in GC?


Variables are not involved in garbage-collection. Only objects are subject to being garbage collected.

And an object can be garbage collected if there are no variables in scope anywhere which contain references to the object. It doesn't matter whether those variables are static variables, or instance variables, or even local variables.

Earlier you said "what if at some point it is set to null?" apparently referring to a static variable. Yes, if you set a static variable (or any other kind of variable) to null, and it wasn't null before, then it will cease to refer to the object which it formerly referred to. This may make that object available to be garbage collected, if there are no other variables referring to it.

Notice that I mentioned variables "in scope"... a static variable is "in scope" as long as the class it belongs to is still loaded. Since mostly classes never get unloaded, that would basically mean "forever". An instance variable is "in scope" as long as the object it belongs to hasn't been garbage-collected yet, and a local variable is "in scope" as long as the flow of control is still inside the method where it's declared.

Those are loose definitions and the actual process of garbage collection can be more complicated than that, but for learning purposes they are good enough. The main thing you have to learn to start with is that variables are not objects; they may contain null references which point to nothing, or they may contain object references which point to objects.
 
Shivanshu Dhawan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

Variables are not involved in garbage-collection. Only objects are subject to being garbage collected.

And an object can be garbage collected if there are no variables in scope anywhere which contain references to the object. It doesn't matter whether those variables are static variables, or instance variables, or even local variables.

Earlier you said "what if at some point it is set to null?" apparently referring to a static variable. Yes, if you set a static variable (or any other kind of variable) to null, and it wasn't null before, then it will cease to refer to the object which it formerly referred to. This may make that object available to be garbage collected, if there are no other variables referring to it.

Notice that I mentioned variables "in scope"... a static variable is "in scope" as long as the class it belongs to is still loaded. Since mostly classes never get unloaded, that would basically mean "forever". An instance variable is "in scope" as long as the object it belongs to hasn't been garbage-collected yet, and a local variable is "in scope" as long as the flow of control is still inside the method where it's declared.

Those are loose definitions and the actual process of garbage collection can be more complicated than that, but for learning purposes they are good enough. The main thing you have to learn to start with is that variables are not objects; they may contain null references which point to nothing, or they may contain object references which point to objects.




Yes, perfect explanation. Thank you Paul you're awesome.
 
Shivanshu Dhawan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivanshu Dhawan wrote:The finalize() method is *not* run as soon as an object is eligible for GC. The method is called during GC.... basically, during GC, if an object is deemed unreachable, and the finalize() method has not been called previously, then it will be called. After the method is called, it is checked again for reachability.



Thank you Henry for your response. Can you please explain me with some sample code the use of static variable in GC?
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here we declare... well, this should be obvious to you. We have an object and a variable which contains a reference to it. Later in the code:



Now this should be just as obvious... the variable doesn't refer to the object any more. In the absence of any other code which assigns references to that object to other variables, it's now available to be garbage collected.

Note that the fact that the variable is static is irrelevant to garbage-collection. The same reasoning applies to any kind of variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!