dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.48k stars 1.07k forks source link

Memory leak #128

Closed ukgrant closed 6 years ago

ukgrant commented 6 years ago

The following code causes a memory leak when publishing messages within a ASP NET Core application. How should I be configuring this library in core?

public Startup(IConfiguration configuration)
{
        Configuration = configuration;
    MqttService.InitalizeServer();
}

public class MqttService
{
        public static IMqttServer MqttServer;

        public static IMqttServer InitalizeServer()
        {
            var optionsBuilder = new MqttServerOptionsBuilder()
            .WithConnectionBacklog(100)
            .WithDefaultEndpointPort(1884);

            MqttServer = new MqttFactory().CreateMqttServer();
            MqttServer.StartAsync(optionsBuilder.Build());
            return MqttServer;
        }

        public static Task PublishAsync(string topic, string payload)
        {
            var message = new MqttApplicationMessageBuilder()
                .WithTopic(topic)
                .WithPayload(payload)
                .Build();

            return MqttServer.PublishAsync(message);
        }
}
JanEggers commented 6 years ago

details pls!

ukgrant commented 6 years ago

I have an aspnet core web application which I am registering the MQTTServer in the startup class constructor. Calling the PublishAsync method numerous times, causes the memory consumption of the application to climb pretty quickly without being freed. This only occurs if there is a client connected. Is there a better way for me to achieve this? If there are any specific details, I can give you, let me know.

Thanks

JanEggers commented 6 years ago

can you take a memory snapshot with visual studio diagnostics and highlight the classes that consume the memory. im using the mqtt server with 1000 msg/sec and have constant 50 mb total memory usage.

archerian commented 6 years ago

@JanEggers how big are the message payloads with the 1000 msg/sec scenario?

chkr1011 commented 6 years ago

Probably this issue is related to #130.

Tyrannmisu commented 6 years ago

I am also experiencing a memory leak with version 2.6.0.

I wrote a simple stresstest that sends 7500 Messages with a Payload that consits of 2000 characters. (and a client that receives these messages). It does not matter if i sent the messages with an managed or unmanaged client. The memory usage of the server climbs steadily and never drops.

JanEggers commented 6 years ago

@Tyrannmisu if you have a repro please share the code and or make a memory snapshot from visual studio so we can see what is wrong.

Tyrannmisu commented 6 years ago

Sry, i wansn't able to get back to the topic until now. I won't be able to share any sources or repositories but i can upload a memory dump: Broker.zip

This test was done by sending 4000 messages with a payload that consits of 1000 characters. The memory usage climbed from around 9Mb to ~16,5MB and never dropped. I am able to reapeat this and even reach a memory consumption of 200MB and more.

Unfortunatly i could not upload the dump that also contains the heap because the file has a size of 160MB.

Edit: Additional info: Encoding was UTF8, retain ins false and QoS was AtMostOnce

chkr1011 commented 6 years ago

I released a preview of version 2.7.0 (nuget). Please let me know if this version has no longer a leak. We fixed several issues which are blocking threads and memory. Best regards Christian

Tyrannmisu commented 6 years ago

I know the issue is already closed, sry that i was not able to resdpond until know. The memory usage seems to be much better, i will have to do some more testing to assure that the leaks are gone.

But it looks good for now.