Skip to content

Commit 0d68ed9

Browse files
committed
repro
1 parent 4f66989 commit 0d68ed9

File tree

2 files changed

+90
-20
lines changed

2 files changed

+90
-20
lines changed

src/mono/sample/wasm/browser/Program.cs

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,88 @@
44
using System;
55
using System.Runtime.InteropServices.JavaScript;
66
using System.Runtime.InteropServices;
7+
using System.ComponentModel;
8+
using System.Collections.Generic;
79

8-
namespace Sample
10+
#pragma warning disable CS8632
11+
12+
namespace Sample;
13+
14+
public partial class TestClass
915
{
10-
public partial class Test
16+
private static readonly ParentClass _parent = new();
17+
private static readonly HashSet<ChildClass> _objects = [];
18+
19+
public static int Main(string[] args)
1120
{
12-
public static int Main(string[] args)
21+
//GC.AddMemoryPressure(1024*1024*512);
22+
var tm = GetStats();
23+
Console.WriteLine($"TotalMemory: {tm}");
24+
return 0;
25+
}
26+
27+
public static long GetStats()
28+
{
29+
var tm = GC.GetTotalMemory(forceFullCollection: false);
30+
// Console.WriteLine($"TotalMemory: {tm}");
31+
32+
//var mi = GC.GetGCMemoryInfo();
33+
//Console.WriteLine($"HighMemoryLoadThresholdBytes: {mi.HighMemoryLoadThresholdBytes}");
34+
//Console.WriteLine($"TotalAvailableMemoryBytes: {mi.TotalAvailableMemoryBytes}");
35+
return tm;
36+
}
37+
38+
[JSExport]
39+
[return: JSMarshalAs<JSType.Number>]
40+
public static long AllocateObjects()
41+
{
42+
for (int i = 0; i < 10; i++)
1343
{
14-
DisplayMeaning(42);
15-
return 0;
44+
var child = new ChildClass(_parent);
45+
_objects.Add(child);
1646
}
1747

18-
[JSImport("Sample.Test.displayMeaning", "main.js")]
19-
internal static partial void DisplayMeaning(int meaning);
48+
return GetStats();
49+
}
50+
51+
[JSExport]
52+
public static void DisposeObjects()
53+
{
54+
foreach (var child in _objects)
55+
{
56+
child.Dispose();
57+
}
58+
}
59+
}
60+
61+
public sealed class ChildClass : IDisposable
62+
{
63+
private readonly ParentClass _parent;
64+
private readonly byte[] _junk = new byte[250_000];
65+
66+
public ChildClass(ParentClass parent)
67+
{
68+
_parent = parent;
69+
_parent.PropertyChanged += OnPropertyChanged;
70+
}
71+
72+
public void Dispose()
73+
{
74+
_parent.PropertyChanged -= OnPropertyChanged;
75+
GC.SuppressFinalize(this);
76+
}
77+
78+
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
79+
{
80+
}
81+
}
82+
83+
public sealed class ParentClass
84+
{
85+
public event PropertyChangedEventHandler? PropertyChanged;
86+
87+
public void NotifyChilderen()
88+
{
89+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Foo"));
2090
}
2191
}

src/mono/sample/wasm/browser/main.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33

44
import { dotnet, exit } from './_framework/dotnet.js'
55

6-
function displayMeaning(meaning) {
7-
document.getElementById("out").innerHTML = `${meaning}`;
8-
}
9-
106
try {
11-
const { setModuleImports } = await dotnet
7+
const { getAssemblyExports, runMain, Module } = await dotnet
128
.withElementOnExit()
139
.withExitOnUnhandledError()
1410
.create();
1511

16-
setModuleImports("main.js", {
17-
Sample: {
18-
Test: {
19-
displayMeaning
20-
}
21-
}
22-
});
12+
await runMain("Wasm.Browser.Sample", []);
2313

24-
await dotnet.run();
14+
const library = await getAssemblyExports("Wasm.Browser.Sample");
15+
const testClass = library.Sample.TestClass;
16+
console.log("Start allocating objects...");
17+
for (var i = 0; i < 389; i++) {
18+
const tm = testClass?.AllocateObjects();
19+
const linear = Module.HEAP32.byteLength;
20+
console.log(`${i}: managed ${tm} WASM ${linear} bytes. Ratio ${linear / tm}`);
21+
}
22+
console.log("Disposing allocated objects...");
23+
testClass?.DisposeObjects();
24+
console.log("Tadaaaa!");
2525
}
2626
catch (err) {
2727
exit(2, err);

0 commit comments

Comments
 (0)