加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala.tools.nsc.interpreter内存泄漏

发布时间:2020-12-16 18:48:08 所属栏目:安全 来源:网络整理
导读:我需要使用 Scala实现DSL(域特定语言),并且此DSL将在Scala解释器(scala.tools.nsc.interpreter)中进行解释.问题是解释器使用的内存不断增加,直到抛出outOfMemory异常.每次我要执行一行DSL时,我都无法重新初始化解释器,因为解释器需要一段时间才能加载.我不能
我需要使用 Scala实现DSL(域特定语言),并且此DSL将在Scala解释器(scala.tools.nsc.interpreter)中进行解释.问题是解释器使用的内存不断增加,直到抛出outOfMemory异常.每次我要执行一行DSL时,我都无法重新初始化解释器,因为解释器需要一段时间才能加载.我不能只调用reset(),因为我需要跟踪解释器执行环境中的一些数据.

即使我执行以下操作,也会发生内存泄漏:

while (true)
{
 interpreter.interpret("println(4 + 5)")
}

过了一会儿,我会得到:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at ch.epfl.lamp.util.ByteArray.<init>(ByteArray.java:24)
    at ch.epfl.lamp.fjbg.JCode.<init>(JCode.java:47)
    at ch.epfl.lamp.fjbg.JExtendedCode.<init>(JExtendedCode.java:113)
    at ch.epfl.lamp.fjbg.FJBGContext.JCode(FJBGContext.java:122)
    at ch.epfl.lamp.fjbg.JMethod.<init>(JMethod.java:52)
    at ch.epfl.lamp.fjbg.FJBGContext.JMethod(FJBGContext.java:87)
    at ch.epfl.lamp.fjbg.JClass.addNewMethod(JClass.java:246)
    at scala.tools.nsc.backend.jvm.GenJVM$BytecodeGenerator.genMethod(GenJVM.scala:755)
    at scala.tools.nsc.backend.jvm.GenJVM$BytecodeGenerator$$anonfun$genClass$7.apply(GenJVM.scala:307)
    at scala.tools.nsc.backend.jvm.GenJVM$BytecodeGenerator$$anonfun$genClass$7.apply(GenJVM.scala:307)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
    at scala.collection.immutable.List.foreach(List.scala:45)
    at scala.tools.nsc.backend.jvm.GenJVM$BytecodeGenerator.genClass(GenJVM.scala:307)
    at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase$$anonfun$run$4.apply(GenJVM.scala:86)
    at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase$$anonfun$run$4.apply(GenJVM.scala:86)
    at scala.collection.mutable.HashMap$$anon$2$$anonfun$foreach$3.apply(HashMap.scala:102)
    at scala.collection.mutable.HashMap$$anon$2$$anonfun$foreach$3.apply(HashMap.scala:102)
    at scala.collection.Iterator$class.foreach(Iterator.scala:660)
    at scala.collection.mutable.HashTable$$anon$1.foreach(HashTable.scala:157)
    at scala.collection.mutable.HashTable$class.foreachEntry(HashTable.scala:190)
    at scala.collection.mutable.HashMap.foreachEntry(HashMap.scala:43)
    at scala.collection.mutable.HashMap$$anon$2.foreach(HashMap.scala:102)
    at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase.run(GenJVM.scala:86)
    at scala.tools.nsc.Global$Run.compileSources(Global.scala:953)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compileAndSaveRun(IMain.scala:756)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compile(IMain.scala:731)
    at scala.tools.nsc.interpreter.IMain$Request.compile(IMain.scala:873)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:576)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543)
    at com.ericsson.ims_dsl.IMS_Interpreter$$anonfun$e$1.apply(IMS_Interpreter.scala:50)
    at com.ericsson.ims_dsl.IMS_Interpreter$$anonfun$e$1.apply(IMS_Interpreter.scala:50)
    at scala.tools.nsc.interpreter.IMain.beQuietDuring(IMain.scala:200)

有人可以帮我解决这个问题吗?

解决方法

每次调用解释方法时,都会存储请求.因此,如果你尝试无限次地调用这种方法,你应该遇到麻烦.所以这不是一个真正的错误.

您可以通过增加JVM最大堆大小来增加崩溃前可能的迭代次数…您可能需要计算可能的调用次数,以检查它是否符合您的目标.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读