aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC-String array doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC-String array doubt" Watch "GC-String array doubt" New topic
Author

GC-String array doubt

Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
I was just playing around with string arrays, and I got this doubt:
How many objects are eligible for GC after the call to myMethod() returns?
also, would the elements of the strarray, that is "a", "b" etc. be kept in the string pool? I guess not..but just want to confirm.
public void myMethod(){
String [] strarray=new String[]("a","b","c","d","e"};
}
Thanks.


Anup Engineer
Donald Yee
Greenhorn

Joined: Feb 22, 2002
Posts: 6
Originally posted by Anup Engineer:
I was just playing around with string arrays, and I got this doubt:
How many objects are eligible for GC after the call to myMethod() returns?
also, would the elements of the strarray, that is "a", "b" etc. be kept in the string pool? I guess not..but just want to confirm.
public void myMethod(){
String [] strarray=new String[]("a","b","c","d","e"};
}
Thanks.

I believe that the array itself counts as a single object, even though it has 5 subitems. As such, there is only 1 object that can be GC'ed.
I think you can also check this link, found it in the search for array and gc
GC link
and for the second question, I don't believe those letters are placed into the string pool, as that creates a bunch of string objects which belong to the array. Or, in other words, it's like shorthand for:
strarray[0] = new String("a");
strarray[1] = new String("b");
strarray[2] = new String("c");
strarray[3] = new String("d");
strarray[4] = new String("e");
So in short the array is one object with 5 subobjects that are string objects. Hrm, that makes it sound confusing. Let me draw a picture...
strarray
+-------------+
| strarray[0] |
| strarray[1] |
| strarray[2] |
| strarray[3] |
| strarray[4] |
+-------------+
So, since the string objects belong to the array, they cannot be individually gc'ed, they have to be gc'ed as a group. Which is why it's one object. Hopefully not too confusing and I said it right. Otherwise someone will correct me soon enough
[ March 27, 2002: Message edited by: Donald Yee ]
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
What I believed was that 6 objects in total will be GCed. But like you said, somebody with good knowledge on this, will soon enough be of help
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
There is only one object elegible for GC,
the array.
All other objects are literal strings and literal
strings are not eligible for GC (on most
platforms).
However, had we created the array like this:
String strings[] =
{
new String("1")
, new String("2")
, new String("3")
, new String("4")
, new String("5")
};
is a different story. That would make it 6
objects (5 strings + 1 array).
Question: What about array literals?
Are they always eligible for GC.
Notice that if we have:
int[] x = {1,2,3};
int[] y = {1,2,3};
then x == y and x.equals(y) both return false.
What does this say? I think it says they are
always eligible for GC. i.e. there is no constant
references to them.
Comments?
thanks, J
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Could someone clear the doubts in this thread?
Val..others...?!
Thanks!
Mario Levesque
Ranch Hand

Joined: Nov 01, 2000
Posts: 51
Guys,
Val posted some interesting comments here on a similar subject.
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=015183
Hope this helps.
Mario


<a href="http://www.ajmasters.com" target="_blank" rel="nofollow">http://www.ajmasters.com</a> Real Estate, Tampa Florida
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC-String array doubt