Scala 解释器 scala.tools.nsc.interpreter.IMain 内存泄漏

发布于 2024-12-09 21:36:28 字数 811 浏览 0 评论 0原文

我需要使用 scala 解释器编写一个程序来动态运行 scala 代码。解释器必须能够运行无限量的代码而无需重新启动。我知道每次调用 scala.tools.nsc.interpreter.IMain 类的方法terpret()时,都会存储请求,因此内存使用量将永远持续上升。这是我想做的事情的想法:

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

如果方法terpret()每次都存储请求,是否有办法清除存储请求的缓冲区? 我现在想做的是计算调用terpret()方法的次数,然后当次数达到100时获取一个新的IMain实例。这是我的代码:

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
    }
}

但是,我仍然看到内存使用量永远在上升。看来 IMain 实例没有被 JVM 进行垃圾收集。

有人可以帮我解决这个问题吗?我确实需要能够让我的程序长时间运行而无需重新启动,但我无法仅仅为了 scala 解释器而承受这样的内存使用量。

预先感谢,

宠物

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情独悲 2024-12-16 21:36:28

在重新分配之前尝试在解释器上调用 close 方法

try calling the close method on the interpreter before reassigning

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文