Memory leak with dynamic array
Joseph Wakeling
joseph.wakeling at webdrake.net
Tue Apr 13 08:15:15 PDT 2010
Steven Schveighoffer wrote:
> Assuming you may ask questions about this, it's not an exact description
> of what happens. The append code and GC are smarter about it than this,
> I just wanted to explain it in an easy-to-understand way :) The real
> code uses algorithms to determine optimal grow sizes and can extend into
> consecutive blocks if possible.
I realised that while running some of the code that bearophile suggested
to me, because if you look at 'capacity' in a non-pre-reserved D array
that's appended to 5 million times, its capacity is only a little over 5
million -- compared to a C++ vector which is the nearest power of 2
(2^23, over 8 million).
The GC overhead means that in this case actual memory usage is a bit
higher than C++, but on a larger scale this could surely make a big
difference.
In the case of the piece of code I'm working on I don't think the
pre-reserve is really so important as far as performance goes -- the
append speed is a bit of a bugger, but the main issue is probably to do
with speed of copying and iterating across arrays.
For example, I suspect that the D array's,
x[] = 0;
y[] = z[];
is not as efficient as a C++ vector's
x.assign(x.size(),0);
y.assign(z.begin(),z.end());
I think I can find some ways round this by taking advantage of some of
the D arrays' cleverness, limiting the copying of values and instead
just swapping around which memory each array is pointing to.
As for iteration, I don't know to what degree D's foreach() across
arrays compares to for() commands in C++ -- I guess it should be pretty
close in performance, no?
Best wishes,
-- Joe
More information about the Digitalmars-d-learn
mailing list