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

Posted by Peter on Stack Overflow See other posts from Stack Overflow or by Peter
Published on 2011-10-16T22:36:10Z Indexed on 2011/11/18 1:50 UTC
Read the original article Hit count: 383

Filed under:

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

© Stack Overflow or respective owner

Related posts about scala