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

我创建了一个基本的默认ASP.NET 5项目.我有一个创建的控制器var engine = new V8ScriptEngine();并返回一些模拟json.当我刷新页面一定次数时,我得到了Fatal error in heap setupAllocation failed – process out o...

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

var engine = new V8ScriptEngine();

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

Fatal error in heap setup

Allocation failed – process out of memory

并跟踪堆栈跟踪

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()

我试着用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

ClearScript版本是“ClearScript.V8”:“5.4.3”

解决方法:

简短版本:完成后,每个脚本引擎需要dispose个.一种方便的方法是使用using声明:

using (var engine = new V8ScriptEngine()) {
    // do stuff
}

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

本文标题为:c# – 反复初始化Clearscript V8引擎时出现内存不足(GC问题?)

基础教程推荐