ironpython2 icon indicating copy to clipboard operation
ironpython2 copied to clipboard

OutOfMemory!!!How to Use CreateEngine or ScriptRuntime

Open jol5 opened this issue 4 years ago • 5 comments

for(var i = 1;i<1000;i++){
  var engine = Python.CreateEngine();
  var scope = engine.CreateScope();
  
  var script = engine.CreateScriptSourceFromString(scriptTxt);
  var result = script.Execute(scope);
}      

the code will cause the memory to rise quickly and not reclaim; i found a lost of functionDefine instance act.

image

jol5 avatar Jan 07 '21 09:01 jol5

You're going to have to provide more information. When I tried it with I did not notice anything like what you're describing. What version are you using? What sort of code appears in scriptTxt?

slozier avatar Jan 07 '21 14:01 slozier

You're going to have to provide more information. When I tried it with I did not notice anything like what you're describing. What version are you using? What sort of code appears in scriptTxt?

netcore 3.1 lastest IronPython

when i add this line of code whichis " import os " ,the bug will review. import os .....

jol5 avatar Jan 09 '21 03:01 jol5

You're going to have to provide more information. When I tried it with I did not notice anything like what you're describing. What version are you using? What sort of code appears in scriptTxt?

when i add this line of code whichis " import os " ,the bug will review. import os .....

as the same time , urllib,urlib2,os,rpdb,random,collections ..... more

jol5 avatar Jan 09 '21 03:01 jol5

Thanks for the additional information. It looks like everything is being kept alive via IronPython.Compiler.Ast.StorageData.

The only suggestion I have would be to reuse the engine instead of creating one every time:

var engine = Python.CreateEngine();
for (var i = 1; i < 1000; i++) {
    var scope = engine.CreateScope();
    var script = engine.CreateScriptSourceFromString("import os");
    var result = script.Execute(scope);
}   

slozier avatar Jan 18 '21 20:01 slozier

I have the same problem. Sometimes it is not possibile to reuse the engine because, as far as I know, it caches import modules and the only way to invalidate cache is to create a new engine and dispose the previous one (but this leads to huge memory usage never reclaimed).

Andrea

apierini avatar May 27 '21 14:05 apierini