Scala 捕获解释器运行中生成的函数后,JVM Perm Gen/System冻结

Scala 捕获解释器运行中生成的函数后,JVM Perm Gen/System冻结,scala,jvm,classloader,interpreter,Scala,Jvm,Classloader,Interpreter,我正在使用Scala动态嵌入,我认为通过在宿主程序的Function0中捕获位来保存对位的重新解释是明智的。大致如下: object Code def intp: IMain = ??? object Capture { private val vr = new ThreadLocal[() => Any] def apply[A](thunk: => A) { vr.set(() => thunk) } def res

我正在使用Scala动态嵌入,我认为通过在宿主程序的
Function0
中捕获位来保存对位的重新解释是明智的。大致如下:

object Code
  def intp: IMain = ???

  object Capture {
    private val vr = new ThreadLocal[() => Any]

    def apply[A](thunk: => A) {
      vr.set(() => thunk)
    }

    def result: () => Any = vr.get()
  }

  def capture(code: String): Future[() => Any] = future {
    blocking {
      intp.setExecutionWrapper("Code.Capture")
      intp.interpret(code)
      Capture.result
    }
  }
}
这“不知怎么”起作用,但在JVM崩溃后不久,基本上挂起,产生Perm Gen错误等。所以我必须做一些被禁止的事情。如果我不使用捕获机制,它会运行得很平稳(但我必须反复执行
exploration
方法,而不是保留编译后的函数),因此这方面没有什么问题

我试着运行step调试器,看看是否发现任何可疑的东西,但没有。在某个时候,程序完全冻结了。我还通过在主
capture
块上启动一个单独的编译线程来隔离futures案例,该块上有一个辅助
Promise
,它等待is完成,但没有改变任何东西

感谢您的帮助。可能与类装入器之类的东西有混淆?(我的
IMain
会覆盖
parentClassLoader
以访问主机类)


我有一个最终的直觉,也许我应该在
apply
方法(
writeObject
)中序列化函数,然后在主机中反序列化。但是
readObject
会生成一个

java.lang.ClassNotFoundException: $line1.$read$$iw$$iw$$anonfun$1
也许暗示了下面的问题