dotnet / dotnet/MQTTnet

ASP net apps consuming data use a lot more memory than console apps

Open
#2,112 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
5.1k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

I've noticed that ASP net apps that consume MQTT messages also use a lot more memory than console apps that subscribe to the same data.
The memory never seems to be released. Running dotMemory shows that the app uses a lot of unmanaged memory

### Which component is your bug related to?

- Client, version 4.3.7.1207

Memory consumption of the Producer console app, Consumer console app and Consumer Asp Net 6 app:

![Image](https://github.com/user-attachments/assets/1df32a94-d4b9-42b0-9ebc-cf385616f380)


Create a console app that pushes data to the bus:

Program.cs:
```csharp
using System.Text;
using System.Text.Json;
using MQTTnet;
using MQTTnet.Client;

var factory = new MqttFactory();
var client = factory.CreateMqttClient();

var options = new MqttClientOptionsBuilder()
.WithClientId("SenderClient")
.WithTcpServer("localhost", 1883)
.Build();

await client.ConnectAsync(options, CancellationToken.None);

var dto = new LargeDto
{
Id = 1,
Name = "Sample",
Data = new byte[1024 * 50]
};

var jsonData = JsonSerializer.Serialize(dto);
var message = new MqttApplicationMessageBuilder()
.WithTopic("large/dto")
.WithPayload(Encoding.UTF8.GetBytes(jsonData))
.WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce)
.Build();

while (true)
{
await client.PublishAsync(message, CancellationToken.None);
await Task.Delay(1);
}

await client.DisconnectAsync();

public class LargeDto
{
public int Id { get; set; }
public string Name { get; set; }
public byte[] Data { get; set; }
}
```

Create an ASP Net web API (tested both .Net 6 and .Net 8)

Program.cs:

```csharp
using WebApplication1;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHostedService();
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();
```

MqttHostedService.cs
```csharp
using MQTTnet;
using MQTTnet.Client;

namespace WebApplication1;

public class MqttHostedService : IHostedService
{
private readonly IMqttClient _client;
private readonly MqttClientOptions _options;

public MqttHostedService()
{
var factory = new MqttFactory();
_client = factory.CreateMqttClient();

_options = new MqttClientOptionsBuilder()
.WithClientId("ListenerClientAsp6")
.WithTcpServer("localhost", 1883)
.Build();

_client.ApplicationMessageReceivedAsync += e =>
{
return Task.CompletedTask;
};
}

public async Task StartAsync(CancellationToken cancellationToken)
{
await _client.ConnectAsync(_options, cancellationToken);
await _client.SubscribeAsync(new MqttTopicFilterBuilder()
.WithTopic("large/dto")
.Build());
}

public async Task StopAsync(CancellationToken cancellationToken)
{
var disconnectOptions = new MqttClientDisconnectOptions
{
Reason = MqttClientDisconnectOptionsReason.NormalDisconnection,
ReasonString = "Service stopping"
};

await _client.DisconnectAsync(disconnectOptions, cancellationToken);
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.