akka.pattern.after:内存泄漏

时间:2016-06-28 03:25:36

标签: scala memory-leaks akka thread-dump

以下代码导致我的Play Scala Web服务出现内存泄漏。当我接受threadDump时,我注意到PluginTimeout线程一直在等待,永远不会被执行。

  override final def execute:Future[Any] = {
    val f1 = executePlugin // this returns a future

    val system = ActorSystem("PluginTimeout")
    val timeoutFuture = akka.pattern.after(timeout, system.scheduler) { throw new TimeoutException(s"{$name} timed out")  }
    Future.firstCompletedOf(f1 :: timeoutFuture :: Nil)
  }

这就是我的线程转储的样子。在进行了几次threa转储之后,我注意到每个后续线程转储中PlugintTimeout对象的数量不断增加。

"PluginTimeout-akka.actor.default-dispatcher-2" #2308 prio=5 os_prio=31 tid=0x00007fa02c84f800 nid=0x88707 waiting on condition [0x00000001803a3000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000007ab6f5d68> (a akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinPool)
        at scala.concurrent.forkjoin.ForkJoinPool.scan(ForkJoinPool.java:2075)
        at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
        at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

   Locked ownable synchronizers:
        - None

"PluginTimeout-scheduler-1" #2307 prio=5 os_prio=31 tid=0x00007fa02b34f800 nid=0x80f07 sleeping[0x000000017f16d000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at akka.actor.LightArrayRevolverScheduler.waitNanos(LightArrayRevolverScheduler.scala:81)
        at akka.actor.LightArrayRevolverScheduler$$anon$4.nextTick(LightArrayRevolverScheduler.scala:260)
        at akka.actor.LightArrayRevolverScheduler$$anon$4.run(LightArrayRevolverScheduler.scala:230)
        at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
        - None

如果我从我的方法中删除了Future timeout逻辑,我没有看到任何内存泄漏问题。可能会发生什么。一种可能的解释是,如果另一个未来成功并且一直在等待永恒,那么timeoutFuture永远不会被执行。这会导致几个期货堆积并占用内存。关于最新情况的任何线索?

1 个答案:

答案 0 :(得分:4)

您正在为n执行调用创建大量ActorSystems(1系统)。

ActorSystem相当繁重,因为它包含配置ThreadPools等。

您应该使用def execute()(implicit system: ActorSystem) ActorSystem并在逻辑中使用它。 例如,通过隐式传递它:foo

相关问题