Repository navigation
Closed
Labels
Milestone
Description
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
dotnet-policy-service commentedon Oct 3, 2024
Tagging subscribers to 'arch-wasm': @lewing
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Oct 3, 2024
Tagging subscribers to this area: @BrzVlad
See info in area-owners.md if you want to be subscribed.
pavelsavara commentedon Oct 3, 2024
cc @kg @dlemstra
pavelsavara commentedon Oct 3, 2024
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
mmapare 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[-][browser] OutOfMemoryException with delegates[/-][+][browser] OutOfMemoryException after bad allocation ratio[/+]pavelsavara commentedon Oct 4, 2024
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 504managed bytes.Size of WASM linear memory is
0x7FD60000at that point.pavelsavara commentedon Oct 7, 2024
setting
USE_MALLOCinsrc\mono\mono\sgen\sgen-los.cfixes the ratio issue. This is not a solution, but rather possible clue to it.pavelsavara commentedon Oct 7, 2024
runtime/src/mono/mono/utils/mono-mmap-wasm.c
Lines 161 to 162 in ba28992
Edited:
If the
alignmentis bigger than 64kb (1M forLOS_SECTION_SIZE) this will be skipped and we will allocate double size below.runtime/src/mono/mono/sgen/sgen-los.c
Line 317 in ba28992
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)
pavelsavara commentedon Oct 7, 2024
Or maybe I'm confused about it. I will continue tomorrow.
pavelsavara commentedon Oct 8, 2024
@dlemstra does your application really allocate many arrays bigger than 64KB ? Or this is just synthetic example that demonstrates same symptoms ?
dlemstra commentedon Oct 14, 2024
The example was created to demonstrate these symptoms. The allocation in the application was done to "simulate" allocations of large objects.
pavelsavara commentedon Oct 14, 2024
This is from the real app on Net9
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 ?dlemstra commentedon Oct 14, 2024
There is no OOM yet in that log output. This is output of a crash (
MONO_WASM: memory access out of bounds) innet9.0that should be fixed with the patch from #108650.dlemstra commentedon Oct 14, 2024
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:
pavelsavara commentedon Sep 17, 2025
I think this is fixed by #118099