• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How Many Objects are eligible for garbage collection?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was trying to solve questions related to Garbage Collection in Kathy Sierra SCJP book and I found that questions relating to the topic mentioned went wrong . I appreciate if some one could analyze and tell me how to answer these kind of questions. For sample, I am providing a questions given in the book..

public class X {

1 public static void main(String args[])
2 {
3 X x=new X();
4 X x2=m1(x);
5 X x4=new X();
6 x2=x4;
7
8 }
9
10 private static X m1(X mx) {
11 mx=new X();
12 return mx;

}
}
After line 6 how many objects are eligible for garbage collection..
A. 0
B. 1
C. 2
D. 3
E. 4

answer: B

class X2{
public X2 x;
public static void main(String args[])
{
X2 x2=new X2();
X2 x3=new X2();
x2.x=x3;
x3.x=x2;
x2=new X2();
x3=x2;//line 9
}
}

after line 9 , how many objects are eligible for garbage collection?

-Reference
SCP&D for java2 Study Guide Kathy Sierra and Bert Bates

Appreciate your time in replying

Thanks
Madhu

}


}
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Iam answering only the first question:

Here it is:

Line 3: Object X referenced as x created
Line 11: As a result of the function call at Line4 new X object created ands returned to main referenced as x2.
Line 5: New X object referred as x4 created.
Line 6 make x2 point to the object at x4. So two references are now pointing to the same object. And the second object which was previously pointed to by x2 is now abandoned. No taker for this. And hence this is the only candidate for GC now.

Hope this helps!!!

--Vidya
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As to your second question, at first two objects are created, each referring to the other and each with a local reference variable in method main() pointing to it.
Then, a third object is created and by line 9 the two local reference variables both point to the third object.


At that point, the first two objects refer to each other but neither object can be accessed by code in any active thread.

So, the first two objects can be garbaged collected. BTW, GC algorithms are not fooled by the objects referring to each other - those objects are toast.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused, I would interpret the 2 lost objects as a memory leak (not GC). Correct me if I'm wrong when an object is still being referenced it will not be garbaged collected. The 2 objects contain instances that refer to each other. Therefore by def'n they should still NOT be garbage collected.

Anyone else have any opinion?

Thanks
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The garbage collector is smart enough to recognise groups of objects referring only to each other but with no way to reference them from outside that group.
If such a group is found by the garbage collector the entire group is garbage collected together, effectively removing one possible cause of memory leakage.

P.S. read up on "islands of isolation" which is what this is called.
[ October 29, 2004: Message edited by: Jeroen Wenting ]
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is a memory leak then possible if this islands of isolation are found by the gc.

Second in your assumption that these islands are gc, what if these islands are complex, it will take a lot of cpu cycles for the gc to locate these and clear them up.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Java Language Specification:

A reachable object is any object that can be accessed in any potential continuing computation from any live thread

In the example, once x2 and x4 point to another object, the first two objects become unreachable and eligible for garbage collection. References that can never be accessed don't matter.

what if these islands are complex, it will take a lot of cpu cycles for the gc to locate these and clear them up.

Algorithms have been developed to make this process efficient. You could google garbage collection to see how.
[ October 29, 2004: Message edited by: Mike Gershman ]
 
Oh. Hi guys! Look at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic