aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

gylph knor
Greenhorn

Joined: Jun 12, 2008
Posts: 20
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

Joined: Aug 16, 2005
Posts: 14430
    
  23

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi!
This program prints "dead" exactly one time.
Run it again and check console output carefully.
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
gylph knor
Greenhorn

Joined: Jun 12, 2008
Posts: 20
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

Joined: Jun 12, 2008
Posts: 20
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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???


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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.


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

thanks ramesh...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection