• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt in GC

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Date object will occupy 24 bytes of memory"

This is system dependent, I think.

Bu.
 
That is a really big piece of pie for such a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic