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

Commented Unassigned: Fatal error in heap setup, Allocation failed - process out of memory [44]

$
0
0
Hi,

While investigating some memory related issues in my application, I managed to reproduce what seems to be a problem. The following minimal application terminates on my PC after ~50 cycles and prints the following to console:

```
#
# Fatal error in heap setup
# Allocation failed - process out of memory
#
```

```
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
using (V8ScriptEngine engine = new V8ScriptEngine())
{
Console.WriteLine("Created engine #{0}", i);
engine.Execute("function zz(){}");
var tmp = engine.Evaluate("zz");
}
}
}
}
```


Strangely, the memory usage doesn't seem high when it terminates.

I used both a Nuget package, and a manually built latest ClearScript + V8 with same results.

Any help will be greatly appreciated.
Thanks in advance again,
Ron

Comments: Hi Ron, ``` C# var tmp = engine.Evaluate("zz"); ``` This statement acquires a script object, and even though the script engine is disposed immediately thereafter, the corresponding unmanaged V8 runtime cannot be destroyed until the script object is also disposed or finalized. In most cases this is no big deal; the garbage collector eventually finalizes the script object and the V8 runtime is destroyed. The problem here is that the code creates additional V8 runtimes in a tight loop. V8 reserves a large block of address space for each one and quickly fills up your 32-bit process before the garbage collector can do its job. The result is a process that cannot allocate memory even though relatively little is actually in use. The solution is to dispose all managed references to the V8 runtime explicitly. Consider coding your loop body as follows: ``` C# using (V8ScriptEngine engine = new V8ScriptEngine()) { Console.WriteLine("Created engine #{0}", i); engine.Execute("function zz(){}"); var tmp = engine.Evaluate("zz"); using (tmp as IDisposable) { // do something with tmp } } ``` Please let us know if this resolves your issue. Thank you!

Viewing all articles
Browse latest Browse all 2297

Trending Articles