Re: How to give selective access to the methods in a class?
- From: Chris Smith <cdsmith@xxxxxxx>
- Date: Fri, 11 Aug 2006 11:05:20 -0600
Patricia Shanahan <pats@xxxxxxx> wrote:
I'm surprised, and curious about this statement. On the face of it, Java
garbage collection is prevented by an object being reachable, and
assigning null to a reference may transition an object from reachable to
unreachable.
Assigning null to a reference is certainly an effective way of
preventing an object from being reachable from that reference. However,
in almost all situations, it should be unnecessary to assign null to a
reference just for the sake of garbage collection. It's generally
better to either allow a reference to go out of scope or rely on a
larger object (perhaps a Collections API container class) to manage
reachability in a more logical way.
Your simulation example may be a pseudo-exception. If each component
can only have one transaction at a time and spends a lot of time without
one, then setting that field to null to indicate that the component is
not working in a transaction is a sensible thing to do. Even so, the
real issue there is that the field being null is *right*, not that it
helps garbage collection. Even without the concern for garbage
collection, setting that field to null to indicate the lack of a
transaction would be a good thing to do.
There are other circumstances (which I only mentioned in passing in my
original response) where it's indeed a good idea to set a variable to
null merely for the sake of garbage collection. For example, the
implementation of ArrayList allocates a block of references, and keeps
the ones that are beyond the end of the current list null to avoid
accidentally retaining objects. However, these are the exception rather
than the rule.
Maybe your experience differs, but I have rarely if ever needed to
explicitly set variables to null just to please the garbage collector.
As such, I acknowledge that this can theoretically occur, but I remain
skeptical when the idea of nulling references is brought up, until
there's evidence that it's needed.
--
Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation
.
- Follow-Ups:
- Re: How to give selective access to the methods in a class?
- From: Chris Smith
- Re: How to give selective access to the methods in a class?
- References:
- How to give selective access to the methods in a class?
- From: toton
- Re: How to give selective access to the methods in a class?
- From: Eric Sosman
- Re: How to give selective access to the methods in a class?
- From: toton
- Re: How to give selective access to the methods in a class?
- From: Eric Sosman
- Re: How to give selective access to the methods in a class?
- From: toton
- Re: How to give selective access to the methods in a class?
- From: Eric Sosman
- Re: How to give selective access to the methods in a class?
- From: toton
- Re: How to give selective access to the methods in a class?
- From: Eric Sosman
- Re: How to give selective access to the methods in a class?
- From: toton
- Re: How to give selective access to the methods in a class?
- From: Chris Smith
- Re: How to give selective access to the methods in a class?
- From: Patricia Shanahan
- How to give selective access to the methods in a class?
- Prev by Date: JNI error with JDK 1.5
- Next by Date: Re: How do i get a reference count of a object in java ?
- Previous by thread: Re: How to give selective access to the methods in a class?
- Next by thread: Re: How to give selective access to the methods in a class?
- Index(es):
Relevant Pages
|