runtime: Memory is not released when using TransactionScope with async option

Sample code

while (true)
{
    using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
    {
        scope.Complete();
    }
}

snapshot1

Memory usage grows, and response times increase until the web api halts. Removing the async option resolves the issue, but async is required in my case. 😞

Workaround (not perfect, see comment)

I created a factory (singleton) around it to manually trigger GC, something like this simplified:

public class TransactionScopeFactory
{
    private const int GcThreshold = 200_000;
    private static int _createdScopes = 0;

    public TransactionScope CreateScope()
    {
        int currentCreatedScopes = Interlocked.Increment(ref _createdScopes);

        if (currentCreatedScopes >= GcThreshold)
        {
            if (Interlocked.CompareExchange(ref _createdScopes, 0, GcThreshold) >= GcThreshold)
            {
                GC.Collect(2);
            }
        }

        return new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
    }
}

Questions

  • Is the workaround okay? I’m a bit hesitant about manual GC, as the docs advises against it without a clear purpose and the TransactionScope docs doesn’t mention anything about manually invoking GC.
  • Why does the workaround only work in release mode? It doesn’t seem to work in debug mode.

Used versions: .NET 8, 9

Thank you 😄

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Please see #50683 for more information about this. To answer a few things brought up in this issue:

  • There is a grey area with the word “leak” here. This benchmark (specifically the ConditionalWeakTable internally used by it) creates a finalizable object graph that is problematic for garbage collection - basically all of the objects require gen2 in order to make progress even though they seem like they should be short-lived. When the program approaches the memory limit, the frequent gen2s limit further growth. However, the app shouldn’t be using anywhere near that much memory, so while it’s not the traditional kind of “leak” it’s still a problem.
  • The GC threshold discussion is a workaround for that underlying problem. Assuming that my guess about the Linux OOM killer is correct, the GC threshold is simply making the GC more aggressive earlier so that the gen2s kick in before the OOM killer gets involved. I’m not sure why the default doesn’t work for you, though maybe the GC somehow isn’t getting the memory limit in order to use it to find the 75% value (i.e., setting the limit directly rather than as a percentage means you can determine that limit rather than depending on the GC to find it).

I thought the interlocked increment & exchange along with two step >= check solves that race.

The >= makes sure that you still get true as a result since the return value from the compare-exchange might be larger, but it doesn’t impact the operation of the compare-exchange itself, which is “update if the old value equals GcThreshold”, not “update if the old value is greater than GcThreshold”. Just use a lock around the increment and compare-exchange (and then they don’t need to be interlocked) as a simpler solution.