首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >重复初始化Clearscript V8引擎时内存不足(GC问题?)

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

Stack Overflow用户
提问于 2016-05-23 09:30:13
回答 1查看 1.2K关注 0票数 4

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

代码语言:javascript
运行
复制
var engine = new V8ScriptEngine();

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

堆设置中的致命错误 分配失败-内存不足的进程

以及跟随堆栈跟踪

代码语言:javascript
运行
复制
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的ram添加到非托管内存中。当它达到一定的限制时,就会像上面解释的那样崩溃。只要在达到限制之前单击force,内存就会下降,我可以再次使用该页面。

我的问题是:为什么GC一开始就不处理这个问题?在每个请求之后,对象都可以被释放,如果我强制GC这样做的话。我会说,如果我几乎没有记忆,但我可以用GC收回它,它会这样做。

我怎么解决这个问题?也许添加更多的内存会有所帮助,但我也不知道如何做到这一点。如果GC永远不会清理那些对象,那么无论如何它都会中断。

在运行Kestrel (dnx web)和IIS时也会发生同样的情况。我的框架设置为"dnx46“

这是我的dnx版本

代码语言:javascript
运行
复制
$ 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"

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-23 11:02:34

简短的版本:您需要dispose每个脚本引擎,当你完成它。一种方便的方法是使用using语句:

代码语言:javascript
运行
复制
using (var engine = new V8ScriptEngine()) {
    // do stuff
}

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

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37387363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档