重复初始化Clearscript V8引擎时出现内存不足(GC问题?)

Łuk*_*asz 4 c# asp.net garbage-collection memory-leaks v8

我创建了一个基本的默认ASP.NET 5项目.我有一个创建的控制器

var engine = new V8ScriptEngine();
Run Code Online (Sandbox Code Playgroud)

并返回一些模拟json.当我刷新页面一定次数时,我得到了

堆设置中的致命错误

分配失败 - 处理内存不足

并跟踪堆栈跟踪

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at V8Isolate.Create(StdString* , V8IsolateConstraints* , Boolean , Int32 )
   at Microsoft.ClearScript.V8.V8IsolateProxyImpl..ctor(String gcName, V8RuntimeConstraints gcConstraints, Boolean enableDebugging, Int32 debugPort)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Microsoft.ClearScript.V8.V8Proxy.CreateImpl[T](Object[] args)
   at Microsoft.ClearScript.V8.V8IsolateProxy.Create(String name, V8RuntimeConstraints constraints, Boolean enableDebugging, Int32 debugPort)
   at Microsoft.ClearScript.V8.V8Runtime..ctor(String name, V8RuntimeConstraints constraints, V8RuntimeFlags flags, Int32 debugPort)
   at Microsoft.ClearScript.V8.V8ScriptEngine..ctor(V8Runtime runtime, String name, V8RuntimeConstraints constraints, V8ScriptEngineFlags flags, Int32 debugPort)
   at Microsoft.ClearScript.V8.V8ScriptEngine..ctor()
Run Code Online (Sandbox Code Playgroud)

我试着用内存来看dotMemory.每次刷新页面时,都会创建一个引擎,并向非托管内存添加2MB内存.当它达到某个限制时,它会如上所述崩溃.只要我在达到限制之前单击强制GC,内存就会下降,我可以再次使用该页面.

我的问题是:为什么GC首先不处理这个问题?在每次请求之后,可以处理对象,如果我强制GC它.我想如果我几乎内存不足但我可以用GC回收它会这样做.

我该如何解决这个问题?也许添加更多内存会有所帮助,但我也不知道如何做到这一点.如果GC永远不会清理那些物体,它无论如何都会破裂.

当我运行Kestrel(dnx web)并使用时,也会发生同样的情况IIS.我将框架设置为"dnx46"

这是我的dnx版本

$ dnx --version
Microsoft .NET Execution environment
 Version:      1.0.0-rc1-16231
 Type:         Clr
 Architecture: x86
 OS Name:      Windows
 OS Version:   10.0
 Runtime Id:   win10-x86
Run Code Online (Sandbox Code Playgroud)

ClearScript版本是 "ClearScript.V8": "5.4.3"

Bit*_*tex 6

简短版本:完成后需要处理每个脚本引擎.一种方便的方法是使用以下using语句:

using (var engine = new V8ScriptEngine()) {
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)

更长版本:每个V8实例都保留了大块地址空间.它们不会显示为已用内存,但在32位进程中,只需几十个实例就可以耗尽地址空间.该管理GC最终将清理这一切,而是因为它不能跟踪V8的地址空间保留,它并不急于这样做,因为它没有检测到任何内存压力.最终,您的内存使用率仍然很低,但V8不能再保留足够大的地址空间块,因此它会失败.