Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why variable c2 is not eligible for garbage collection?

 
Dana Ucaed
Ranch Hand
Posts: 154
3
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,



the question is: When //do Stuff is reached, how many objects are eligible for garbage collection?

In my opinion after the call of method go(), c2 become null.

Why c2 is not eligible for garbage collection?

Thank you very much,
Anda
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:
Q: Why c2 is not eligible for garbage collection?

A: after the call of method go(), c2 become null.


"null" and "no reachable reference to" are not synonymous.

An object, regardless of content--null or otherwise--is garbage collected when "the object is no longer reachable" (no references pointing to it and/or references are out-of-scope).

Think of an object like a glass of water: glassOfWater = null; just empties the glass of water; it doesn't throw the glass away or make the glass unusable.  "null" is analogous to "empty", not "non-existent".
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: Sybex8, p. 36
 
Piet Souris
Rancher
Posts: 1687
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:(...)
In my opinion after the call of method go(), c2 become null.
(...)

Note that c2 does not become null. Any idea why, and how to test for this?
 
Liutauras Vilda
Marshal
Posts: 3399
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be tricky to understand initially. Try to read this text, the answer is there, nearly at the end, but read whole text for wider topic's understanding.
 
Dana Ucaed
Ranch Hand
Posts: 154
3
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Anda Cristea wrote:(...)
In my opinion after the call of method go(), c2 become null.
(...)

Note that c2 does not become null. Any idea why, and how to test for this?


The variable c2 remains not null. is not the same with the variable cb.
 
Liutauras Vilda
Marshal
Posts: 3399
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read that article I linked before?

Particularly read "How does pass-by-value work with references?" paragraph if you haven't done so yet. There is an answer.
 
Piet Souris
Rancher
Posts: 1687
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP's answer is correct. C2 and cb are  different references,  initially referring to the same object. Cb is next set to null,  while C2 remains unchanged.
 
Liutauras Vilda
Marshal
Posts: 3399
131
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I slightly misunderstood it.

In addition to OP's answer, it is worth to mention, that cb reference variable isn't just different from c2 reference variable, it is a copy of c2. While these both reference variables refering to the same object, references are different, and once you re-assign cb to something else, you lose the link with the object c2 is refering to, but it has no effect for c2 reference variable.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how It would look in pictorial form:
Note: Beneath object circles those address numbers 1000, 500 and 1007, 505 are just assumed to identify which object we are talking about.
Diagram-(1).png
[Thumbnail for Diagram-(1).png]
 
Roel De Nijs
Sheriff
Posts: 10763
148
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:Why c2 is not eligible for garbage collection?

First of all, you should always QuoteYourSources when you are posting a code snippet. Secondly when you post such a code snippet, you should not change the class name(s). And the reason why is pretty simple: using the class name of a code snippet ranchers can easily search (using the excellent search function) for topics about the same code snippet they have doubts/questions about. If you change the class name to something different, they might miss out on valuable information.

In this topic you'll find an excellent explanation (with additional code snippets) about exactly the same code snippet. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Dana Ucaed
Ranch Hand
Posts: 154
3
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian West wrote:Source: Sybex8, p. 36


Sorry, what is Sybex8?
 
Roel De Nijs
Sheriff
Posts: 10763
148
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:Sorry, what is Sybex8?

That's this OCAJP8 study guide.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!