Array Capacity Field
Sean Kelly
sean at invisibleduck.org
Wed May 7 19:06:02 PDT 2008
dsimcha wrote:
> According to the Phobos docs, std.gc.capacity "Returns capacity (size of the
> memory block) that p points to the beginning of. If p does not point into the gc
> memory pool, or does not point to the beginning of an allocated memory block, 0 is
> returned." I had looked through this before.
Right. And since resizing an interior slice "in place" is typically
undesirable, this does exactly what you want. If you really want the
capacity regardless of this however, you can use
tango.core.Memory.GC.query(), which returns the size of the block even
for interior pointers.
> It also doesn't appear that writing my own function to do this works:
>
> void capacity(T)(ref T[] array, uint newCapacity) {
> if(newCapacity < array.length) {
> array.length=newCapacity;
> }
> std.gc.realloc(array.ptr, T.sizeof*newCapacity);
> }
>
> When I use this after allocating a huge array, and then run a fullCollect() and a
> minimize(), the memory usage of my programs appear not to go down.
This is somewhat of a different issue. The Phobos GC never returns any
memory to the OS. It's also possible to make a Phobos app run out of
memory simply by looping on an append op (if I remember the example
correctly). The Tango GC behaves a bit differently, and will actually
return memory to the OS in certain circumstances.
Sean
More information about the Digitalmars-d
mailing list