On 11/09/13 15:13, monarch_dodra wrote:
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.

Just to clarify, is this buffer meant only for internal use in your function or is it meant to be externally accessed as well? I'd kind of assumed the former.

Either way, isn't it sufficient to have some kind of

    if (buf is null)
    {
        // allocate the buffer
    }

check in place? The basic model seems right -- at the moment when you need the buffer, you check if it's allocated (and if not, allocate it as needed); you indicate to the GC that it shouldn't collect the memory; you use the buffer; and the moment it's no longer needed, you indicate to the GC that it's collectable again.

It means having to be very careful to check the buffer's allocation status whenever you want to use it, but I think that's an unavoidable consequence of wanting a static variable that can be freed if needed.

The alternative I thought of was something like comparing the size difference between the currently-needed buffer and the last-needed buffer (... or if you want to be over-the-top, compare to a running average:-), and if the current one is sufficiently smaller, free the old one and re-alloc a new one; but that's a bit _too_ greedy in the free-up-memory stakes, I think.

Reply via email to