-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
I noticed when doing some performance testing of my code that my usage of TransactionScope resulted in huge memory usage that just grew, never shrank, even after GC. At first I thought it was the SqlClient but in the end I boiled it down to the TransactionScope class and the TransactionScopeAsyncFlowOption.Enabled option which is used to support async/await with TransactionScope. I use this option everywhere to allow for SQL transactions to flow correctly with async code.
I am not entirely sure if it's me who is misunderstanding the debugger's output but running the code below leaves me with multi-gigabyte memory usage according to both the debugger and the task manager. It looks like a memory leak to me but I'm not an expert in these things, so I thought I'd report it.
There are two versions below, the first one with TransactionScopeAsyncFlowOption.Enabled grows its memory usage constantly and the other doesn't.
using System.Transactions;
namespace TestTrxScope
{
class Program
{
static void Main(string[] args)
{
while(true)
{
using(var scope = CreateTransactionScope())
{
scope.Complete();
}
}
}
public static TransactionScope CreateTransactionScope()
{
//Memory grows constantly
return new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
//This version doesn't constantly grow in memory usage
//return new TransactionScope();
}
}
}