From: | Jim Peters |
Date: | 22 Sep 2000 at 10:45:12 |
Subject: | Re: AMIOPEN: XML and Java, a uncharted territory? |
On Fri, Sep 22, 2000 at 08:31:35AM +0200, Juan Carlos Marcos Rodr�guez wrote:
> > I can't possibly tolerate everything stopping dead every
> > now and then for it to garbage-collect.
>
> This garbage collection issue is not a thing that can get done in the
> background, right? Why is it?
If you take the normal mark-and-sweep approach to garbage collection,
or one of those related techniques, then you have to stop everything
whilst you scan all allocated memory starting from references in
global variables picking up references and following them until you
have found all memory that is still in use. At this point you can
start everything going again, and toss out all the allocated memory
which was not referenced.
You have to stop everything, because otherwise the running code could
move a reference from unscanned memory into already-scanned memory,
meaning that the garbage collector wouldn't see it, and would
incorrectly free that chunk.
There are ways around this, but lots of trade-offs, and things start
getting really complex.
The other common way is reference-counting, which means that every
time you create a reference to an object, you increment an internal
counter in that object (its reference-count), and every time you drop
a reference to an object, you decrement its reference count. Any
object with a 0 reference count can be garbage-collected, either at
the moment that the count goes to zero, or by a parallel thread that
handles this.
The often-stated disadvantage of this method of garbage-collection is
that it does not garbage-collect circular lists and so on, where each
member references the next, so the counts never go to zero without
programmer intervention to break the chain. It also adds a bit of
extra code, but it does have the advantage of being suitable for
real-time applications. Unfortunately, I've yet to see a Java
implementation (for Linux at least) that uses it, and people seem to
see it as an out-dated approach - I honestly don't know why.
I hear there are ways around the circular-list problem, but only by
getting more complex again.
The link someone put up recently to the arstechnica site was
interesting because the guy was talking about making a difference in
the language between two types of references - ones that denote
ownership (and hence responsability for freeing memory), and ones that
are simply cross-links. The original Amiga had this notion of memory
ownership - in certain cases you could hand over the responsability
for a chunk to the OS or some other component, and other times you
took on the responsability for a chunk. Other times you were just
`borrowing' a chunk that someone else really owned, like when scanning
system lists.
I've yet to think through how having two types of references could
make garbage collection simpler. For this to work you'd need support
from your language, which means no-chance for Java, and maybe-possible
for GCC. The guy was making reference to stuff that the guy "Wouter"
had been doing regarding types of language that can clean up as they
go. It'd be interesting to see what he's come up with on this
subject.
Jim