using D without GC

Oleg B via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 7 03:16:35 PDT 2015


Hello. I want to try use D without GC and I'm not sure what I do 
all right.

import core.memory;

version( gcdis ) enum gc_disable = true;
else             enum gc_disable = false;

class Foo
{
     byte[][] buf;
     this()
     {
         foreach( i; 0 .. 32 )
             buf ~= new byte[]( 65536 * 16 );
     }
     ~this()
     {
         static if( gc_disable )
         {
             // no warning about deprecated 'delete'
             foreach( i; 0 .. 32 )
                 delete buf[i];
             delete buf;
         }
     }
}

void main()
{
     static if( gc_disable ) GC.disable();

     foreach( i; 0 .. 200 )
         scope foo = new Foo;

     static if( gc_disable )
     {
         GC.enable();
         GC.collect();
     }
}

dmd -version=gcdis test.d && ./test "--DRT-gcopt=profile:1"
	Number of collections:  2
	Total GC prep time:  0 milliseconds
	Total mark time:  0 milliseconds
	Total sweep time:  0 milliseconds
	Total page recovery time:  0 milliseconds
	Max Pause Time:  0 milliseconds
	Grand total GC time:  0 milliseconds
GC summary:   51 MB,    2 GC    0 ms, Pauses    0 ms <    0 ms

dmd test.d && ./test "--DRT-gcopt=profile:1"
	Number of collections:  205
	Total GC prep time:  1 milliseconds
	Total mark time:  1 milliseconds
	Total sweep time:  5 milliseconds
	Total page recovery time:  0 milliseconds
	Max Pause Time:  0 milliseconds
	Grand total GC time:  8 milliseconds
GC summary:   69 MB,  205 GC    8 ms, Pauses    3 ms <    0 ms

I use this feature correctly?


More information about the Digitalmars-d-learn mailing list