• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

the finalize method of Garbage Collection

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey !
Please forgive me...the question that you see may be totally irrelevent and crazy, but I am just a beginner picking up Java entirely on my own...with no background in Prog. at all....
Ok ! I went the topic Garbage Collection topic in a couple of books. I seemed to have understood the purpose and the most imp fact that Grabage Collection can only be suggested and not ensured etc...
Now, in Jaworski(those of you who might not have seen it , it doesnt really matter)
It says the prime condition for an OBJECT to be Grabage Collected after it becomes unreachable is that the object MUST OVERRIDE its finalize method....and if it doesnt then the it is not spotted by the Garbage Collector, does this mean it is upto the programmer to override the finalize method(or does it happen by default) of every object he feels should be suggested to the Garbage Collecter.
All the while I was under the impression that Garbage collection was an automatic operation
that is,

A a = new A("Hello");
B b = new B("Bye")
a = b;
"Hello" would automatically be reffered for Garbage Collection from this point onwards....

But for SURE it will not go to the Garbage Collector becoz I have not overridden the Objects finlaize method(According to Jaworski)....CAN I SAY THIS STATEMENT TO BE TRUE...
I hope I have been able to convey what I meant...If you guys have understood what i meant please provide me a detailed explanantion,
if this question is way out of the exam questions and it just cant be explained/making sense forget it.....
JDummy
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an class has the finalize method, it will be called on dead instances of that class before the memory occupied by that object is re-used. You only really need that method if you have some specific reason for wanting to get hold of objects as they are garbage collected, which is rare so most of the time you can ignore finalize().
If present, a class's finalise() method is called by the garbage collector at some point after the object is first recognised as garbage and before the memory is reclaimed.
Hope that's helpful.
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDummy,

You wrote :
----------------------------------------------------------------
It says the prime condition for an OBJECT to be Grabage Collected after it becomes unreachable is that the object MUST OVERRIDE its finalize method....and if it doesnt then the it is not spotted by the Garbage Collector
----------------------------------------------------------------
I have a copy of Jaworski, even as I am responding now, and NOWHERE in the Garbage Collection section does it say this, in my copy. My copy highlights the following points about Finalization:
1. Classes inherit finalize() from Object.This doesnot do anything so that
2.If Objects want to perform some clean up prior to being garbage collected, the should override this method.
3.If the object becomes unreachable, there is no guarantee that its finalize() method will be invoked, however
4.If an object does have a finalize() method, then it will be invoked prior to being collected.
5.finalize() will never be invoked more than once for a given ovject.
I hope you concentrate and re-read that material, it does not say overriding finalize() is the prime condition to be garbage collected.
I hope tis helps.
Herbert
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Herbert is absolutely right. It is important to understand Garbage Collection is JVM dependant. Java Language Specification only outlines the miminum guaranteed behaviour of GC.
An Object will not be garbage collected as long as it has active references. The Object's finalize() method will be invoked before the object is actually discarded.
IMO, these are the only two things you can rely on. It is quite an important concept for the certification. Don't get carried away by other options which look almost true.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls. see pg. 164 Jaworski, a sorta flow chart is drawn indicating the step by step procedure of the way Garbage collection works..
Now what is troubling me is this part :
If an object becomes unreachable, there is no guarantee that the object's finalize() method will be invoked.
THIS IS FINE, NEXT STATEMENT --->
However, if an object does have a finalize() method, this will be invoked before the object is Garbage Collected. Fig 7.3 illustrates the fine print...............
THIS STATEMENT ALSO OK...
My Question :
What if an object doesnt have a finalize method ??? Then what??
My Guess is : then NO WAY THE OBJECT IS ELIGIBLE FOR GARBAGE COLLECTION
AND IF GARBAGE COLLECTION IS TO TAKE PLACE THERE MUST BE A :
protected void finalize() {
}
Guys above thx for your help but I have still not understood, please try this....
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDummy,
All( most? ) Java classes descend from the priomordial java.lang.Object class. The do-nothing protected void finalize() method is defined in the object class. When a class does not provide( "override" ) the finalize method, the finalize method in the super class is called. This is just like calling a method in a typical inheritance scenario. So the finalize method is searched for beginning from the current class, all the way up the tree till the Object.
So, if your object does not override finalize() eventually the Object's finalize() method gets called, which does nothing.
Bottom line - overriding finalize() method gives your class an oppportunity to clean up the mess. However, in many cases you may not need such a facility. If you don't override finalize(), it will not stop the JVM from sweeping all unwanted objects.
Hope this helps.
Ajith
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you are getting this at all. The finalize() method is only needed when an object has to do something before it is garbage collected. It is not required. As you say, if an object has a finalize() method then it will be run before garbage collection. If it doesn't then the object is simply garbage collected.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith !!!
I got it....that was just the sorta explanation I was looking for.....
thx all
JD
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a similar question in this general area that is in response to Khalid's mock exam.
One of the statements identified as "true" in Question #30 is that:


The finalize() method wil never be called more than once on an object.


I understand that every object as an ultimate child of Object has a non-op finalize() method...that's no problem...however...
But if it is possible an overriding finalize() method to be defined that makes the object reachable again and the object later becomes unreachable...why won't its finalize() method be invoked again?
I can't think of how to do it exactly but I guess the code would be:
protected void finalize() {
super.finalize();
if ( someConditionIsTrue ) {
makeMeReachable();
}
else {
keepMeUnreachable();
}
}
Best regards,
Steve Butcher
exceptionraised@aol.com
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic