Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: I get memory leaks without running CollectGarbage

0
0
Hi Stefan,

I find that if I do not collect garbage eventually I end up with a memory leak, what is it holding onto I assume the collection passed into the method?

The CLR and the script engine have independent object systems and garbage collectors. When you pass a .NET object to script code (or vice versa), a proxy is created on the receiving side that provides access to the actual object on the sending side.

By necessity, the proxy represents a reachable reference to the actual object and prevents it from being collected. Only when the proxy is collected does the actual object become available to its garbage collector, and only if no other references exist on either side.

The issue you're running into, most likely, is that V8's garbage collector is very lazy and relies on external triggers. Specifically, V8 needs the host to say, "Hey V8, now would be a good time to collect some garbage" (CollectGarbage(false) in ClearScript), or "Hey V8, I'm running low on memory, collect all the garbage you can no matter how long it takes" (CollectGarbage(true) in ClearScript).

running garbage collection impacts my performance, is there something else I can do?

Sometimes V8 can detect garbage collection opportunities (or emergencies) internally, but it seems to favor performance very heavily over memory efficiency without external prodding. Therefore it's up to the host to track its own condition (e.g., detect idleness, low-memory situations, etc.) and send the appropriate notifications via CollectGarbage().

Good luck!

Viewing all articles
Browse latest Browse all 2297