1
我有一個簡單的測試程序來嘗試...內存泄漏
object ActorLeak extends App {
val system = ActorSystem("ActorLeak")
val times = 100000000
for (i <- 1 to times) {
val myActor = system.actorOf(Props(classOf[TryActor], i), name = s"TryActor-$i")
//Thread sleep 100
myActor ! StopCmd
if (i % 10000 == 0)
println(s"Completed $i")
}
println(s"Creating and stopping $times end.")
val hookThread = new Thread(new Runnable {
def run() {
system.shutdown()
}
})
Runtime.getRuntime.addShutdownHook(hookThread)
}
case object StopCmd
class TryActor(no: Int) extends Actor {
def receive = {
case StopCmd => context stop self
}
}
我發現:一段時間的OutOfMemoryError,有時會使JVM死了,跑慢點慢點......
是否有記憶泄漏創建/停止演員?
我把你的for循環重寫爲一個遞歸函數,認爲它可能是咀嚼內存的'1 to times'範圍的分配;但是我仍然看到隨着時間的推移內存使用量的顯着增長(類似於您的結果)。 –