using D without GC

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 7 03:23:15 PDT 2015


On 7/06/2015 10:16 p.m., Oleg B wrote:
> 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?

Don't worry about collecting at the end. The OS will clean up the app no 
matter what.

You shouldn't be using delete or new for that matter.

You should be using malloc + free. And emplace.

Also you don't need to enable the GC before telling it to collect.


I can't really help with emplace usage.
But for the record the GC isn't going to slow you down here.
No just reserve some memory and preallocate the buffer you want before 
using it. It'll be a lot faster and cheaper.


Concatenation is your real enemy here. Aka realloc.


More information about the Digitalmars-d-learn mailing list