You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Blazor WebAssembly suffers memory leaks from compiled lambda expressions #82716
We have a Blazor WebAssembly app whose memory keeps increasing until it hits the 2 GB limit, then silently crashes. We've narrowed down the main cause of the leak to compiled lambda expressions.
Reproduction Steps
The following code may be used to reproduce the memory leak. It can be wired up as the event handler of a button on a page. When left running in the active tab (in the foreground), it leaks around 46 MB of memory a minute, causing the 2 GB limit to be reached (and the app to crash) in 47 minutes.
privateasyncTaskRunMemoryLeakTestAsync(){longtotal=0;for(longi=0;i<2_000_000_000;i++){Expression<Func<long,long>>expression= x =>x+x;varfunc=expression.Compile();total+=func(i);if(i%1_000==0)awaitTask.Delay(millisecondsDelay:100);// keep app responsive and give GC time to run}}
Expected behavior
The online consensus appears to be that compiled lambda expressions shouldn't cause memory leaks in .NET. (That answer was directed at .NET proper, not Blazor WebAssembly.)
Actual behavior
Compiled lambda expressions cause a memory leak in Blazor WebAssembly. The app would consequently silently crash once the 2 GB limit is reached.
changed the title [-]Blazor WebAssembly suffers memory leaks from compiled expressions[/-][+]Blazor WebAssembly suffers memory leaks from compiled lambda expressions[/+]on Mar 1, 2023
I am really interested in this, but I don't really think it causes any leaks:
private int RunGC() {
var watch = Stopwatch.StartNew();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
return (int)watch.ElapsedMilliseconds;
}
private async void Page_Loaded(object sender, RoutedEventArgs e) {
await Task.Delay(10000);
long total = 0;
var count = 500_000_000;
RunGC();
double gcMemoryMB = 0;
gcMemoryMB = (double)GC.GetTotalMemory(false) / (1024d * 1024d);
Console.WriteLine($"START mb={gcMemoryMB}");
for (long i = 0; i < count; i++)
{
Expression<Func<long, long>> expression = x => x + x;
var func = expression.Compile();
total += func(i);
if (total > 1_000_000_000)
total = 0;
if (i % 100_000 == 0) {
var gcTime = RunGC();
gcMemoryMB = (double)GC.GetTotalMemory(false) / (1024d * 1024d);
Console.WriteLine($"i={i} mb={gcMemoryMB} time={gcTime}");
await Task.Delay(millisecondsDelay: 100); // keep app responsive and give GC time to run
}
}
gcMemoryMB = (double)GC.GetTotalMemory(false) / (1024d * 1024d);
Console.WriteLine($"END before gc mb={gcMemoryMB}");
RunGC();
gcMemoryMB = (double)GC.GetTotalMemory(false) / (1024d * 1024d);
Console.WriteLine($"END mb={gcMemoryMB}");
RunGC();
RunGC();
gcMemoryMB = (double)GC.GetTotalMemory(false) / (1024d * 1024d);
Console.WriteLine($"END 2 mb={gcMemoryMB}");
}
I've run this test in .net7 + webassembly -- manually calling the GC.Collect() will show you stuff is properly collected.
I' haven't tested .net6, but I'm almost certain it'll behave the same way.
Activity
[-]Blazor WebAssembly suffers memory leaks from compiled expressions[/-][+]Blazor WebAssembly suffers memory leaks from compiled lambda expressions[/+]jtorjo commentedon May 10, 2023
I am really interested in this, but I don't really think it causes any leaks:
I've run this test in .net7 + webassembly -- manually calling the GC.Collect() will show you stuff is properly collected.
I' haven't tested .net6, but I'm almost certain it'll behave the same way.
lewing commentedon Aug 18, 2023
We've made some fixes here for RC1 and believe it is resolved. Please reopen if you are still seeing problems