Disabling GC in D

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 21 21:15:13 PST 2016


On Thursday, 21 January 2016 at 23:06:55 UTC, Dibyendu Majumdar 
wrote:
> On Thursday, 21 January 2016 at 22:44:14 UTC, H. S. Teoh wrote:
>>> Hi - I want to be sure that my code is not allocating memory 
>>> via the GC allocator; but when shipping I don't need to 
>>> disable GC - it is mostly a development check.
>>> 
>>> I want to manage all memory allocation manually via 
>>> malloc/free.
>>
>> Just write "@nogc:" at the top of every module and the 
>> compiler will tell you if there's a GC allocation anywhere.
>>
>
> Thanks!

You can also compile with -vgc and it will tell you where gc 
allocations are taking place.

```
import std.stdio;
void main() {
     int[] ints = new int[](10);
     for(int i=0; i<15; ++i)
         ints ~= i;
     writeln("Here be GC");
}
```

Compiling the above produces the following messages from the 
compiler:

alloc.d(3): vgc: 'new' causes GC allocation
alloc.d(5): vgc: operator ~= may cause GC allocation


More information about the Digitalmars-d-learn mailing list