hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
824 stars 153 forks source link

BlockingClient receive(long, Duration) Memory leak #597

Closed imascha closed 7 months ago

imascha commented 8 months ago

🐛 Bug Report

The member Variable List: entries inside: com.hivemq.client.internal.mqtt.MqttBlockingClient.MqttPublishes increases in size with each call to receive(long, Duration) The entries list seems to only get cleared, when a message to a subscribed topic is received. This can cause memory problems, when no message is received for a long time.

🔬 How To Reproduce

Steps to reproduce the behavior:

  1. Subscribe to a Topic that does not receive any messages
  2. Call receive(long, Duration) in a loop with a small duration. (Im using a larger duration in my application, but choose a small one here to reproduce the problem quicker)

Code sample

  var blockingClient = Mqtt5Client.builder()
          .identifier("blocking-subscriber")
          .serverHost("localhost")
          .serverPort(1883)
          .automaticReconnectWithDefaultConfig()
          .buildBlocking();

  try (final Mqtt5BlockingClient.Mqtt5Publishes publishes = blockingClient.publishes(MqttGlobalPublishFilter.ALL)) {
      blockingClient.connect();
      blockingClient.subscribeWith().topicFilter("example/topic").send();

      while (true) {
          var publish = publishes.receive(1, TimeUnit.MICROSECONDS);
      }
  }

Environment

What version of this client are you using?

1.3.3

JVM version?

21

Operating System?

Windows 10

Which MQTT protocol version is being used?

5

Which MQTT broker (name and version)?

mosquitto 2.0.15

Screenshots

Memory image

📈 Expected behavior

The entries list should probably be periodically cleared, even when no messages are received.

SgtSilvio commented 8 months ago

Hi @imascha

Thank you for your report and that you already found out the cause for this behavior. I will take a look, but can not promise a deadline.