java—在kafka consumer中反序列化avro数据包时获取堆空间问题

yvfmudvl  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(382)

在kafka consumer中反序列化avro消息时出现内存不足的堆空间异常。
使用本地kafka生产者和消费者在java中运行消费者代码,我试图在intellij中增加堆内存到10gb,但仍然遇到这个问题。
简单消费者类别代码

Properties props = new Properties();

            props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
                    "localhost:9092");
            props.put(ConsumerConfig.GROUP_ID_CONFIG, "test1");
             props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
            props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");
            props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
           props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
                    StringDeserializer.class.getName());
            props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
                    AvroDeserializer.class.getName());

            KafkaConsumer<String, BookingContext> consumer = new KafkaConsumer<>(props);

                consumer.subscribe(Arrays.asList("fastlog"));
       while (true) {
                    ConsumerRecords<String, MyClass> records = consumer.poll(100);
                    for (ConsumerRecord<String, MyClass> record : records)  
        {
                        System.out.printf("----------------------" +
                                "+\noffset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());

       }
    }

这是我的反序列化程序类,我在其中编写了将数据包转换为普通类的过程。avro反序列化程序代码:

public T deserialize(String topic, byte[] data) {
        try {
          T result = null;

          if (data != null) {
            LOGGER.debug("data='{}'", DatatypeConverter.printHexBinary(data));

            DatumReader<GenericRecord> datumReader =
                new SpecificDatumReader<>(MyClass.getClassSchema());
            Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);

            result = (T) datumReader.read(null, decoder);
            LOGGER.debug("deserialized data='{}'", result);
          }
          return result;
        } catch (Exception ex) {
          throw new SerializationException(
              "Can't deserialize data '" + Arrays.toString(data) + "' from topic '" + topic + "'", ex);
        }
      }
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at org.apache.avro.generic.GenericData$Array.<init>(GenericData.java:245)
        at org.apache.avro.generic.GenericDatumReader.newArray(GenericDatumReader.java:391)
        at org.apache.avro.generic.GenericDatumReader.readArray(GenericDatumReader.java:257)
        at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:177)
        at org.apache.avro.specific.SpecificDatumReader.readField(SpecificDatumReader.java:116)
        at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222)
        at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175)
        at org.apache.avro.specific.SpecificDatumReader.readField(SpecificDatumReader.java:116)
        at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222)
        at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175)
        at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
        at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:145)
        at kafka.serializer.AvroDeserializer.deserialize(AvroDeserializer.java:59)
        at kafka.serializer.AvroDeserializer.deserialize(AvroDeserializer.java:21)
        at org.apache.kafka.common.serialization.ExtendedDeserializer$Wrapper.deserialize(ExtendedDeserializer.java:65)
        at org.apache.kafka.common.serialization.ExtendedDeserializer$Wrapper.deserialize(ExtendedDeserializer.java:55)
        at org.apache.kafka.clients.consumer.internals.Fetcher.parseRecord(Fetcher.java:918)
        at org.apache.kafka.clients.consumer.internals.Fetcher.access$2600(Fetcher.java:93)
        at org.apache.kafka.clients.consumer.internals.Fetcher$PartitionRecords.fetchRecords(Fetcher.java:1095)
        at org.apache.kafka.clients.consumer.internals.Fetcher$PartitionRecords.access$1200(Fetcher.java:944)
        at org.apache.kafka.clients.consumer.internals.Fetcher.fetchRecords(Fetcher.java:567)
        at org.apache.kafka.clients.consumer.internals.Fetcher.fetchedRecords(Fetcher.java:528)
        at org.apache.kafka.clients.consumer.KafkaConsumer.pollOnce(KafkaConsumer.java:1110)
        at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1043)
        at SimpleConsumer.main(SimpleConsumer.java:43)
hzbexzde

hzbexzde1#

您发布的代码没有显示任何可能会耗尽内存的内容,但显然您正在存储这些内容 result 在其他地方返回值,并且不在它们之后进行清理。我建议你查一下你的电话号码 deserialize 方法,并检查是否将所有这些结果存储在一个列表或另一个数据结构中,而不是清除它们。
您可以做的另一件事是运行jvmprofiler,比如jvisualvm,然后执行堆转储,显示阻塞jvm堆的对象的类型/数量。

相关问题