aspose file tools
The moose likes Java in General and the fly likes Question on Garbage Collection on arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Question on Garbage Collection on arrays" Watch "Question on Garbage Collection on arrays" New topic
Author

Question on Garbage Collection on arrays

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
Originally posted by Clay Chow:
So another question on objects:

Since an array (whether it be an array of primitives or objects) is an object itself, then a 2D array would create (number of elements in first dimension plus one) objects ?

For example, in the below code.

Line 1 creates 4 objects.
Line 2 creates 1 object (?)

After line 4, one object is eligible for gc (the original array at x[0]).
However, after line 5, there is still only 1 object eligible for gc (since there was not an array at x2[0] originally).

Thanks in advance for your help!

Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3268

Hi Clay,

Others may answer your question from a code perspective (please chip in people). But I found when learning this stuff that drawing diagrams helped a great deal.

Try drawing a diagram of the variables and their pointers to their objects and see when the point is safely 'cut' as you step through each line of code.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
Thanks for the advice, but that question is less about "is that object gc eligible?" and more about "was there a new object created?".

I still am confused on the situation above (any help would be great).

Also, another example I thought of would be below. Am I right in thinking there are two Integer objects created (one for 6 and one for 7) ? and would that mean that after the "y++" line, there is one object that is garbage collector eligible (i.e. new Integer(6)) or would it not be eligible because they are like immutable (like Strings are).

thanks!

[CODE}
Integer y = new Integer(6);
y++;
[/CODE]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

Also, another example I thought of would be below. Am I right in thinking there are two Integer objects created (one for 6 and one for 7) ? and would that mean that after the "y++" line, there is one object that is garbage collector eligible (i.e. new Integer(6))


This is going to be confusing -- you have too many things going on here.

Yes, the Integer(6) does get created -- as you created it yourself.

Yes, autoboxing will reassign the reference to a Integer(7). But, this object won't be created, as it already exist in the integer cache (which autoboxing uses).

Yes, the Integer(6) will be eligible for GC -- as it didn't come from the cache.

or would it not be eligible because they are like immutable (like Strings are).


There is no relationship between immutability and eligible for GC. Strings are immutable and they can be eligible for GC.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
Thanks for your help.

I will look up the Integer Cache topic.

Did you agree with my assessments in the opening post ?

Also, when a class is instantiated, does it also create all the parent objects as well (i guess when it calls the parent constructors. Therefore, in the code below, there would be two object(an 'Over' and an 'Under') eligible for GC after the 'a=null' line.



[ December 15, 2008: Message edited by: Clay Chow ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Question on Garbage Collection on arrays
 
Similar Threads
Garbage collection explanation
Garbage Collection Q & A
Question about Garbage Collection from ExamLab
Garbage collection
One more time :) G. C. Q and A