On Tue, May 4, 2010 at 11:30 AM, Marco Rogers <
marco....@gmail.com> wrote:
> I'm dealing with this right now in libxmljs. My lib will shortly be
> updated to work the way Buffer does. Killing the reference to the js
> object with delete does not force or even suggest garbage collection
> in v8. v8 has it's own internal heuristics for when it decides to run
> the GC. Unfortunately, external bindings like Buffer and libxmljs
> allocate memory *outside* of the v8 heap space. So the context
> doesn't care about that memory unless you tell it to.
>
> The way you take part in this heuristic is by letting v8 know how much
> memory you're allocating outside of v8. There is a function surfaced
> in the API for this. Buffer does this so if you allocate lots of
> buffer space, v8 will see this and the gc will run. If there are
> buffers that can be collected, their destructors will be called and
> the internal memory will be freed. But the delete operator has
> nothing to do with that. All this does is reset the variable
> reference. Hope that makes sense.
>
> One idea I thought of if people are concerned about this is a
> Buffer#destroy method. This would essentially free the internal
> buffer. There are concerns with this though. If it's an instance
> method, i.e. buf.destroy(), then you still have the js reference and
> if you try to access it, you'll get a seg fault. If it's a class
> method, Buffer.destroy(buf), that's a little better, but class methods
> have their own issues in terms of testibility and reasoning about
> external interactions. Could be useful in this narrow case though.
Making a destroy method is very dangerous. I suggest that if you're