• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need some clarification....

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain in greater detail what he's trying to say?
Source:www.jchq.net


[ June 30, 2008: Message edited by: gylph knor ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


EdGrundy(){
while(stop==false){
new Box(99);
}
}



let's look at what this code is doing. It is a while loop that will run, in essence, forever (nothing ever sets stop to true). Consequently we sit in this loop spinning on the new Box(99) statement which means that we are constantly creating new instances of a Box. But notice that we are not assigning the Box reference to a reference variable. That means that the references are not retained, rather they are dropped immediately upon creation. Every time that we drop a newly created Box it is elligible for garbage collection.

Now, let's look at garbage collection. The most that you can do is "ask" that garbage collection be run, but you can't force it. GC will run whenever it thinks it is needed usually when heap space is becoming short in supply. When and if GC collects a Box that has been discarded it will see that finalize() has been overridden and it will call it, at most once per object. Then, presuming the finalze() does not keep the object alive, GC will return it to free heap space.

So what we have here is a loop that is constantly instantiating a Box and then abandoning it. It is not retaining it BUT it is not going to be immediately returned to the heap as free. What you will see is that the program runs, scooping it's way through the heap creating and dropping new Box objects, eventually, free heap will start to get low and at some point GC will run and reclaim a number of abandoned objects and, for each one of these Box objects a finalizer() will be called that prints out "finalizing a Box". It will print that once for every Box that it collects to reclaim. When it has enough free heap space it stops collecting and the program continues running happily along until heap space gets low again. So your program will sit in this cycle until you stop it.

Does that help?
 
gylph knor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes , but why will it call the finalize method while collecting the abandoned objects?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why wouldn't it call the finalize method? If you're in doubt what that method does, check its (extensive) javadoc description.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gylph knor:
yes , but why will it call the finalize method while collecting the abandoned objects?



That's what the finalize() method is for. It is like how a Constructor is for the object being created, a finalize() method is for an object being destroyed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic