File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
gylph knor
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • 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:



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
This program prints "dead" exactly one time.
Run it again and check console output carefully.
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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???
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ramesh...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic