aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
After line 1 how many objects are eligible for gc.



I think only line2 is eligible to be garbage collected.
Because in line 2 we are passing the object and in the domethod() a new object is created and returned so the one created in line 2 is lost and is eligible to be garbage collected.

Correct me if i am wrong.
Thanks in advance
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Correct. There are a total of 4 objects created. The first of these is never assigned to any variable, and that's the one eligible for garbage collection. As of line 1, the other 3 objects remain referenced by variables d, c, and y.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
thanks marc
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


At line 1 or the correct wordings are after the main method get completes , how many objects are eligible for GC ??

The correct answer is 4 .

Right ?

Thanks .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by rathi ji:
...after the main method get completes , how many objects are eligible for GC ??

The correct answer is 4 .

Right ? ...

Right. The 3 variables referencing call objects are all local to main, so as soon as main returns, there's nothing referencing those objects, so they're all eligible for garbage collection.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks marc
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
From the original question:

After line 1 how many objects are eligible for gc.

code:


public class call
{
public static void main(String[]arg)
{
call d=domethod(new call());//line 2
call c=domethod(d);//line 3
call y = new call();//line 4
//line 1
}

public static call domethod(call a)
{
call b=new call();
return b;
}
}

Shouldn't the answer be 4, even though line 1 is inside the main method. If there are no future references to the object in the method, they could be garbage collected, though the method has not completed. Right?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by M Rama:
... If there are no future references to the object in the method, they could be garbage collected, though the method has not completed. Right?

The thing to consider is that these variables are still in scope. Although they're aren't being used after line 1, they are still available for use.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection