Greedy memory handling

monarch_dodra monarchdodra at gmail.com
Wed Sep 11 06:13:22 PDT 2013


On Wednesday, 11 September 2013 at 11:19:27 UTC, Joseph Rushton
Wakeling wrote:
> 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 :-)

That's somewhat better, as it would allow the GC to collect my 
buffer, if it wants to, but I wouldn't actually know about it 
afterwards which leaves me screwed.

I *think* addRoot and removeRoot is really designed to pass GC 
memory to functions that aren't GC-scanned...


More information about the Digitalmars-d-learn mailing list