File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in 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 "Doubt in GC" Watch "Doubt in GC" New topic
Author

Doubt in GC

madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
Hi
I have tried below example given in K&B
I did my experiments on it.



when I have executed above prog I got result as

Total Memory 5177344
Free Memory 4994848
Before GC 4962024
After GC 5049808

I have created 1000 objects then nullified all the references.
so all the created objects are eligible for GC.
then the memory diff is 5049808-4962024=87784 means this memory is occupied by 1000 Date Objects

I have removed line 1 in the above prog
then the result:
Total Memory 5177344
Free Memory 4994848
Before GC 4962024
After GC 5049784
no object is eligible for GC.
then Memory diff is 5049784-4962024=87760

So when we have used null, it has released 24 bytes extra memory than using without null.

can anyone justify this answer?
or guide me if I am understanding wrongly?

Thanks in advance
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy Madhu!

You're doing interesting experiments.

In each iteration of the for loop, the object of the previous iteration is eligible for GC also without the null line. Because you re-reference the d variable to the actual new date object. And the object from the last round is now "not accessible by any live thread". Hence eligible without the nulling.

The extra memory with your line 1 comes from the last iteration. When d is not nulled, it is the only date object that is still referenced by variable d. If you null this, also the last d is doomed.


Yours,
Bu.


all events occur in real time
madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
Thanks Bu,
for your explanation.
well understood.
So I can write as below also. right?


so, Date object will occupy 24 bytes of memory.

Thanks
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
"Date object will occupy 24 bytes of memory"

This is system dependent, I think.

Bu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in GC