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

New Post: I get memory leaks without running CollectGarbage

$
0
0
Hi Stefan,

A few thoughts:

  • Calling CollectGarbage() every time is almost certainly overkill. Yes, it causes the script engine to release managed objects it's no longer holding, but those objects will stick around until the managed garbage collector kicks in, and that's very unlikely to happen at the end of each loop iteration unless you invoke it explicitly, especially if we're talking about a tight loop. Instead, consider calling CollectGarbage() periodically, or based on a schedule that makes sense for your application - possibly something as simple as calling it once for every n loop iterations.
  • Be aware also that unmanaged proxies will pile up within the script engine unless you call CollectGarbage() at some point; that's unavoidable. V8 will clean them up eventually, but we've found that when it comes to garbage collection, V8 is lazy enough to get itself into trouble - a sort of "garbage thrashing" state that kills performance.
  • If you really need the script engine to release an individual managed object that you've passed into it - something that makes sense for very large objects, for example - then consider exposing it indirectly. In this case, instead of passing a large list, pass a tiny forwarding wrapper that implements the same IList<int> interface. When you're done, reset the wrapper's internal reference and the large list is ready to be collected. The script engine never touches it at all.
Cheers!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>