MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

订阅高并发topic场景会存在内存泄漏

Open colincodefirst opened this issue 11 months ago • 4 comments

Verification

screenshot-20240311-131946

colincodefirst avatar Mar 11 '24 05:03 colincodefirst

screenshot-20240311-134723

// See https://aka.ms/new-console-template for more information
using MQTTnet;
using MQTTnet.Client;

Console.WriteLine("Hello, World!");

var mqttFactory = new MqttFactory();
var mqttClient = mqttFactory.CreateMqttClient();

var mqttClientOptions = new MqttClientOptionsBuilder()
     .WithTcpServer("10.132.105.8")
     .WithClientId("ebb46a457522466882bdb6d316fa29f3")
     .Build();

// Setup message handling before connecting so that queued messages
// are also handled properly. When there is no event handler attached all
// received messages get lost.
mqttClient.ApplicationMessageReceivedAsync += e =>
{
    Console.WriteLine("Received application message.");
    Thread.Sleep(500);
    return Task.CompletedTask;
};

await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);

var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
.WithTopicFilter(
    f =>
    {
        f.WithTopic("/iothub/#");
    })
.Build();

await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);

Console.ReadLine();

topic: /iothub/a 这个主题一秒钟有100条数据包

colincodefirst avatar Mar 11 '24 05:03 colincodefirst

Google Translate: 'Das Abonnieren von Themenszenarien mit hoher Parallelität kann zu Speicherverlusten führen'

SeppPenner avatar Mar 12 '24 10:03 SeppPenner

@colincodefirst First I would suggest to replace Thread.Sleep(500); with await Task.Delay(TimeSpan.FromMilliseconds(500)); Does this change anything?

MD-V avatar Mar 13 '24 12:03 MD-V

@colincodefirst First I would suggest to replace Thread.Sleep(500); with await Task.Delay(TimeSpan.FromMilliseconds(500)); Does this change anything?

I agree, the rest of the code looks good for me.

SeppPenner avatar Mar 14 '24 06:03 SeppPenner