在Kafka Consumer中反序列化Avro数据包时出现堆空间问题

时间:2019-05-03 07:50:05

标签: java apache-kafka deserialization heap-memory avro

在Kafka使用者中反序列化avro消息时获取内存不足的堆空间。

在Java中与本地kafka生产者和消费者一起运行消费者代码,我试图在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)

1 个答案:

答案 0 :(得分:0)

您发布的代码不会显示任何会耗尽内存的内容,但是您显然会将这些result返回的值存储在其他位置,并且不会在它们之后进行清理。我建议您检查正在调用deserialize方法的内容,并检查是否可能将所有结果存储在列表或其他数据结构中,而不要清理它们。

您可以做的另一件事是运行JVM Profiler(如JVisualVM),然后执行堆转储,以显示什么类型/数量的对象阻塞了JVM堆。