On Tue, Dec 1, 2009 at 11:09 PM, Bert Belder <
bertb...@gmail.com> wrote:
> process.memoryUsage() is absolutely necessary to check if the memory
> is getting full, but the question remains *when* it's time to check
> the memory usage and possibly start flushing cache entries. Obviously
> you could just check the memory usage on a fixed interval, but then
> there would be no way to account for the fact that the garbage
> collector may be about to free up memory as well, which could make the
> cache flush unnecessary.
>
> I think it would be better to sync the cache clearing with the garbage
> collector, so maybe a system event that is raised when the garbage
> collector starts or finishes a cycle is the way to go. Then you could
> just hook up an event that checks the memory usage just after a GC
> cycle.
Okay, sure.
In V8 there is already a hook to get a callback before or after a GC.
(SetGlobalGCPrologueCallback() and SetGlobalGCEpilogueCallback().)
This wouldn't be a bad thing to implement as a process event:
process.addListener("beforeGC", ...)
process.addListener("afterGC", ...)
For performance reasons, I don't want to add a C++ level callback
unless there are JS level listeners. But other than that, it should be
straightforward to implement.
I would accept a patch for this feature.