Skip to content

[browser] OutOfMemoryException after bad allocation ratio #108510

Closed

Description

@pavelsavara
MONO_WASM: Out of memory
   at System.MulticastDelegate.RemoveImpl(Delegate value)
   at System.Delegate.Remove(Delegate source, Delegate value)
   at Sample.ParentClass.remove_PropertyChanged(PropertyChangedEventHandler value)
   at Sample.ChildClass.Dispose()
   at Sample.TestClass.DisposeObjects()
   at Sample.TestClass.__Wrapper_DisposeObjects_19325221(JSMarshalerArgument* __arguments_buffer)
Error: Out of memory
    at marshal_exception_to_js (https://localhost:8000/_framework/dotnet.runtime.js:2384:18)
    at invoke_sync_jsexport (https://localhost:8000/_framework/dotnet.runtime.js:3571:15)
    at Object.bound_fn_0V (https://localhost:8000/_framework/dotnet.runtime.js:4626:13)
    at Object.JSExport_DisposeObjects (https://dotnet/JSExport/DisposeObjects:4:55)
    at https://localhost:8000/main.js:20:16

Probably related #107215

Repro

I'm able to reproduce it on latest Net10 main, but customer is reporting similar issues on Net8.

using System;
using System.Runtime.InteropServices.JavaScript;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Collections.Generic;

#pragma warning disable CS8632

namespace Sample;

public partial class TestClass
{
    private static readonly ParentClass _parent = new();
    private static readonly HashSet<ChildClass> _objects = [];

    public static int Main(string[] args)
    {
        return 0;
    }

    [JSExport]
    public static string AllocateObjects(int count)
    {
        for (int i = 0; i < count; i++)
        {
            var child = new ChildClass(_parent);
            _objects.Add(child);
        }

        return GC.GetTotalMemory(forceFullCollection: false).ToString();
    }

    [JSExport]
    public static void DisposeObjects()
    {
        foreach (var child in _objects)
        {
            child.Dispose();
        }
    }
}

public sealed class ChildClass : IDisposable
{
    private readonly ParentClass _parent;
    private readonly byte[] _junk = new byte[250_000];

    public ChildClass(ParentClass parent)
    {
        _parent = parent;
        _parent.PropertyChanged += OnPropertyChanged;
    }

    public void Dispose()
    {
        _parent.PropertyChanged -= OnPropertyChanged;
        GC.SuppressFinalize(this);
    }

    private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
    }
}

public sealed class ParentClass
{
    public event PropertyChangedEventHandler? PropertyChanged;

    public void NotifyChilderen()
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Foo"));
    }
}
import { dotnet, exit } from './_framework/dotnet.js'

try {
    const { getAssemblyExports, runMain } = await dotnet
        .withElementOnExit()
        .withExitOnUnhandledError()
        .create();

    await runMain("Wasm.Browser.Sample", []);

    const library = await getAssemblyExports("Wasm.Browser.Sample");
    const testClass = library.Sample.TestClass;
    console.log("Start allocating objects...");
    const allocatedBytes = testClass?.AllocateObjects(3890);
    console.log(`Allocated ${allocatedBytes} bytes`);
    console.log("Disposing allocated objects...");
    testClass?.DisposeObjects();
}
catch (err) {
    exit(2, err);
}

Activity

  1. added
    arch-wasm
    os-browser
    on Oct 3, 2024
  2. added this to the 10.0.0 milestone on Oct 3, 2024
  3. self-assigned this
    on Oct 3, 2024
  4. dotnet-policy-service commented on Oct 3, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing
    See info in area-owners.md if you want to be subscribed.

  5. dotnet-policy-service commented on Oct 3, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @BrzVlad
    See info in area-owners.md if you want to be subscribed.

  6. pavelsavara commented on Oct 3, 2024

    @pavelsavara
    MemberAuthor
  7. pavelsavara commented on Oct 3, 2024

    @pavelsavara
    MemberAuthor

    The problem is allocation ratio. For each byte of managed memory, we allocate 2.2 bytes of WASM linear memory.
    The customer would like to use 500MB dataset + 300MB operation memory.
    I suspect that our problems with mmap are taking them over 2GB boundary. Also the paging problem is probably not visible to managed GC as a memory pressure.

    388: managed 981875504 WASM 2144731136 bytes. Ratio 2.184320850517929

  8. changed the title [-][browser] OutOfMemoryException with delegates[/-] [+][browser] OutOfMemoryException after bad allocation ratio[/+] on Oct 3, 2024
  9. pavelsavara commented on Oct 4, 2024

    @pavelsavara
    MemberAuthor

    This is the GC log just before the crash on Net10.

    It shows that we are running out of 2GB address space after we allocated only 981 852 504 managed bytes.
    Size of WASM linear memory is 0x7FD60000 at that point.

    Allocated object 0xc51658, vtable: 0x7fef0ee8 (Entry[]), size: 80d
    ...
    Wbarrier store at 0x7fecf548 to 0x1d329d0 (RuntimeType)
    
  10. pavelsavara commented on Oct 7, 2024

    @pavelsavara
    MemberAuthor

    setting USE_MALLOC in src\mono\mono\sgen\sgen-los.c fixes the ratio issue. This is not a solution, but rather possible clue to it.

  11. pavelsavara commented on Oct 7, 2024

    @pavelsavara
    MemberAuthor

    if (mono_opt_wasm_mmap && ((MWPM_PAGE_SIZE % alignment) == 0))
    return valloc_impl (NULL, size, flags, type);

    Edited:
    If the alignment is bigger than 64kb (1M for LOS_SECTION_SIZE) this will be skipped and we will allocate double size below.

    section = (LOSSection *)sgen_alloc_os_memory_aligned (LOS_SECTION_SIZE, LOS_SECTION_SIZE, (SgenAllocFlags)(SGEN_ALLOC_HEAP | SGEN_ALLOC_ACTIVATE), NULL, MONO_MEM_ACCOUNT_SGEN_LOS);

    And for objects bigger than 1MB it will go sgen_alloc_os_memory -> mono_valloc -> mono_valloc_aligned(size, 65536)
    So, maybe it's broken just for allocations between 65536 and 1MB (in Net9)

  12. pavelsavara commented on Oct 7, 2024

    @pavelsavara
    MemberAuthor

    Or maybe I'm confused about it. I will continue tomorrow.

  13. pavelsavara commented on Oct 8, 2024

    @pavelsavara
    MemberAuthor

    @dlemstra does your application really allocate many arrays bigger than 64KB ? Or this is just synthetic example that demonstrates same symptoms ?

  14. dlemstra commented on Oct 14, 2024

    @dlemstra
    Contributor

    The example was created to demonstrate these symptoms. The allocation in the application was done to "simulate" allocations of large objects.

  15. pavelsavara commented on Oct 14, 2024

    @pavelsavara
    MemberAuthor

    This is from the real app on Net9

    ...
    [MONO] GC_MINOR: (Nursery full) time 20.80ms, stw 20.80ms promoted 2854K major size: 19712K in use: 15524K empty reserved: 768K los size: 16704K in use: 14780K
    [MONO] GC_MINOR: (Nursery full) time 5.50ms, stw 5.60ms promoted 1626K major size: 21248K in use: 17187K empty reserved: 1280K los size: 16704K in use: 15732K
    MONO_WASM: memory access out of bounds
    

    Edit: I was confused
    It seems to me that between last GC and OOM there must be lot of allocations that GC doesn't consider worth collecting. Because the reported size is low megabytes, not gigabyte, right ? Maybe that's LOS with bad ratio ? Or something else ?

  16. dlemstra commented on Oct 14, 2024

    @dlemstra
    Contributor

    There is no OOM yet in that log output. This is output of a crash (MONO_WASM: memory access out of bounds) in net9.0 that should be fixed with the patch from #108650.

  17. dlemstra commented on Oct 14, 2024

    @dlemstra
    Contributor

    This file contains the logging of our application on Net8 that is running out of memory: gc-log.zip. This might be different from what is happening here but @pavelsavara asked me to attach that log file here. The last lines of the log file contain this:

    [MONO] GC_MINOR: (Nursery full) time 9.00ms, stw 9.10ms promoted 1145K major size: 954368K in use: 922763K empty reserved: 92160K los size: 480384K in use: 469992K
    [MONO] GC_MINOR: (Nursery full) time 4.20ms, stw 4.20ms promoted 1K major size: 954368K in use: 922765K empty reserved: 92160K los size: 480384K in use: 469992K
    [MONO] GC_MINOR: (Nursery full) time 6.40ms, stw 6.40ms promoted 1K major size: 954368K in use: 922767K empty reserved: 92160K los size: 480384K in use: 469992K
    [MONO] GC_MINOR: (Nursery full) time 3.60ms, stw 3.60ms promoted 1K major size: 954368K in use: 922769K empty reserved: 92160K los size: 480384K in use: 469992K
    [MONO] GC_MINOR: (Nursery full) time 8.70ms, stw 8.80ms promoted 370K major size: 954624K in use: 923149K empty reserved: 91904K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 5.60ms, stw 5.60ms promoted 81K major size: 954688K in use: 923233K empty reserved: 91840K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 6.00ms, stw 6.00ms promoted 131K major size: 954752K in use: 923369K empty reserved: 91776K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 4.60ms, stw 4.60ms promoted 74K major size: 954816K in use: 923445K empty reserved: 91712K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 3.90ms, stw 3.90ms promoted 11K major size: 954816K in use: 923457K empty reserved: 91712K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 3.90ms, stw 3.90ms promoted 3K major size: 954816K in use: 923461K empty reserved: 91712K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 4.60ms, stw 4.60ms promoted 8K major size: 954816K in use: 923469K empty reserved: 91712K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 4.20ms, stw 4.30ms promoted 52K major size: 954816K in use: 923524K empty reserved: 91712K los size: 488192K in use: 477123K
    [MONO] GC_MINOR: (Nursery full) time 14.40ms, stw 14.40ms promoted 3022K major size: 955968K in use: 926550K empty reserved: 90560K los size: 630656K in use: 619684K
    [MONO] GC_MINOR: (Nursery full) time 14.20ms, stw 14.20ms promoted 3191K major size: 957888K in use: 929838K empty reserved: 88640K los size: 630656K in use: 620066K
    [MONO] GC_MINOR: (Nursery full) time 20.70ms, stw 20.70ms promoted 3279K major size: 959744K in use: 933316K empty reserved: 86784K los size: 631680K in use: 620686K
    [MONO] GC_MINOR: (Nursery full) time 26.30ms, stw 26.30ms promoted 3385K major size: 962304K in use: 936909K empty reserved: 84224K los size: 631680K in use: 620853K
    [MONO] GC_MINOR: (Nursery full) time 20.10ms, stw 20.10ms promoted 3279K major size: 965184K in use: 940399K empty reserved: 81344K los size: 632704K in use: 621480K
    [MONO] GC_MINOR: (Nursery full) time 21.60ms, stw 21.70ms promoted 3417K major size: 968640K in use: 944011K empty reserved: 77888K los size: 632704K in use: 621968K
    [MONO] GC_MINOR: (Nursery full) time 18.70ms, stw 18.70ms promoted 3301K major size: 971712K in use: 947501K empty reserved: 74816K los size: 633728K in use: 622599K
    [MONO] GC_MINOR: (Nursery full) time 14.20ms, stw 14.20ms promoted 3404K major size: 975232K in use: 951158K empty reserved: 71296K los size: 633728K in use: 622752K
    Cannot enlarge memory, asked to go up to 2147749888 bytes, but the limit is 2147483648 bytes!
    Cannot enlarge memory, asked to go up to 2147753984 bytes, but the limit is 2147483648 bytes!
    
  18. modified the milestones: 10.0.0, Future on Nov 8, 2024
  19. pavelsavara commented on Sep 17, 2025

    @pavelsavara
    MemberAuthor

    I think this is fixed by #118099

  20. locked and limited conversation to collaborators on Oct 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-GC-monoos-browser

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions