Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper class array and Garbage Collection

 
Nakul Kasadwala
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All wrapper class is immutable.
Then what about array of wrapper class?
How many objects will be eligible for Garbage Collection in following example on the line no 6 and 7.

public class GarbageCollectionArray{
public static void main(String args[]){
trial();
}// line 7
static void trial(){
String arr[] = new String[10]; // Line 1
for(int i = 0; i < arr.length; i++){// Line 2
arr[i] = ""+i;// Line 3
}// Line 4
arr[5] = "Six";// Line 5
System.out.println("arr[5] : "+arr[5]);// line 6
}
}
Hoping for the best
 
asim wagan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
1) At line 6; only one object will be available for garbage collection because arr[5] is now referencing to something else.
2) At line 7; There will be 11 objects available for gc.
 
Nakul Kasadwala
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any one can tell me wheather i am right or wrong?
e.g String name[] = {"NAkul","Atif","Kamlesh","MAc"};
"Assuming that name is local array there will be 4 handle on stack and 4 object on heap."
and if i say name[2] = "Jay";
"Kamlesh" will be eligible for garbage collection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic