This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Why variable c2 is not eligible for garbage collection?

 
Ranch Hand
Posts: 451
6
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
 
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
 
Saloon Keeper
Posts: 3413
149
  • 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?
 
Marshal
Posts: 6973
471
Mac OS X VI Editor BSD Linux
  • 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: 451
6
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: 6973
471
Mac OS X VI Editor BSD Linux
  • 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
Saloon Keeper
Posts: 3413
149
  • 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: 6973
471
Mac OS X VI Editor BSD Linux
  • 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.
 
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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]
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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: 451
6
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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anda Cristea wrote:Sorry, what is Sybex8?


That's this OCAJP8 study guide.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!