A program leaking memory.
Nick
Nick_member at pathlink.com
Sat Mar 18 00:46:30 PST 2006
Johan Gronqvist spake thusly:
>Is there a way to tell the gc never to use more than (say) 100MB of memory?
Nope, not at the moment, but it would be nice if there was.
>The arrays I initialize in this way are never again resized. With this I
>mean that the two first things I do are
>
>real[] grad;
>grad.lenth = myLength;
>
>thereafter I modify the elements and read off their values a lot, but I
>never resize the array again, i.e., the memory need for grad should
>never change during a step, and as soon as one step ends and the next
>starts the old grad-vector is no loner needed.
Whenever you have such a planned memory usage (ie. allocate a fixed array, use
once, discard, then repeat many times), it isn't terribly efficient to rely on
the GC to pick up all your discarded memory. But that's OK, D doesn't force you
to.
The first thing I would try is to put the array outside the step() function,
like this:
# read grad[];
#
# void step()
# {
# grad.length = whatever;
# ... // Use it here
# }
This will resize the _same_ array over and over, instead of creating a new one
each time. This will often (but not always) resize in place, thus saving memory.
Especially in situations like this:
# ...
# grad.length = 100000; // Big allocation
# ...
# grad.length = 10; // Resize does not move array
#
# grad.length = 100000; // The already array points to a block large
# // enough to hold this data, so it does not move.
Thus this allocates a total of 100000 reals, not 200010.
Hope this helps. If not there are other more advanced techniques one can use.
Nick
More information about the Digitalmars-d-learn
mailing list