【发布时间】:2010-12-12 11:35:00
【问题描述】:
我需要一些帮助来解决我遇到的内存泄漏问题。我有一个 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();
}
加载随机模块的示例“test.py”脚本(增加约 1500 KB 内存):
import random
print "Random number: %i" % random.randint(1,10)
一种会导致系统内存不足的循环机制:
while(1)
{
Script s = new Script("test.py");
s.Run();
s.Dispose();
}
我根据在this 线程中发现的内容添加了不优化编译的部分,但无论哪种方式都会发生内存泄漏。添加对 s.Dispose() 的显式调用也没有任何区别(如预期的那样)。我目前正在使用 IronPython 2.0,但我也尝试升级到 IronPython 2.6 RC2,但没有任何成功。
当脚本引擎/运行时超出范围时,如何让嵌入 IronPython 脚本中的导入模块像普通 .NET 对象一样被垃圾收集?
【问题讨论】:
标签: .net memory-leaks ironpython