Skip to content

Blazor WebAssembly suffers memory leaks from compiled lambda expressions #82716

Closed

Description

@DouglasKSmith

Description

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.

private async Task RunMemoryLeakTestAsync()
{
    long total = 0;

    for (long i = 0; i < 2_000_000_000; i++)
    {
        Expression<Func<long, long>> expression = x => x + x;
        var func = expression.Compile();
        total += func(i);

        if (i % 1_000 == 0)
            await Task.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.

image

Regression?

Compiled lambda expressions don't cause memory leaks in .NET proper.

Known Workarounds

The only workaround we have is to avoid using compiled lambda expressions in Blazor WebAssembly.

Configuration

Blazor WebAssembly, compiled on .NET 6.0 using Release configuration.
Run in Google Chrome 110.0.5481.104 (64-bit) without debugging, on Windows 10.

Other information

May be related to #62054. However, we've given our issue a quick test on .NET 7.0, and it still occurred.

Activity

  1. ghost added
    untriaged
    on Feb 27, 2023
  2. changed the title [-]Blazor WebAssembly suffers memory leaks from compiled expressions[/-] [+]Blazor WebAssembly suffers memory leaks from compiled lambda expressions[/+] on Mar 1, 2023
  3. added this to the 8.0.0 milestone on Mar 10, 2023
  4. ghost removed
    untriaged
    on Mar 10, 2023
  5. jtorjo commented on May 10, 2023

    @jtorjo
    Contributor

    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.

  6. lewing commented on Aug 18, 2023

    @lewing
    Member

    We've made some fixes here for RC1 and believe it is resolved. Please reopen if you are still seeing problems

  7. ghost locked as resolved and limited conversation to collaborators on Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions