Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Objects eligible for Garbage collection question  RSS feed

 
Ranch Hand
Posts: 121
Java
  • 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 .
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 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
  • 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
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again on which line?
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • 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
  • 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
  • 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
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean, after completion of the program?
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!