aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused with gc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused with gc" Watch "confused with gc" New topic
Author

confused with gc

Armstrong Dor
Greenhorn

Joined: Jul 23, 2002
Posts: 5
Hi,
I am not able to understand the code below from Mughal's page 253 & 254 but i know it is finalize() chaining i have gone through these two pages many times but still not able to understand the problem I have two doubts

when you compile and run the code twice by giveing two types of COMMAND ARGUMENTS as BELOW

java Finalizers 5 50 (the 1st time)
it prints like this
50: Hello
50: Hello
50: Hello
50: Hello
50: Hello
5 blobs alive
------------------------------------------
java Finalizers 5 50000 (the 2nd time)
it prints like this
50000: Hello
50000: Hello
50000: Hello
: Bye
: Bye
50000: Hello
50000: Hello
3 blobs alive
------------------------------------------
the above is correct but what i did not understand is this
1. why cant it print 5 blobs alive when i give the 2nd COMMAND ARGUMENT as 50000
why is it saying 3 blobs alive.
2. why does it print Bye after the 3rd Hello when I give the 2nd COMMAND ARGUMENT as 50000
which does not happen when i give the 2nd COMMAND ARGUMENT 50.

Thank you very much


Armstrong D
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
This program shows you the thing about GC; GC will re-claim memory, but the exact time or the calculation it goes through to find out the time is platform dependent. In Java, you can't force GC to re-claim a particular object, to re-claim objects at particulary, or both. This means as a programmer you can never predict just when would an object that is eligible for garbage collection would be garbage collected.
This program create objects that were not assign any reference to it, so you know that right after you create them they are eligible to be garbage collected. To show you how it work, the constructor prints out message showing you that the objects arecreated. Then, when GC actually do decide to work, the finalize method is executed to show that the object is re-claimed. And at the end, the program prints out just how many object are still alive (it uses a static int to keep track, notice how it increase in consstructor and decrease in finalize method).
For you first question, GC did not work (for some reason it decide not to do any work that time). That is why you see 5 objects being created and all 5 objects were still alive when the program end. The second time, GC did work after the 3rd object was created. Relating to your second question, this is why bye was printed; bye was printed twice because 2 out of the 5 objects created was garbage collected, so each of the GCed objects' finalize method was executed. Because 2 of the 5 objects were GCed, and so at the end of the 2nd run you see that only 3 blobs are alive.
Does this help? And please correct me if I am wrong folks.


Let us be showered in the light of confusion!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
This program simply creates objects from the heap and causes them to be of a given size, depending upon the second argument, the "blob size." Notice that, once an object is created, there are no references to it so it is immediately available for garbage collection.
What this program is showing is that, if the objects are of a small size, the garbage collector won't bother to run. In that case, there is plenty of available memory so using up precious processor time to clean up memory is just a waste.
In the second case, we cause the size of each object to be quite large. In this case, after a few objects are created, we need to reclaim some space from memory in order to create more. So, the garbage collection process executes and collects two objects, calling their finalize methods. That's what causes the two "Bye" statements to be there. That's where two objects got garbage collected.
I hope that helps,
Corey


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused with gc