开发者

Scala Interpreter scala.tools.nsc.interpreter.IMain Memory leak

开发者 https://www.devze.com 2023-04-13 04:52 出处:网络
I need to write a program using the scala interpreter to run scala code on the fly. The interpreter must be able to run an infinite amount of code without being restarted. I know that each time the me

I need to write a program using the scala interpreter to run scala code on the fly. The interpreter must be able to run an infinite amount of code without being restarted. I know that each time the method interpret() of the class scala.tools.nsc.interpreter.IMain is called, the request is stored, so the memory usage will keep going up forever. Here is the idea of what I would like to do:

var interpreter = new IMain
while (true)
{
    interpreter.interpret(some code to be run on the fly)
}

If the method interpret() stores the request each time, is there a way to clear the buffer of stored requests? What I am trying to do now is to count the number of times the method interpret() is called then get a new instance of IMain when the number of times reaches 100, for instance. Here is my code:

var interpreter = new IMain
var counter = 0
while (true)
{
    interpreter.interpret(some code to be run on the fly)
    counter = counter + 1
    if (counter > 开发者_开发技巧100)
    {
         interpreter = new IMain
         counter = 0
    }
}

However, I still see that the memory usage is going up forever. It seems that the IMain instances are not garbage-collected by the JVM.

Could somebody help me solve this issue? I really need to be able to keep my program running for a long time without restarting, but I cannot afford such a memory usage just for the scala interpreter.

Thanks in advance,

Pet


try calling the close method on the interpreter before reassigning

0

精彩评论

暂无评论...
验证码 换一张
取 消