Wrestling the Garbage Collector.. And loosing..
Simen Haugen
simen at norstat.no
Wed Apr 30 09:13:37 PDT 2008
Hi.
I'm having a problem with my application using extremly much memory, and the
only way I'm able to free it is by deleting objects explicitly. I guess I'm
not understanding the GC right, but I thought that it should free all
objects without references to it when it's collecting, but my testing below
shows I'm doing something wrong.
// Test 1
auto str = new char[512];
delete str;
str = new char[512]; // OK. No new allocation
// Test 2
auto str = new char[512];
str = new char[512]; // New memory allocated. GC isn't run. Guess it's
expected behavior.
// Test 3
auto str = new char[512];
str = null;
str = new char[512]; // New memory allocated. GC isn't run. Guess it's
expected behavior.
// Test 4
auto str = new char[512];
str = null;
GC.collect;
str = new char[512]; // New memory allocated. Why?
// Test 5
auto str = new char[512];
str.length = 0;
GC.collect;
str = new char[512]; // New memory allocated. Why?
I would have thought Test 4 and 5 shouldn't allocate more memory either as I
remove the reference (or zero length) and run the GC, but it does.
I would also think Test 2 and 3 should make the GC reclaim memory when it is
run.
The same applies for classes too, but there I need to explicitly delete all
members in the destructor also..
And what does GC.minimize actually do? Even if I free an object and run
minimize, the application still uses the same amout of memory (or at least
it looks like it)
This is tested with dmd 1.028, tango ca. 0.99.5 (used trunk from back then)
and win xp.
At this point my program is basically useless because of this. You can run
it and restart it before the next run, but this limits its use very much. I
can fix it by explicity deleting objects when I don't need them, but I
really hope I don't have to.
I'm already using scope everywhere I can, and my tests with those shows it
helps a lot, but the application will still require a good deal of memory
and I need to limit this as much as I can.
I really hope someone could enlighten me on the correct usage of the GC
(I've read the garbage collector documentation and memory article on
digitalmars and the wikipedia article without getting much smarter)
More information about the Digitalmars-d-learn
mailing list