Greedy memory handling

Jacob Carlborg doob at me.com
Wed Sep 11 23:27:59 PDT 2013


On 2013-09-11 10:06, monarch_dodra wrote:
> I have a function that will *massively* benefit from having a persistent
> internal buffer it can re-use (and grow) from call to call, instead of
> re-allocating on every call.
>
> What I don't want is either of:
> 1. To set a fixed limitation of size, if the user ends up making
> repeated calls to something larger to my fixed size.
> 2. For a single big call which will allocate a HUGE internal buffer that
> will consume all my memory.
>
> What I need is some sort of lazy buffer. Basically, the allocation
> holds, but I don't want the to prevent the GC from collecting it if it
> deems it has gotten too big, or needs more memory.
>
> Any idea on how to do something like that? Or literature?

How about keeping a stack or static buffer. If that gets too small use a 
new buffer. When you're done with the new buffer set it to null to allow 
the GC to collect it. Then repeat.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list