new Type[count] takes too much?
Jonathan M Davis
jmdavisProg at gmx.com
Thu Oct 31 03:11:51 PDT 2013
On Thursday, October 31, 2013 10:59:48 Namespace wrote:
> On Thursday, 31 October 2013 at 09:48:23 UTC, safety0ff wrote:
> > On Thursday, 31 October 2013 at 09:15:53 UTC, Namespace wrote:
> >> I'm sure we had already this conversation but I don't find the
> >> thread.
> >>
> >> T[] buffer = new T[N]; assumes more space than stated (in
> >> average 2010 elements more. See:
> >> http://dpaste.dzfl.pl/af92ad22c). It behaves exactly like
> >> reserve and that is IMO wrong. If I reserve memory with
> >> buffer.reserve(N), I want to have at least N elements. That
> >> behaviour is correct. But if I use new T[N] I mostly want
> >> exactly N elements and no extra space.
> >>
> >> Thoughts?
> >
> > To me it looks like it is derived directly from the way the GC
> > allocates chunks:
> > Next power of two if less than 4096 otherwise some multiple of
> > 4096.
> >
> > Unless you modify the GC, this behaviour is present whether you
> > can see it or not (http://dpaste.dzfl.pl/5481ffc2 .)
>
> Maybe (and hopefully) I'm wrong, but it seems that the static
> array is on the heap?
T[] buffer = new T[N];
is a dynamic array. Now, in the code in your dpaste link, you have a static
array which is in a struct which is on the heap, so that's different, but now
you're dealing with the amount of memory that gets allocated on the heap for
the struct, and as the GC is going to allocate in multiples of 2, more than
the size of the struct is going to be allocated. It might be that some of the
memory beyond the end of the struct might actually be used for another object
rather than the struct using the memory block by itself (I'm not sure what the
GC does in that regard), but it's definitely going to allocate in multiples of
2, so if the GC has to go up another multiple of 2 to make the struct fit,
it'll go up another multiple of 2.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list