This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Garbage Collection question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When "e3 = null;", does that set e3.e to null as well? If not why? It can no longer be referenced. So after "e3 = null;", isn't their one object available for GC?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeff Schuler:
... When "e3 = null;", does that set e3.e to null as well? If not why? ...


It does not set the e3.e reference to null. However, the object that e3.e is pointing to can no longer be reached, and this makes it eligible for garbage collection.

But there is a problem with this code. At runtime, it will throw a NullPointerException at the line e2.e = e1; At that point, e2 is already null, so e2 cannot be dereferenced to get to e2.e.
[ July 28, 2007: Message edited by: marc weber ]
 
Jeff Schuler
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by marc weber:

It does not set the e3.e reference to null. However, the object that e3.e is pointing to can no longer be reached, and this makes it eligible for garbage collection.

But there is a problem with this code. At runtime, it will throw a NullPointerException at the line e2.e = e1; At that point, e2 is already null, so e2 cannot be dereferenced to get to e2.e.

[ July 28, 2007: Message edited by: marc weber ]



Thanks for the reply. This question is referring to a question in K&B book chapter 3. It states that a single object is never eligible for GC even though there is one prior to the runtime exception.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeff Schuler:
... This question is referring to a question in K&B book chapter 3. It states that a single object is never eligible for GC even though there is one prior to the runtime exception.


Hmmm... I'm not sure what that means. Did you check the K&B Errata page?
 
I AM MIGHTY! Especially when I hold this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!