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