• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

GC question

 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below program compiles and runs.
But my doubt is that the finalize() method should be 'protected', right?
so, how come this version of finalize() runs?

class Box{
private int iVolume;
Box(int iVolume){
this.iVolume=iVolume;
}
public void finalize(){
System.out.println("finalizing a Box");
}
}

public class EdGrundy{
boolean stop = false;
public static void main(String argv[]){
new EdGrundy();
}
EdGrundy(){
while(stop==false){
new Box(99);
}
}
}
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finalize method is inherited from Object class. So when you are using it in your class, the rules of overriding are applied. One of the rules is that the overriden method may have a less restritive access modifier than the parent class and must not have a more restrictive access modifier. This is the reason why your code compiles and runs.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just the same as

compiles and output is Beta
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I changed the original program to
----------------------------------
EdGrundy(){
//while(stop==false){
new Box(99);
//}
----------------------------------

nothing is printed. but when the while loop is running the "finalizing a Box" string is printed continuously why?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi cowboys!

If you //delete the loop only one object is made. You cannot say when the garbage collector will run. Probably he isn't caring about just one object and leaves it alone.
With the endless loop the garbage collector is pretty busy though.

What if you experiment a bit using a for loop running 100.000 times, or less, or more?
There is a chance for the GC to run (and so the finalize method) with having only one Box if you invoke System.gc(); after.
But only a chance, there's no garanty that it will run. But there's a garanty that GC will invoke finalize on each object only once.


Yours,
Bu.
 
reply
    Bookmark Topic Watch Topic
  • New Topic