File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC question

 
Surendra Kumar
Ranch Hand
Posts: 236
  • 0
  • Mark post as helpful
  • send pies
  • 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);
}
}
}
 
Arnab Sinha
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just the same as

compiles and output is Beta
 
parshuram bingi
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic