This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

when is eligible for garbage collection?

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. }
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic