• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection question

 
Jeff Schuler
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?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 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
Java Mac Safari
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic