cgy*_*per 8 .net ironpython memory-leaks
我需要一些帮助来找到我遇到的内存泄漏的解决方案.我有一个C#应用程序(.NET v3.5),允许用户运行IronPython脚本进行测试.脚本可以加载Python标准库中的不同模块(包含在IronPython二进制文件中).但是,当脚本完成时,分配给导入模块的内存不会被垃圾回收.循环使用一个脚本的多次运行(完成压力测试)会导致系统在长期使用期间耗尽内存.
这是我正在做的简化版本.
脚本类主要功能:
public void Run()
{
// set up iron python runtime engine
this.engine = Python.CreateEngine(pyOpts);
this.runtime = this.engine.Runtime;
this.scope = this.engine.CreateScope();
// compile from file
PythonCompilerOptions pco = (PythonCompilerOptions)this.engine.GetCompilerOptions();
pco.Module &= ~ModuleOptions.Optimized;
this.script = this.engine.CreateScriptSourceFromFile(this.path).Compile(pco);
// run script
this.script.Execute(this.scope);
// shutdown runtime (run atexit functions that exist)
this.runtime.Shutdown();
}
Run Code Online (Sandbox Code Playgroud)
加载随机模块的示例'test.py'脚本(添加~1500 KB的内存):
import random
print "Random number: %i" % random.randint(1,10)
Run Code Online (Sandbox Code Playgroud)
一种循环机制,会导致系统内存不足:
while(1)
{
Script s = new Script("test.py");
s.Run();
s.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
我添加了一节以不根据我在此线程中找到的内容优化编译,但内存泄漏发生在任何一种方式.将显式调用添加到s.Dispose()也没有任何区别(如预期的那样).我目前正在使用IronPython 2.0,但我也尝试过升级到IronPython 2.6 RC2而没有任何成功.
当脚本引擎/运行时超出范围时,如何将嵌入式IronPython脚本中导入的模块像普通.NET对象一样进行垃圾收集?
使用Iron Python 2.6 RC 2和C#3.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
namespace IPmemTest {
class IPy {
private string script = "import random; random.randint(1,10)";
public IPy() {
}
public void run() {
//set up script environment
Dictionary<String, Object> options = new Dictionary<string, object>();
options["LightweightScopes"] = true;
ScriptEngine engine = Python.CreateEngine(options);
ScriptRuntime runtime = engine.Runtime;
ScriptScope scope = runtime.CreateScope();
var source = engine.CreateScriptSourceFromString(this.script);
var comped = source.Compile();
comped.Execute(scope);
runtime.Shutdown();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的循环是
class Program {
static void Main(string[] args) {
while (true) {
var ipy = new IPy();
ipy.run();
}
}
}
Run Code Online (Sandbox Code Playgroud)
内存使用量增加到大约70,000K,但随后趋于平稳.
| 归档时间: |
|
| 查看次数: |
4424 次 |
| 最近记录: |