# Memory leak after actors are tried to be stopped

**URL:** <https://discuss.akka.io/t/memory-leak-after-actors-are-tried-to-be-stopped/8060>\
**Category:** Akka Streams & Alpakka\
**Tags:** scala, kafka\
**Created:** [March 30, 2021, 11:20am UTC](https://discuss.akka.io/t/memory-leak-after-actors-are-tried-to-be-stopped/8060 "2021-03-30T11:20:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hakangs](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/hakangs/32/2289_2.png) [@hakangs](https://discuss.akka.io/u/hakangs)\
**Post date:** [March 30, 2021, 11:20am UTC](https://discuss.akka.io/t/memory-leak-after-actors-are-tried-to-be-stopped/8060/1 "2021-03-30T11:20:02Z")

</div>

Hi, I am trying to create actors periodically every 10 minutes, and stopping them after 5 minutes (exception topic actors).

As I analyze from profiler (YourKit), I see that actors are not being garbage collected and have strong references to GC root. So the heap size is growing.

 ![Screen Shot 2021-03-30 at 14.16.43](https://us1.discourse-cdn.com/flex020/uploads/discuss_lightbend/original/2X/5/5680262a6910c56ddeaf2c4a3aace34f7d4c8a8f.png)

After creating a new actor object, my code schedules to call following commands:

```auto
override def receive: Receive = {
    case Start =>
      logger.info(s"$actorName started")

      val (killSwitch, result) = consumer
        .map(message => message.value())
        .groupedWithin(20, 5.seconds)
        .mapAsync(5)(processMessage)
        .viaMat(KillSwitches.single)(Keep.right)
        .toMat(Sink.seq)(Keep.both)
        .run()

      consumerBase = result
      consumerKillSwitch = killSwitch

    case Stop =>
      logger.info(s"$actorName stopped")

      system.stop(self)
      context.stop(self)
      consumerKillSwitch.shutdown()
      self ! Kill
  }

```

What are the best practices to kill/terminate actors so that they can be collected by GC?

Versions used :  
`akka-actor = "2.6.13"`  
`akka-stream = "2.6.13"`  
`alpAkka = "2.0.7"`

---

<div class="post-metadata">

**Author:** ![seglo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/seglo/32/1767_2.png) [@seglo](https://discuss.akka.io/u/seglo)\
**Post date:** [April 6, 2021, 1:49pm UTC](https://discuss.akka.io/t/memory-leak-after-actors-are-tried-to-be-stopped/8060/3 "2021-04-06T13:49:17Z")

</div>

Hi @hakangs. By default there is a delay in shutting down the Kafka consumer. This delay exists to allow the stream some time to commit any outstanding offsets in flight in the stream, because the consumer is required to do the commit. By default the timeout is 30 seconds, so that might be showing up in your profiler as a leak. If you use the `DrainingControl` to shutdown your stream (instead of the kill switch) then it’s safe to set this timeout to 0. Or if you’re not concerned about missing some commits at shutdown, you can set it to 0.

Hope that helps!

- [`stop-timeout` config](https://doc.akka.io/docs/alpakka-kafka/current/consumer.html#settings)
- [`DrainingControl`](https://doc.akka.io/docs/alpakka-kafka/current/consumer.html#draining-control)

---

<div class="post-metadata">

**Author:** ![hakangs](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/hakangs/32/2289_2.png) [@hakangs](https://discuss.akka.io/u/hakangs)\
**Post date:** [April 7, 2021, 7:59am UTC](https://discuss.akka.io/t/memory-leak-after-actors-are-tried-to-be-stopped/8060/4 "2021-04-07T07:59:55Z")

</div>

thanks @seglo , here is the latest version and we are not creating new actors (using existing actors, with restarting them) , what do you think about it 🙂

 ![Screen Shot 2021-04-07 at 10.56.04](https://us1.discourse-cdn.com/flex020/uploads/discuss_lightbend/original/2X/6/681638f37ee788bfeaac76d4c4bf425a893969c4.png)

---

<div class="post-metadata">

**Author:** ![seglo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/seglo/32/1767_2.png) [@seglo](https://discuss.akka.io/u/seglo)\
**Post date:** [April 7, 2021, 1:57pm UTC](https://discuss.akka.io/t/memory-leak-after-actors-are-tried-to-be-stopped/8060/5 "2021-04-07T13:57:32Z")

</div>

Are you setting the `stop-timeout` to 0 when you create the Alpakka Kafka consumer source?

Ideally you wouldn’t stop the actor until draining is complete. `drainAndShutdown` returns a `Future`. I would recommend using the [`pipeTo` pattern](https://doc.akka.io/docs/akka/current/actors.html#ask-send-and-receive-future) to send a message to self (or some other caller) when the draining is complete.
