• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Garbage Collection on arrays  RSS feed

 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!