java - 卡夫卡生产者巨大的内存使用量(泄漏?)
问题描述
我们在生产中部署了 3 个代理 Kafka 0.10.1.0。有些应用程序中嵌入了 Kafka 生产者,它们将应用程序日志发送到主题。该主题有 10 个分区,复制因子为 3。
我们观察到其中一些应用程序服务器上的内存使用量会间歇性地飙升。在采取 heapdump 之后,我们发现主要嫌疑人是:
**org.apache.kafka.common.network.Selector -**
occupies 352,519,104 (24.96%) bytes. The memory is accumulated in one instance of "byte[]" loaded by "<system class loader>".
**org.apache.kafka.common.network.KafkaChannel -**
occupies 352,527,424 (24.96%) bytes. The memory is accumulated in one instance of "byte[]" loaded by "<system class loader>"
这两个都持有大约 352MB 的空间。3 个这样的实例,因此它们消耗了大约 1.2GB 的内存。
现在关于生产者的使用。没有大量日志被发送到 Kafka 集群。它大约是 200 条消息/秒。在整个应用程序中只使用一个生产者对象。使用异步发送功能。
如此巨大的内存使用量可能是什么原因?这是这个特定 Kafka 版本中的某种内存泄漏吗?
这是生产中使用的 Kafka Producer 配置。
kafka.bootstrap.servers=x.x.x.x:9092,x.x.x.x:9092,x.x.x.x:9092
kafka.acks=0
kafka.key.serializer=org.apache.kafka.common.serialization.StringSerializer
kafka.value.serializer=org.apache.kafka.common.serialization.StringSerializer
kafka.max.block.ms=1000
kafka.request.timeout.ms=1000
kafka.max.in.flight.requests.per.connection=1
kafka.retries=0
kafka.compression.type=gzip
kafka.security.protocol=SSL
kafka.ssl.truststore.location=/data/kafka/kafka-server-truststore.jks
kafka.ssl.truststore.password=XXXXXX
kafka.linger.ms=300
logger.level=INFO
这是 GC 日志中显示 Kafka 网络线程分配的部分
<allocation-stats totalBytes="3636833992" >
<allocated-bytes non-tlh="3525405200" tlh="111428792" />
<largest-consumer threadName="kafka-producer-network-thread | producer-1" threadId="0000000033A26700" bytes="3525287448" />
</allocation-stats>
<gc-op id="591417" type="scavenge" timems="21.255" contextid="591414" timestamp="2018-09-19T17:55:32.938">
<scavenger-info tenureage="14" tenuremask="4000" tiltratio="89" />
<memory-copied type="nursery" objects="61155" bytes="6304384" bytesdiscarded="3968416" />
<memory-copied type="tenure" objects="1199" bytes="230312" bytesdiscarded="38656" />
<finalization candidates="461" enqueued="316" />
<ownableSynchronizers candidates="18" cleared="5" />
<references type="soft" candidates="231" cleared="0" enqueued="0" dynamicThreshold="23" maxThreshold="32" />
<references type="weak" candidates="20" cleared="2" enqueued="1" />
<references type="phantom" candidates="2" cleared="0" enqueued="0" />
</gc-op>
<gc-end id="591418" type="scavenge" contextid="591414" durationms="21.715" usertimems="11.640" systemtimems="0.125" timestamp="2018-09-19T17:55:32.939" activeThreads="64">
<mem-info id="591419" free="4226106664" total="6049234944" percent="69">
<mem type="nursery" free="3855164752" total="4294967296" percent="89">
<mem type="allocate" free="3855164752" total="3865444352" percent="99" />
<mem type="survivor" free="0" total="429522944" percent="0" />
</mem>
<mem type="tenure" free="370941912" total="1754267648" percent="21">
<mem type="soa" free="362646600" total="1740233728" percent="20" />
<mem type="loa" free="8295312" total="14033920" percent="59" />
</mem>
<pending-finalizers system="315" default="1" reference="1" classloader="0" />
<remembered-set count="4110" />
</mem-info>
</gc-end>
<cycle-end id="591420" type="scavenge" contextid="591414" timestamp="2018-09-19T17:55:32.940" />
<allocation-satisfied id="591421" threadId="0000000033A26700" bytesRequested="352518920" />
<af-end id="591422" timestamp="2018-09-19T17:55:32.962" />
<exclusive-end id="591423" timestamp="2018-09-19T17:55:32.962" durationms="45.987" />
解决方案
可能有很多原因。但是如果您需要优化,您可以尝试以下内容:
replica.fetch.max.bytes
- 每个分区的缓冲区大小。分区数乘以最大消息的大小不超过可用内存。同样适用于消费者-fetch.message.max.bytes
,max.partition.fetch.bytes
- 服务器将返回的每个分区的最大数据量。可以通过使用来完成缺失数据的检查点replica.high.watermark.checkpoint.interval.ms
,这将调整吞吐量。
2. batch.size
(不应该超过可用内存)和linger.ms
(设置缓冲数据的最长时间,如果它是异步的)
推荐阅读
- angular - 如何在Angular 9中获取一个div元素的高度并将其设置为另一个?
- node.js - 在生产开发的基础上改变package.json文件依赖
- mongodb - 聚合查询返回空数组
- python - Mac 上的 Python 版本帮助
- scala - Scala 自定义类型类
- performance - 如何编写代码来测量我的 android 手机的 FLOPS
- azure - 仅从 Azure 存储 [Azure-Blob][REST] 中的 Blob 列表获取特定元数据
- ruby-on-rails - Rails 服务器无法在 docker 容器中启动
- javascript - React Rodal 不适合模式窗口内的文本
- java - Java:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 3 at javaapplication9.Tictactoe.setPlay