beforeGarbageCollection

Michel Fortin michel.fortin at michelf.com
Thu Jun 17 10:02:48 PDT 2010


On 2010-06-17 12:15:58 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

>> I'm also wondering if the idea of using a *callback* for this is really 
>> a good idea. If you're in the process of updating a cache, you call 
>> 'new', it trigger the garbage collector which reentrantly asks your 
>> cache to clear itself, won't this easily become trouble? Apple use an 
>> event for this on the iPhone, which is handled by the event loop of the 
>> application; perhaps a low memory *message* could be sent to all the 
>> threads that are listening to the message passing system, this would 
>> avoid the reentrancy issue and would work better with multithreading.
> 
> I was thinking new would throw if called from within a callback. These 
> are known solutions.

I wasn't thinking about calling 'new' from within the callback. The 
problem is more that you have to deal with reentrancy while you work in 
the cache data structure.

For instance, let's say I use a AA for a cache. Then I add an element 
to the cache. Adding one element might call the GC to get more memory, 
so it has the potential to call the low memory callback every time you 
add something to it. So while implementing the AA you must be extra 
careful so that the data structure invariants for the cache holds at 
all times, even in the middle of a function, when you have a line of 
code that may call new (directly or indirectly), and you must recheck 
your assumptions about that structure after every memory allocation 
because the callback might have tampered with it. That sounds 
troublesome... it's akin to a race condition, but with a single thread.

I won't say the callback can't be useful. But if I were using it, I 
wouldn't do any work in the callback. Instead, I would raise a flag 
somewhere that the program can check the next time it enters into a 
state where it's safe to clear the caches. In an event-driven 
application, that would be the event loop.

In a program with multiple threads, a good strategy might be to ask 
idle threads to clear their thread-local caches instead of the one 
which is currently working. Having a callback could enable such a 
strategy, and many others, so I'll admit it's a good idea. But it 
should be used with caution.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list