aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Garbage collection problem" Watch "Garbage collection problem" New topic
Author

Garbage collection problem

Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58


when line 7 is reached, how many objects are eligible for garbage collection?
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2928
    
  15

I think its None. Cause each of the instances are accessible via some reference.


Mohamed Sanaulla | My Blog
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

If this is a mock exam question, then please QuoteYourSources...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3050
    
    1

Actually, it's 3.

The go method will create two new objects, which will refer to each other. Then, it will let the argument refer to the one, and the method will return the other.

The main method creates a new object, which is passed to the go method. So this object will now refer to one of the two new objects in the go method, and t2 will now refer to the other object returned from the method.

Then, the method goes out of scope, so all objects become eligible for collection (note that t hides the instance member t).

If the question means that by line 7 the method hasn't gone out of scope yet, then no object is eligible, since t still has a reference to one of them, which has a reference to t2 in go, which has a reference to t1 in go.
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2928
    
  15

And I considered that the method has not gone out of scope Thanks Stephan for the explanation.
Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 142

Stephan van Hulst wrote:
Then, the method goes out of scope, so all objects become eligible for collection (note that t hides the instance member t).


Stephan, when you say that the method goes out of scope are you considering the "main" method or "go" method?
Because I think that if you are talking about the go method, no objects will be collected but if you are considering the main method going out of scope so I agree.

I'm only asking to understand better this question.

Thanks.


Adolfo Eloy
Software Developer
OCPJP 6
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3050
    
    1

I meant the main method.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Garbage collection problem
 
Similar Threads
Question about finalize method of GC
Garbage Collection
Doubt in Garbage Collection (GC) question in K&B Book.
Grabage Collection
garbage collection and finalize method- inconsistent result