Garbage Collector profiling and the dynamic array reserve() function

Tony tonytdominguez at aol.com
Tue Oct 17 06:14:59 UTC 2017


Found this unanswered question on StackOverflow.

This program:

import std.stdio;

void add(ref int[] data)
{
     data ~= 1;
     data ~= 2;
}

void main()
{
     int[] a;
     writeln("capacity:",a.capacity);
     auto cap = a.reserve(1000); // allocated may be more than 
requested
     assert(cap >= 1000);
     assert(cap == a.capacity);
     writeln("capacity:",a.capacity);
     a.add();
     writeln(a);

}

compiled with "dmd -profile=gc"

has this output in profilegc.log

bytes allocated, allocations, type, function, file:line
               4	              1	int[] profiling.add profiling.d:8
               4	              1	int[] profiling.add profiling.d:7

The question is: why doesn't using reserve() cause an allocation 
to be shown?


More information about the Digitalmars-d-learn mailing list