Greedy memory handling

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Wed Sep 11 04:19:18 PDT 2013


On 11/09/13 13:14, Joseph Rushton Wakeling wrote:
> On 11/09/13 12:34, monarch_dodra wrote:
>> But if the buffer is stored in a static variable, the GC will never collect it.
>> I *could* also free it myself, but why/when would I do that?
>>
>> Did you just just let your buffer grow, and never let it get collected?
>>
>> Is there a way to do something like "I'm using this buffer, but if you want to
>> collect it, then go ahead. I'll reallocate a new one *if/when* I need it again"
>
> How about GC.addRoot and GC.removeRoot ... ?

I should clarify that a bit more.  I mean, from what I understand, you want to 
be able to do something like this:

     void foo(/* vars */)
     {
         // 1. if buffer not allocated, allocate as necessary

         // 2. send GC a message: "Hey, I'm using this buffer!  Don't free!

         // 3. carry out your calculations

         // 4. send GC a message: "Hey, this buffer can be freed if you need to."
     }

If I understand right, GC.addRoot should take care of (2) and GC.removeRoot can 
take care of (3).  Then, if there's a collection cycle in-between calls to foo, 
fine; if not, next time you enter foo(), the new call to GC.addRoot will protect 
the memory for the lifetime of the calculation.

But this is conjecture, not speaking from experience :-)


More information about the Digitalmars-d-learn mailing list