File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects eligible for Garbage collection question 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 "Objects eligible for Garbage collection question" Watch "Objects eligible for Garbage collection question" New topic
Author

Objects eligible for Garbage collection question

Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Hello ,
I got this question from CertPal Testing Engine .

The question is how many objects are eligible for Garbage Collection and what is the output .
According to me it is 2 and output is 0.
But on the Test site it is given it is 0 Objects are Eligible for Garbage collection and the output is 10 .
I am not getting it .

Understanding is Everything - Peter Lord
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

This is about passing arguments to the methods

always remember that when we are passing arguments to the methods,
then the copies of the reference variables are created and both refer to the same object
hence even if one copy becomes null, another one is still referring to it
so the object is not eligible for the garbage collection



hope this helps


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

I understand that .
I thought
since new Main().go() does not have a reference it is eligible .
and
since in method go()
pudding reference is on the stack and the stack will be blown after the method is complete because it is local to method
But still the object new Main() and object referenced by pudding is on the Heap and can be garbage collected .
And
I agree the ouput will be 10 i just miswrote it in the thread .

So
Do you think i misunderstood the question
Reply
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Pratik D mehta, you've missed something there. On which line, you are concerning about the GC?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

yes Abimaran you are right i missed that part .
The question is exactly like this
How many objects are eligible for garbage collection and what is the program output ?
And yes Thank you that is the important part
So what do you think should be the answer .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Again on which line?
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

I just told you the question is exactly as i mentioned
so the question is incomplete and something is wrong with the test engine ??

and what will be the answer if it was the last line of the program .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ok, which objects are eligible for GC? Why do think, it's two?
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Pratik D mehta wrote:
since in method go()
pudding reference is on the stack and the stack will be blown after the method is complete because it is local to method

Reply
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Pratik D mehta wrote:
new Main().go()
does not have a reference variable
since in method go()
pudding reference is on the stack and the stack will be blown after the method is complete because it is local to method

Reply
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

Pratik D mehta wrote:
Pratik D mehta wrote:
new Main().go()
does not have a reference variable
since in method go()
pudding reference is on the stack and the stack will be blown after the method is complete because it is local to method

Reply


actually
this is what I was trying to answer in the first post
look at the following code


hope this is clear now
happy to help :-)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You mean, after completion of the program?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

If the main method in the question looks like this
Then the answer would be 2 objects eligible for GC...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

yup thank you Ankit and Abimaran
yes i got that .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You are Welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects eligible for Garbage collection question