A program leaking memory.
Thomas Kuehne
thomas-dloop at kuehne.cn
Mon Mar 13 09:02:32 PST 2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Johan Grönqvist schrieb am 2006-03-13:
> I have now transcribed a program from c++ to D, but when I run it, it
> uses all memory and crashes.
>
> I try to minimise a function of many variables iteratively using a
> steepest descent method. The iteration steps are performed in a function
> named step.
>
> Step uses two vectors, and these are allocated anew in each step. After
> the step-function has finished, I expected these vectors to be
> garbage-collected and disappear.
>
> I allocate vectors with:
> real[] grad;
> grad.lenth=1000;
> for (uint ix = 0; ix<grad.lenth; ix++) grad[ix] = gradient(ix);
>
> I the use the vectors but do nothing to deallocate them or indicate that
> I am done using them.
>
> Memory usage grows until the program need too much memory. My
> interpretation is that new vectors are allocated at each iteration, but
> the old vectors are not freed.
>
> The behaviour is the same using DMD and GDC.
Does grad.length increase with each step call? (potential fragmentation)
Is step an anonymous function or is it nested in an other function?
Can you replace
real[] grad;
grad.length = 1000;
with
real[1000] grad;
? (depends on you problem and your design)
Neither the internal GC stats nor external tools did show any GC
misbehaviour:
# import std.random;
# import std.gc;
# import std.stdio;
#
# real gradient(size_t x){
# return x * rand();
# }
#
# void step(){
# real[] grad;
# grad.length = 1000;
#
# for(size_t ix = 0; ix < grad.length; ix++){
# grad[ix] = gradient(ix);
# }
# }
#
# int main(){
# size_t i = 0;
# GCStats gc;
# while(i++ < i.max){
# step();
# getStats(gc);
# writefln("[%s] pool:%s used:%s free:%s FREE:%s PAGE:%s",
# i, gc.poolsize, gc.usedsize, gc.freelistsize,
# gc.freeblocks, gc.pageblocks);
# }
# return 0;
# }
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFEFbLp3w+/yD4P9tIRAoQEAJ93ePekWy3TTnPNBSVt48RtPqhRCwCfb+rF
oO+/3cS25HMU8AYkwNFyfQ8=
=5q+D
-----END PGP SIGNATURE-----
More information about the Digitalmars-d-learn
mailing list