Skip to content

Sentry profiler seems to be leaking memory #5113

Closed

Description

@kanadaj

Package

Sentry.Profiling

.NET Flavor

.NET

.NET Version

10.0.104

OS

Any (not platform specific)

OS Version

Any

Development Environment

Rider 2025.x (Windows)

Other Error Monitoring Solution

No

Other Error Monitoring Solution Name

No response

SDK Version

6.3.0

Self-Hosted Sentry Version

6.1.0

Workload Versions

N/A

UseSentry or SentrySdk.Init call

//...
x.AddProfilingIntegration();
//...

Steps to Reproduce

  1. Enable Sentry Profiler Integration via AddProfilerIntegration()
  2. Deploy to production for real scale workload
  3. Watch memory climb until OOM issues

Expected Result

It shouldn't :P

Actual Result

We seem to have found memory leaks in the Sentry Profiler integration added via AddProfilingIntegration() due to relying on Microsoft.Diagnostics.Tracing.Etlx. The cause seems to be from undisposed instances of TraceLog, with several gigabytes of call stacks accumulating over the period of roughly an hour for us, eventually causing process memory starvation and crashes.

Activity

  1. added
    Bug
    .NET
    on Apr 2, 2026
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Apr 2, 2026
  3. linear-code commented on Apr 2, 2026

    @linear-code
  4. jamescrosswell commented on Apr 6, 2026

    @jamescrosswell
    Collaborator

    Hi @kanadaj ,

    Without a minimal reproducible example, it's very difficult to say what might be happening.

    Deploy to production for real scale workload

    Specifically this... would be very hard to guess at. What about your production scale workloads is necessary for the problem to manifest? We'd just be stabbing in the dark trying to recreate this here.

  5. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Apr 6, 2026
  6. github-actions commented on Apr 6, 2026

    @github-actions
    Contributor

    👋 Thanks for the report! We've added the Needs Reproduction label because we need a minimal reproduction to investigate further.

    Please provide a link to a public GitHub repository that reproduces the issue with as little code as possible. Our repro guide has tips on how to create one.

    Some quick hints:

    • Start from a fresh dotnet new console (or similar) project
    • Add only the Sentry NuGet package(s) relevant to the bug
    • Include the exact SDK version number and your SentrySdk.Init / UseSentry call (with the DSN blanked out)

    Alternatively, if the issue only manifests in your own codebase, you could try gradually removing code until you have a minimal repro.

  7. moved this to Waiting for: Community in GitHub Issues with 👀 3on Apr 8, 2026
  8. tvardero commented on Apr 8, 2026

    @tvardero

    Can confirm that, we had tried profiling with asp.net core back in January, in production it was eating ram. I'll try find some memory dumps before oom, will attach a bit later.

  9. 13 remaining items

  10. kanadaj commented on Apr 15, 2026

    @kanadaj
    ContributorAuthor

    @jamescrosswell Another memory leak seems to be from https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry.AspNetCore/SentryMiddleware.cs#L134 at least according to the memory dump we have. It's not a massive amount but a leaked handler is a leaked handler and should be removed during Scope cleanup

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

    @jamescrosswell
    Collaborator

    @jamescrosswell Another memory leak seems to be from https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry.AspNetCore/SentryMiddleware.cs#L134

    Thanks @kanadaj. Nothing from that line of code jumps out at me. What are you seeing as a leak (i.e. what isn't being released)?

  13. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Apr 15, 2026
  14. kanadaj commented on Apr 15, 2026

    @kanadaj
    ContributorAuthor

    @jamescrosswell Another memory leak seems to be from https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry.AspNetCore/SentryMiddleware.cs#L134

    Thanks @kanadaj. Nothing from that line of code jumps out at me. What are you seeing as a leak (i.e. what isn't being released)?

    According to the memory dump I have, the event handler's lambda method's compiler generated closure class itself is retained for some reason?

    Image

    I think it might be because of the circular reference, since the scope event handler method references the middleware instance, and the middleware instance references the scope.

    Probably need to change that method to be an instance method and unsubscribe it at the end of the request (or when the middleware is disposed of with IDisposable)

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

    @jamescrosswell
    Collaborator

    I think it might be because of the circular reference, since the scope event handler method references the middleware instance, and the middleware instance references the scope.

    Circular references shouldn't be an issue for GC... as long as none of the roots reference any of the objects in the object graph, the GC should clean these up.

    This is what I'm seeing after running your repro for about 75s (I did force a GC shortly before taking the snapshots, to make sure I wasn't seeing any false positives due to a lazy GC):

    Image

    So nothing relevant I don't think.

    I think we should merge the PR we already have, since that definitely addresses some confirmed memory leaks. Maybe we open a new issue if we think there are still issues from there.

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

    @jamescrosswell
    Collaborator

    @kanadaj let me know if you've still got issues even with 6.3.3 when this is available... we can create a new issue referencing the relevant comments from this one.

    I'll kick off a new release now.

  19. jamescrosswell commented on Apr 17, 2026

    @jamescrosswell
    Collaborator

    Just following up to say version 6.4.0 is available now. Would be great to know how this affects things for you and whether we need to keep digging or not:

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

Metadata

Metadata

Labels

.NETBugProduct: Profiling

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions