Re: Garbage collection in C++
"Matthias Buelow" <mkb@incubus.de> wrote in message
news:6oj5jvF3us0aU1@mid.dfncis.de...
Juha Nieminen wrote:
I have worked in a project where the amount of calculations performed
by a set of programs was directly limited by the amount of available
memory. (If the program started to swap, it was just hopeless to try to
wait for it to finish.)
You seem to think that a program that uses GC uses more memory, which is
just false in the general case.
Well, memory only gets reclaimed when the GC "decides" to run a scan. So, if
the program is making frequent allocations, and the GC does not run enough
scans to keep up, it has no choice to keep expanding its internal heaps.
// GC world
struct foo {
void do_something();
};
void thread() {
foo* f;
for (;;) {
f = new foo();
f->do_something();
}
}
int main() {
// create 32 threads
reutrn 0;
}
// Manual world
struct foo {
void do_something();
};
void thread() {
foo* f;
for (;;) {
f = new foo();
f->do_something();
delete f;
}
}
int main() {
// create 32 threads
reutrn 0;
}
Why should it use more? 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.
Which program is going to use more memory? The Manual world, there can only
ever be up to 32 foo objects at a time. In the GC worlds, well, there can
potentially be hundreds, or thousands, in between GC scan intervals...