• 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

garbage collection

 
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! can anybody tell that why this program not printing "dead" but is rather throwing this exception after creating a few objects







Program:



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the following two lines of your code. What do you think is happening in these two lines?

And what is happening in the finalize() method? Especially in this line:

Do you understand that the finalize() method is a special kind of method, that is called when the object is garbage collected?
[ August 25, 2008: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
This program prints "dead" exactly one time.
Run it again and check console output carefully.
 
Ranch Hand
Posts: 664
  • Likes 1
  • 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:
hi! can anybody tell that why this program not printing "dead" but is rather throwing this exception after creating a few objects









It's because you are using while(true).This results in an endless loop as it is alwyas true and it keeps creating objects till you run out of space.

That's why you get the java.lang.OutOfMemoryError

Hope that helps
 
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
hi,

yes i understand what should happen here. i am trying to fill the memory with objects having no refrence, thus forcing the garbage collector to run and in turn execute the finalize method. But this doesn't happen. i do not understand why? can someone 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
and i added the Thread.sleep(10000) to allow me to see that my finalize method has executed, otherwise its very difficult to see.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but the sleep is blocking JVM from destroying your objects for about 1000 seconds(this value might be wrong). So when the garbage collector runs it is blocked for 1000secs(so no objects can be garbage collected for that time period) in that time the loop creates so many objects that the JVM is out of memory.....

After that I want to ask that is Garbage collector a thread. As the output of this program goes, dead is displayed only once. This means that garbage collector is a thread(daemon) and the sleep method stops it from garbage collecting any objects.

Or is a separate garbage collector invoked to destroy every object???
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
garbage collection is a daemon thread.

and in above program remove sleep in finalize method you can see several times finalize is called, use eclipse and run in debug mode you can find control entering finalize method.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ramesh...
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankit Garg:
but the sleep is blocking JVM from destroying your objects for about 1000 seconds(this value might be wrong). So when the garbage collector runs it is blocked for 1000secs(so no objects can be garbage collected for that time period) in that time the loop creates so many objects that the JVM is out of memory.....

After that I want to ask that is Garbage collector a thread. As the output of this program goes, dead is displayed only once. This means that garbage collector is a thread(daemon) and the sleep method stops it from garbage collecting any objects.

Or is a separate garbage collector invoked to destroy every object???


About your last question: I don't know, but it doesn't matter. What happens is that the program very quickly creates lots of objects, but destroying each of those objects takes a long time because of the 1000 seconds pause in the finalizer. It doesn't matter if there is only one garbage collector or if there is a separate garbage collector for every object (think about it!) - simply because objects are created much faster than they are destroyed, you'll get an OutOfMemoryError.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:

About your last question: I don't know, but it doesn't matter. What happens is that the program very quickly creates lots of objects, but destroying each of those objects takes a long time because of the 1000 seconds pause in the finalizer. It doesn't matter if there is only one garbage collector or if there is a separate garbage collector for every object (think about it!) - simply because objects are created much faster than they are destroyed, you'll get an OutOfMemoryError.



Ya I know that....In 1000secs JVM can make objects that will fill this whole earth . I just wanted to explain it the correct way as I had an idea that Garbage collector is a thread. I just wanted to confirm that....
 
reply
    Bookmark Topic Watch Topic
  • New Topic