Consider a doubly-linked list holding a handful of nodes. If the list
is cleared by nulling the head and tail references, is the GC
intelligent enough to dispose of all of the nodes? For example, the
first node will no longer be referenced by the list, but it will be
referenced by the second node. Likewise, the second node will be
referenced by the first and third nodes, and so on.
Any thoguhts?
Thomas Schodt - 01 Nov 2005 18:39 GMT
> Consider a doubly-linked list holding a handful of nodes. If the list
> is cleared by nulling the head and tail references, is the GC
> intelligent enough to dispose of all of the nodes?
> Any thoughts?
Google for how modern GC works.
Benji - 01 Nov 2005 19:28 GMT
> Consider a doubly-linked list holding a handful of nodes. If the list
> is cleared by nulling the head and tail references, is the GC
> intelligent enough to dispose of all of the nodes? For example, the
> first node will no longer be referenced by the list, but it will be
> referenced by the second node. Likewise, the second node will be
> referenced by the first and third nodes, and so on.
> Any thoguhts?
Java's garbage collector is actually a misnomer. It's more of a
non-garbage collector. It finds everything that it can determine is
useful, and then keeps it, and then the rest is automatically removed
in the process of copying the good data.
"good" data (or live data) is discovered by doing a recursive search
through references in the root set (all global and stack variables).
So, as long as the middle nodes in the linked list are not reachable
from anything, then they will be discarded.

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
Monique Y. Mudama - 01 Nov 2005 23:33 GMT
> Java's garbage collector is actually a misnomer. It's more of a
> non-garbage collector. It finds everything that it can determine is
[quoted text clipped - 5 lines]
> So, as long as the middle nodes in the linked list are not reachable
> from anything, then they will be discarded.
I thought the precise implementation of the GC was left to the
discretion of the implementer ...

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Chris Smith - 01 Nov 2005 23:48 GMT
> I thought the precise implementation of the GC was left to the
> discretion of the implementer ...
It is. I don't know a lot about the implementation of garbage
collection in Sun's reference implementation of the JVM... but modern
high-performance garbage collectors in general tend to split things up
in this way:
1. New objects that are smaller than some threshold size are allocated
in the "nursery". New objects larger than that threshold size are
allocated in a "large object area" (skip to #4).
2. The nursery is generally copy-collected. When an object survives the
nursery, it is moved to the next intermediate generation, which is
probably also copy-collected.
3. Eventually, after some number of copy-collected generations, there is
a mark/sweep generation which is sometimes called "oldspace". Objects
live there for the remainder of their lives.
4. Large objects never enter any copy-collected generation. They are
allocated either directly to oldspace, or to the large object area
(LOA). In either case, they are not copy-collected at all.
So it's most likely a good deal more complicated than Benji said. And
yes, it's all undefined, so there's likely to be a good deal of
variation in the implementation between virtual machines.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Benji - 01 Nov 2005 23:50 GMT
> I thought the precise implementation of the GC was left to the
> discretion of the implementer ...
Well, you could implement GC as a stop-and-copy collector, or an incremental
collector, but you're guarenteed that liveness is the criteria for whether
or not something is considered garbage. There are API utilities for
tweaking the GC that utilize this. (e.g. WeakReference / StrongReference)

Signature
Of making better designs there is no end,
and much refactoring wearies the body.
Chris Smith - 01 Nov 2005 23:53 GMT
> Consider a doubly-linked list holding a handful of nodes. If the list
> is cleared by nulling the head and tail references, is the GC
> intelligent enough to dispose of all of the nodes?
Yes.
In very old versions of Java from Sun (and possibly in some non-Sun
implementations of the JVM) there might be a potential problem due to
conservative collection. Doubly linked lists or other large cycles are
a sort of worst-case scenario for conservative collectors, since any
accidental retention will cause all the remaining nodes to be retained
as well. However, you're unlikely to find a conservative collector in a
production-quality modern virtual machine for JavaSE or JavaEE. I'm
unsure whether conservative collectors are in use in ME environments.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation