I think it might be worthwhile talking about the mark-and-sweep GC
that you had previously mentioned. Mark-and-sweep involves the GC
first looking at the main program and looking for object references
there, and "marking" them as valid. It then looks at the references
that those hold, marking them, and so on.
After it completes the marking, it then deletes the objects that
weren't marked, (since it has has a complete list of all the new'ed
objects), first calling finalize on the objects before removing the
memory.
Mark-and-sweep been in place since at least 1.1 (maybe before)..
"Adam Maass" <> wrote in message news:<>...
> "Novice" <> wrote:
> > "Adam Maass" <> wrote:
> > |
> > | "Novice" <> wrote:
> > | > Hi, I am doing some work with Java and C++ and am curious as to
> whether
> self
> > | > references in Java cause memory leaks.
> > | >
> > | > For example:
> > | >
> > | > class Test{
> > | > Object foo =this;
> > | > }
> > | >
> > | > If I instantiate Test, the reference count of any Test object will
> never
> > | > become zero (as it always keeps a reference to itself) so long as the
> foo
> > | > variable is never re-assigned.
> > | >
> > | > Does this present a problem for Java's garbage collection?
> > | >
> > |
> > | Garbage collection in Java does not use reference counting. Rather, most
> > | simple gc systems use a mark-and-sweep method.
> > |
> >
> > Is there somewhere I can find this documented?
>
> OK, this isn't exactly crystal-clear from the specs, but here's the
> definition of an object's lifecycle:
>
> http://java.sun.com/docs/books/jls/s...doc.html#74701
>
> And here's the definition of the term "reachable," which is part of the
> lifecycle definition:
>
> http://java.sun.com/docs/books/jls/s...doc.html#44762
>
>
> The short answer is that an object that refers to itself may be garbage
> collected if there are no references to it from any live thread.
>
> Java garbage collection does not use (a naive implementation of)
> reference-counting.
>
>
>
> -- Adam Maass