Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when is eligible for garbage collection?

 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone give me a concept and answer the below question? Thanks.
When is the Integer object created in line 3, which line eligible for garbage collection?
1. class m {
2. public Object m () {
3. object o = new Integer (10);
4. object [] oa = new object [1];
5. oa[0]= o;
6. o = null;
7. return oa[0];
8. }
9. }
 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Integer object created in line 3 is not eligible for gc in the m() method. This is bcos line 7 is returning a reference to this object.

1 class m {
2 public Object m () {
3 object o = new Integer (10);
4 object [] oa = new object [1];
5 oa[0]= o;
6 o = null;
7 return oa[0];
8 }
9 public static void main(String s[]){
10 m m1 = new m();
11 Object o = m1.m;
12 o = "Don Bosco";
}
}

In the above code, the object created in line 3 is eligible for gc in line 12.
 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Don Bosco,how about line 6, is line 6 eligible
for garbage collection? (o=null),thanks
1 class m {
2 public Object m () {
3 object o = new Integer (10);
4 object [] oa = new object [1];
5 oa[0]= o;
6 o = null;
7 return oa[0];
8 }
9 public static void main(String s[]){
10 m m1 = new m();
11 Object o = m1.m;
12 o = "Don Bosco";
}
}
 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by eric lee:
Hi,Don Bosco,how about line 6, is line 6 eligible
for garbage collection? (o=null),thanks
1 class m {
2 public Object m () {
3 object o = new Integer (10);
4 object [] oa = new object [1];
5 oa[0]= o;
6 o = null;
7 return oa[0];
8 }
9 public static void main(String s[]){
10 m m1 = new m();
11 Object ob = m1.m;
12 ob = "Don Bosco";
}
}


o and oa[0] both point to same OBJECT before line6.
at line6 o is made to point null. but since oa[0] still points to that OBJECT, the OBJECT is not eligible for garbage collection.
remember that garbage collection is done on OBJECTS, not on references.
[ December 03, 2002: Message edited by: Don Bosco ]
 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Don:
so the line 5 is eligible for garbage collection?
Do i correct? Thanks.
class test {
public static void main(String args[]){
1. String myString = new String("Batman");
2. String mySecond = new String("Robin");
3. System.out.println(myString + mySecond);
4. myString = null;
5. myString = mySecond;
6. mySecond = null;
}
}
 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After line 4, the object created in line 1 is eligible for gc.
 
eric lee
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Don Bosco,i get a little clear now,can you give me one more example for gc like garbage collection is done on OBJECTS, not on references.
by the way, you said that after line 4 ,so the answer is line 5 for gc correct? Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic