Not necessarily. If say 50% of the tenured objects eventually are
collected, then you only have to copy the equivalent of those 50% of
objects to fill the holes they once occupied.
Okay. Firstly, no: that would leave any surplus nursery survivors
scattered throughout the nursery, which would mean you hadn't collected
the nursery. You need to move those ones too, so you can reclaim the
nursery for fresh allocation.
Secondly, no: that would require the GC to know where the free space is,
ie to maintain a free list like a traditional C malloc/free system, and
modern GCs don't do that. At least, as far as i know - am i wrong?
You may not end up with 100% memory compaction (there may be small holes
left over that you can't fill because the new objects aren't exactly the
same size as the old ones) but I'm not sure that 100% compaction should
be the goal either.
The problem is not leaving spaces in the older generation - that's fine,
they'll get squeezed out when that generation eventually gets collected -
but rather leaving live objects scattered throughout the nursery. That
clobbers reuse of the nursery space, which is what you're trying to
accomplish.
To bring this discussion full circle, it seems to me that the .NET
garbage collector was brought up because it's an excellent balance
between "do some compaction" and "grow the heap." Unlike Sun's GC, it
works very well with a strategy to grow the heap so an app can use all
available memory, with out suffering heavy compaction algorithms.
Really? I think i've got a handle on what the .NET GC does, based on
Peter's explanation, and it does just as much compaction as a traditional
generational GC (which is what i'm assuming Sun's is). The difference is
that a traditional system has a fixed-size older generation, which fills
up and must then be collected, whereas .NET has one which grows over time,
and can be collected at some arbitrary time. The traditional system bounds
memory use, but can involve frequent collections of older-space; the .NET
approach gobbles up more and more memory if you don't collect frequently
enough, but lets you spend less time collecting.
I can image that .NET's approach is more suited to the desktop, where you
typically have one app active at a time, and it's fine to let that app
grow to consume all available memory, because nobody else needs it. It
would perhaps be less suitable for servers running several services, where
you'd like the apps to play nice and use limited, fixed amounts of memory.
It's an answer to Sun's protestations of "we can do this, but it's
complicated." No, the proper algorithm is simple.
I'm sure the computer scientists who have spent decades developing the art
of garbage collection will be pleased to hear that.
tom
--
Imagine a city where graffiti wasn't illegal, a city where everybody
could draw wherever they liked. Where every street was awash with a
million colours and little phrases. Where standing at a bus stop was never
boring. A city that felt like a living breathing thing which belonged to
everybody, not just the estate agents and barons of big business. Imagine
a city like that and stop leaning against the wall - it's wet. -- Banksy