Search code examples
c++memoryembedv8embedded-v8

How to free up the memory claimed by V8?


An application I'm working on embeds V8 for scripting. To conserve memory, I want to shut down the scripting component when it is not needed, but for some reason the memory is never returned to the OS.

Here's what I tried:

1. Call Dispose() on the persistent context handle

context.Dispose();

2. Force a garbage collection

while (!v8::V8::IdleNotification());

None of that has any notable effect on the processes memory usage. I can clearly see how it's going up when a script claims memory, but it never goes down again.

I'm determining process memory usage with ps -o rss. I know that figuring out how much memory a process is using is not really possible without a profiler, but I do believe rss should go down when V8 lets go of the memory.


Solution

  • The OS may simply be not reclaiming memory (for performance reasons for example) even if your application has freed it correctly. The application heap may also be holding the memory in case you need it again quickly. Either way, if you're sure you're not leaking (try something like valgrind), I wouldn't worry about it.