Large Arrays and GC

dsimcha dsimcha at yahoo.com
Fri May 9 15:03:24 PDT 2008


This appears to be analogous to simly setting the length field to zero in a
dynamic array.  It just nulls the pointers and still requires the GC to know to
free the memory properly.  On the other hand, delete() for dynamic arrays is truly
manual memory management and actually frees the memory in question.  The following
test case illustrates my point, and fails after a few iterations.

void main () {
    uint count;
    while(true) {
        test();
        fullCollect();
        minimize();
        writefln(++count);
    }
}

void test() {
    string[uint] foo;
    for(uint i=1; i < 10_000_000; i++) {
        foo[i]="Some ridiculously random text foo foo foo foo foo foo foo foo foo";
    }
    clear(foo);
}

Note:  clear() is clear function copied and pasted from Bearophine.

== Quote from bearophile (bearophileHUGS at lycos.com)'s article
> dsimcha Wrote:
> > Does anyone know of another way to manually delete associative arrays?
> Time ago I have asked that question, and someone has shown me a way, you can
find a modified version of it as the clear() function in my libs:
> http://www.fantascienza.net/leonardo/so/libs_d.zip
> T clear(T)(ref T items) {
>     static if (IsAA!(T)) { // This only works with the current AA implementation
>         struct BB {
>             void*[] b;
>             size_t nodes;
>         }
>         union ToPtr_clear {
>             T x;
>             void* ptr;
>         }
>         ToPtr_clear toptr;
>         toptr.x = items;
>         BB* b = cast(BB*) toptr.ptr;
>         if (b) {
>             b.b = null;
>             b.nodes = 0;
>         }
>         return items;
>     } else static if (IsDynamicArray!(T)) {
>         items.length = 0;
>         return items;
>     } else
>         static assert(0, "clear() accepts only AAs and dynamic arrays.");
> }
> You can find unittests, ddocs and the IsAA!() into those libs, the "func" module
(the IsAA isn't necessary if you just want to use it for AAs).
> Bye,
> bearophile




More information about the Digitalmars-d mailing list