|
4 | 4 | using System; |
5 | 5 | using System.Runtime.InteropServices.JavaScript; |
6 | 6 | using System.Runtime.InteropServices; |
| 7 | +using System.ComponentModel; |
| 8 | +using System.Collections.Generic; |
7 | 9 |
|
8 | | -namespace Sample |
| 10 | +#pragma warning disable CS8632 |
| 11 | + |
| 12 | +namespace Sample; |
| 13 | + |
| 14 | +public partial class TestClass |
9 | 15 | { |
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) |
11 | 20 | { |
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++) |
13 | 43 | { |
14 | | - DisplayMeaning(42); |
15 | | - return 0; |
| 44 | + var child = new ChildClass(_parent); |
| 45 | + _objects.Add(child); |
16 | 46 | } |
17 | 47 |
|
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")); |
20 | 90 | } |
21 | 91 | } |
0 commit comments