• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Garbage Collection on arrays

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic