File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects eligible for Garbage collection question

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik D mehta, you've missed something there. On which line, you are concerning about the GC?
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again on which line?
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, which objects are eligible for GC? Why do think, it's two?
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean, after completion of the program?
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the main method in the question looks like this
Then the answer would be 2 objects eligible for GC...
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup thank you Ankit and Abimaran
yes i got that .
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic