Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
q-bertsuit opened this issue Nov 20, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@q-bertsuit
Copy link

q-bertsuit commented Nov 20, 2024

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

Create a console app that pushes data to the bus:

Program.cs:

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:

using WebApplication1;

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

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

app.Run();

MqttHostedService.cs

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);
    }
}
@q-bertsuit q-bertsuit added the bug Something isn't working label Nov 20, 2024
@xljiulang
Copy link
Contributor

@q-bertsuit
Copy link
Author

@xljiulang Thanks for your reply. When forcing the ASP net app to use workstation GC, i see very similar memory usage to the console app, like you said.

When using Server GC though, even when disconnecting to the MQTT server and disposing the client, the memory never seems to be released, even when i force a GC. Not sure if this is related to MQTTNet or if it's just how .Net works in this mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants