向kafka发送大量数据报错java.lang.OutOfMemoryError: Java heap space

  • Post author:
  • Post category:java


当发送大量数据到kafka时会内存溢出java.lang.OutOfMemoryError: Java heap space

错误如下:

2023-06-16T05:08:10.023302226Z java.lang.OutOfMemoryError: Java heap space

2023-06-16T05:08:10.023372917Z [2023-06-16 05:08:10,022] ERROR Processor got uncaught exception. (kafka.network.Processor)

2023-06-16T05:17:36.153705406Z java.lang.OutOfMemoryError: Java heap space

2023-06-16T05:17:36.153716255Z at java.nio.HeapByteBuffer.(HeapByteBuffer.java:57)

2023-06-16T05:17:36.153726874Z at java.nio.ByteBuffer.allocate(ByteBuffer.java:335)

2023-06-16T05:17:36.153735348Z at org.apache.kafka.common.memory.MemoryPool$1.tryAllocate(MemoryPool.java:30)

2023-06-16T05:17:36.153744283Z at org.apache.kafka.common.network.NetworkReceive.readFromReadableChannel(NetworkReceive.java:140)

2023-06-16T05:17:36.153753348Z at org.apache.kafka.common.network.NetworkReceive.readFrom(NetworkReceive.java:93)

2023-06-16T05:17:36.153762477Z at org.apache.kafka.common.network.KafkaChannel.receive(KafkaChannel.java:235)

2023-06-16T05:17:36.153770683Z at org.apache.kafka.common.network.KafkaChannel.read(KafkaChannel.java:196)

2023-06-16T05:17:36.153805259Z at org.apache.kafka.common.network.Selector.attemptRead(Selector.java:559)

2023-06-16T05:17:36.153825723Z at org.apache.kafka.common.network.Selector.pollSelectionKeys(Selector.java:495)

2023-06-16T05:17:36.153835989Z at org.apache.kafka.common.network.Selector.poll(Selector.java:424)

2023-06-16T05:17:36.153843689Z at kafka.network.Processor.poll(SocketServer.scala:665)

2023-06-16T05:17:36.153851286Z at kafka.network.Processor.run(SocketServer.scala:582)

2023-06-16T05:17:36.153858786Z at java.lang.Thread.run(Thread.java:748)

原因是:

是因为发送的消息超过了kafka默认的大小 默认是100m

在kafka的配置文件server.properties中修改socket.request.max.bytes的值,修改之后不再报错

如:socket.request.max.bytes=1073741824

以上配置完成可以解决发送大量数据到kakfka的问题;

但是里面有 java.lang.OutOfMemoryError: Java heap space

我们还需要配置kafka-server-start.sh 将启动命令中-Xmx1G -Xms1G加大默认是1个G,可以加大到NG根据自己的服务器及数据量来配置。

还要改哈kafka与zk的通信时间

server.properties中增加

zookeeper.connection.timeout.ms=14000

如有帮助就麻烦动动你温柔帅气及漂亮的小手点点关注!!!谢谢!!



版权声明:本文为weixin_40341521原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。