J
James Kanze
You seem to think that a program that uses GC uses more
memory, which is just false in the general case.
It's certainly possible for a program to not use more memory
when garbage collection is used. If a copying collector is
used, it's even possible for it to use less, because there will
be no fragmentation. But typically, in order to use so little
memory, the collector must be run very, very often, which has a
decidedly negative impact on performance. Of course, a copying
collector could improve locality, thus reducing swapping. But
there are also applications where this won't make a difference.
Applications which systematically allocate a very few, large
arrays should probably not use garbage collection, at least not
"out of the box". (The Boehm collector has provisions for a
mixed model; you can allocate the large arrays outside of
garbage collection, and let the garbage collector take care of
the rest. But of course, this negates one of the advantages of
garbage collection; you now have to think about how memory is
going to be allocated and freed, using different strategies for
different objects.)
Why should it use more?
Because it does. That's been my experience, anyway.
I don't know enough about Juha's application, but there are
certainly applications which shouldn't use garbage collection,
and others which should use some sort of mixed model, or perhaps
require special tuning for garbage collection to be effective.
A lot of programs which calculate something and then terminate
don't really ever need to free anything. In which case, they
could use something a lot simpler than either malloc or garbage
collection. (Of course, if you never trigger the collector when
you're using garbage collection, garbage collection can be
incredibly fast.)
The amount of memory a program is using depends on the program
logic and its memory trace and not on how the memory is
managed, which is, as I said, an uninteresting implementation
detail.
That's not true when you start pushing towards the limits. In
the distant past, I've had programs which would run out of
memory using one implementation of malloc, and not with another.