Usage of memory by arrays

Steven Schveighoffer schveiguy at yahoo.com
Thu Apr 5 21:11:53 UTC 2018


On 4/5/18 4:58 PM, unDEFER wrote:

> It prints:
> $ ./memory
> float = 4 bytes
> float[3] = 12 bytes
> 100K * float[3] = 2356 Kbytes
> 
> Why not 1200 Kbytes?

Array appending is complex.

As you append, it continually "fills in" the memory block you have. But 
once it outgrows that block, it needs to allocate and move everything to 
a new bigger block.

But the old block doesn't go away! It's collected and stored in a free 
list for future allocations. Since you are only ever growing your block, 
and not allocating smaller ones, it never gets to use those free list 
blocks which are too small. Normal non-pathological programs are going 
to use that memory for all kinds of things, so your overall memory 
should stabilize at some level.

-Steve


More information about the Digitalmars-d mailing list