# Resource exhaustion: huge number of "kafka-producer-network-thread | producer-xx" threads created

**URL:** <https://discuss.akka.io/t/resource-exhaustion-huge-number-of-kafka-producer-network-thread-producer-xx-threads-created/7960>\
**Category:** Akka Streams & Alpakka\
**Tags:** kafka\
**Created:** [March 8, 2021, 8:02pm UTC](https://discuss.akka.io/t/resource-exhaustion-huge-number-of-kafka-producer-network-thread-producer-xx-threads-created/7960 "2021-03-08T20:02:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartosz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/bartosz/32/2258_2.png) [@bartosz](https://discuss.akka.io/u/bartosz)\
**Post date:** [March 8, 2021, 8:02pm UTC](https://discuss.akka.io/t/resource-exhaustion-huge-number-of-kafka-producer-network-thread-producer-xx-threads-created/7960/1 "2021-03-08T20:02:03Z")

</div>

I’m having trouble with my producer process apparently exhausting its resources.  
After sending a large batch of messages the JVM process has a large number of threads named as follows:  
`kafka-producer-network-thread | producer-xx`

My producer instance is shared across threads and looks similar to this:

```auto
class MyKafkaProducer @Inject()(actorSystem: ActorSystem)(implicit mat: Materializer) {
  val config = actorSystem.settings.config.getConfig("akka.kafka.producer")
  val producerSettings = ProducerSettings(config, new StringSerializer, new StringSerializer)
  val producer = Producer.plainSink(producerSettings)

  def send(key: String, msg: String) = {
    val record = new ProducerRecord[String, String]("topic", key, msg)
    Source.single(record).runWith(producer)
  }
}

```

Do I need to release producer resources somehow after invoking `send`?

---

<div class="post-metadata">

**Author:** ![mcnerneyd](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/mcnerneyd/32/3136_2.png) [@mcnerneyd](https://discuss.akka.io/u/mcnerneyd)\
**Post date:** [March 29, 2024, 11:22am UTC](https://discuss.akka.io/t/resource-exhaustion-huge-number-of-kafka-producer-network-thread-producer-xx-threads-created/7960/2 "2024-03-29T11:22:31Z")

</div>

Had a similar problem - sorry for bumping but needed to share. Symptoms: massive increase in stack space usage, heap is normal - resulting in sawtooth memory usage as Kubernetes restarts the pod.

Turning on Kafka producer logging, I see multiple producers created with a failure to register the mbean for JMX (because it already did that).

To fix, I created a separate producer (not a sink), and then used .withProducer on the settings to create a sink, thereby forcing it to reuse the same producer. Works a treat now - boring flatline memory graph.

That way it reuses the same producer rather than creating a new one for each message passing through the sink.
