Re: How to give selective access to the methods in a class?



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
.



Relevant Pages

  • Re: How to free memory in Javascript?
    ... The only memory allocated here is the space allocated for i. ... How do i get rid of the reference to button? ... Whilst in some sense reference counting is a garbage collection pattern ...
    (microsoft.public.scripting.jscript)
  • Re: Question about turning off garbage collection
    ... turn off garbage collection while the critical code of the thread is ... Now, I don't know much about how the garbage collector works in python, ... Reference counting alone is not ... strong enough to detect trash objects in cycles (like A points to B ...
    (comp.lang.python)
  • Re: Dispose(bool), Idisposable, form closing etc.
    ... It is called when a form is closed and it is not a modal form, ... to be hidden and the DialogResult property to be set to DialogResult.Cancel. ... and therefore a candiate for garbage collection, ... There is no actual reference count, per se, but all the reference to the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Why C# and Java have got it wrong
    ... >> which as far as I know, after decades of programming, is not true. ... >> 1) Garbage collection is inefficient in actual systems. ... variable is the primary example of an expanded instance. ... obtain a copyable reference to an expanded instance. ...
    (comp.programming)