delegates and heap usage

Simen kjaeraas simen.kjaras at gmail.com
Wed Dec 1 06:01:07 PST 2010


Franciszek Czekala <home at valentimex.com> wrote:

> I am not sure that this is a correct answer. 1600 bytes is just 100x2x8  
> bytes.
> Since delegates are fat (double) pointers this is probably just the  
> memory
> occupied by dlg variables themselves, not the memory to which they  
> point. Indeed
> adding an extra z variable in my code to the g() function and using it  
> in writeln
> does not change the output: it is still 1600 bytes.

Wrong. The delegates would be 100x2x4 bytes, as DMD only produces 32-bit
exes. The 16 bytes per delegate is because that's the smallest block the
GC will allocate.

Proof:


long recurse( long delegate() dg ) {
     long n = 1;
    long m = 0;
     long r = 0;
     long s = 0;
     long t = 0;
     return GC.sizeOf( dg.ptr ) + ( dg() ? recurse( { return dg() - n + m -  
r + s + t; } ) : 0 );
}

void main(){
     writeln( recurse( { return 100L; } ) );
}

Outputs 6400.

-- 
Simen


More information about the Digitalmars-d mailing list