This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many object are eligible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many object are eligible?" Watch "How many object are eligible?" New topic
Author

How many object are eligible?

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
How many objects are eligible for garbage collection by the time the method f() is invoked?

Result says its 2, but what i think it should be 1, can somebody elaborate how it should be 2 if my concept is wrong?

As i can see there are 3 objectes created and at the end 'c1' is still pointed by z, while 'c2' is lost and 'c3 'is pointed by c1 and c3


Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

I guess you are mistaken here that c1 is pointed to c.......Well 1 thing about java is that its pass by copy. So bitpattern used to get to the object it refers is passed not the object. and when c1 = c3 and c2 = c3 happens means c1 and c2 is used to refer the object which is referred by c3. Now the objects referred by c1 and c2 are eligible for GC. The reference z inside the objects are reference variables which reside in the heap and thus cannot be used from the outside world or from a live thread.

gc questions use pen and paper to solve.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

UseCodeTags explains how to use code tags correctly; use [ and ] instead of < and >, or just use the "Code" button above the editing window.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Sorry but as i was not giving a code so i din't use any [code] to explain it theoretically.
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Where did this code some from? Was it K&B? Quote your sources.

Lines 18-19
c1 --> (MyClass)
c2 --> (MyClass)

Lines 20-21
c1 --> (MyClass - with z --> c2)
c2 --> (MyClass - with z --> c1)

Line 22
c3 --> (MyClass)

Lines 23-24
c1 --> c3
c2 --> c3
c3 --> (MyClass)
?? --> (MyClass - with z --> c2)[was c1]
?? --> (MyClass - with z --> c1)[was c2]

There is nothing on the stack referring to the objects that c1 and c2 used to refer to, they are both eligible. Answer is 2.

Judging by what you have said before the code, you have misunderstood what was happening. I suggest you go through is again carefully.


SCJP6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Nitish Bangera wrote:Sorry but as i was not giving a code so i din't use any [code] to explain it theoretically.

My post was for Rajesh, not for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many object are eligible?
 
Similar Threads
how many objects will be eligible for garbage collection
Stuck in garbage collection???????????
Garbage collection doubt
Garbage Collection doubt
Query related to garbage collection