Skip to content

Memory Issues in HostedService #4685

Closed
Closed

Description

@joshua211

Package

Sentry.AspNetCore
Sentry.OpenTelemetry

.NET Flavor

.NET Core

.NET Version

9.0.111

OS

Linux

OS Version

Debian 12 (official aspnetcore docker image)

Development Environment

Rider 2024 (Windows)

SDK Version

5.16.0

Self-Hosted Sentry Version

No response

Workload Versions

aspire 8.2.2/8.0.100 SDK 9.0.100

UseSentry or SentrySdk.Init call

builder.WebHost.UseSentry(options =>
{
            //DSN from appsettings
            options.UseOpenTelemetry();
            options.SendDefaultPii = true;
            options.MaxRequestBodySize = RequestSize.Always;
            options.MinimumBreadcrumbLevel = LogLevel.Debug;
            options.MinimumEventLevel = LogLevel.Error;
            options.AttachStacktrace = true;
            options.TracesSampleRate = 0.0;
            options.Environment = builder.Environment.EnvironmentName;

            options.AddExceptionFilterForType<OperationCanceledException>();
            options.AddExceptionFilterForType<KustoClientRequestCanceledByUserException>();
});

Steps to Reproduce

  1. Create a WebApplicationBuilder
  2. Add a hosted Service (BackgroundService) in our case
  3. Run that service in a loop (while (!stoppingToken.IsCancellationRequested)...)
  4. Watch the memory for a prolonged time

Expected Result

The memory keeps stable over a longer period

Actual Result

tldr:
memory spikes that results in System.OutOfMemory exeptions

Image

Longer version:
We've introduced Sentry in 4 of our backend services a few weeks ago. Right after deploying those services to our Azure container background, we've noticed some OutOfMemory Exceptions on two of our services. Both services have BackgroundServices where the exceptions occur. Both BackgroundServices start a loop for the entire lifetime of the service, periodically checking with an Azure Queue Client for updates.
We've tried a few things, but in the end kinda resolved the issue with a manual GC.Collect() after each run of the loop and the memory level went back to normal (as you can see in the screenshot)

Activity

  1. added
    BugSomething isn't working
    .NETPull requests that update .net code
    on Oct 31, 2025
  2. linear commented on Oct 31, 2025

    @linear
  3. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Oct 31, 2025
  4. jamescrosswell commented on Nov 2, 2025

    @jamescrosswell
    Collaborator

    Hey @joshua211 ,

    You could try capturing a heap dump, to see what's eating all the memory:

    That functionality is still experimental. The main issue with it is that Sentry can't handle really large files, but if it's just in your sample app and the dump is reasonably simple then it should work.

  5. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Nov 2, 2025
  6. jamescrosswell commented on Nov 17, 2025

    @jamescrosswell
    Collaborator

    @joshua211 could it be related to this?

    That has been fixed in the upcoming version 6 release. We'll get it back ported to v5 as soon as possible as well.

  7. self-assigned this
    on Nov 26, 2025
  8. joshua211 commented on Apr 15, 2026

    @joshua211
    Author

    Hello,
    sorry it took us some time to look into this.
    We upgraded our Sentry version to 6, but the memory leak still persists.

  9. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Apr 15, 2026
  10. jamescrosswell commented on Apr 15, 2026

    @jamescrosswell
    Collaborator
  11. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Apr 15, 2026
  12. jamescrosswell commented on Apr 17, 2026

    @jamescrosswell
    Collaborator

    @joshua211 version 6.4.0 is now available. Would be great to check/confirm whether this resolves the issue for you. If not, we'd need to do some more digging.

  13. jamescrosswell commented on May 4, 2026

    @jamescrosswell
    Collaborator

    I'll close this issue for now. If you've still got problems after upgrading to the latest version of Sentry, feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

.NETPull requests that update .net codeBugSomething isn't workingNeeds Reproduction

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions