using Opc.Ua; sealed class Ids : INodeIdFactory { int n; public NodeId New(ISystemContext c, NodeState node) => new NodeId("n" + (++n), 2); } static class P { static int Count(ISystemContext ctx, NodeState n, Dictionary kinds) { kinds[n.NodeClass.ToString()] = kinds.GetValueOrDefault(n.NodeClass.ToString()) + 1; var children = new List(); n.GetChildren(ctx, children); int c = 1; foreach (var ch in children) c += Count(ctx, ch, kinds); return c; } static void Run(string name, Func make, ISystemContext ctx) { var parent = new FolderState(null); var alarm = make(parent); alarm.ConfirmedState = new TwoStateVariableState(alarm); alarm.Confirm = new AddCommentMethodState(alarm); alarm.ShelvingState = new ShelvedStateMachineState(alarm); alarm.MaxTimeShelved = new PropertyState.Implementation(alarm); alarm.Create(ctx, new NodeId("cond", 2), new QualifiedName("cond", 2), new LocalizedText("cond"), true); var kinds = new Dictionary(); int total = Count(ctx, alarm, kinds); Console.WriteLine($"{name}: total={total} ({string.Join(", ", kinds.Select(k => k.Key + "=" + k.Value))}); Quality={(alarm.Quality!=null)} BranchId={(alarm.BranchId!=null)} Comment={(alarm.Comment!=null)}"); } // #929: GC.GetTotalMemory counts committed-but-free regions on .NET 10. Use the last full blocking GC's // HeapSizeBytes - FragmentedBytes plus a settled working set: the same method as the L2 harness's Measure() // (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionMemoryMeasurementTests.cs). static (long Heap, long WorkingSet) Measure() { for (int i = 0; i < 3; i++) { GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); GC.WaitForPendingFinalizers(); } GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); var info = GC.GetGCMemoryInfo(GCKind.FullBlocking); using var p = System.Diagnostics.Process.GetCurrentProcess(); p.Refresh(); long ws = p.WorkingSet64; var settle = System.Diagnostics.Stopwatch.StartNew(); while (settle.Elapsed < TimeSpan.FromSeconds(10)) { Thread.Sleep(500); p.Refresh(); long next = p.WorkingSet64; bool stable = Math.Abs(next - ws) < 1024 * 1024; ws = next; if (stable) break; } return (info.HeapSizeBytes - info.FragmentedBytes, ws); } static void Main() { var ctx = new SystemContext((ITelemetryContext)null) { NodeIdFactory = new Ids(), NamespaceUris = new NamespaceTable() }; ctx.NamespaceUris.Append("urn:a"); ctx.NamespaceUris.Append("urn:b"); Run("AlarmConditionState", p => new AlarmConditionState(p), ctx); Run("OffNormalAlarmState", p => new OffNormalAlarmState(p), ctx); Run("DiscreteAlarmState", p => new DiscreteAlarmState(p), ctx); var keep = new List(); var (heap0, ws0) = Measure(); long before = GC.GetTotalMemory(true); for (int i = 0; i < 2000; i++) { var parent = new FolderState(null); var a = new OffNormalAlarmState(parent); a.ConfirmedState = new TwoStateVariableState(a); a.Confirm = new AddCommentMethodState(a); a.ShelvingState = new ShelvedStateMachineState(a); a.MaxTimeShelved = new PropertyState.Implementation(a); a.Create(ctx, new NodeId("c"+i, 2), new QualifiedName("c"+i, 2), new LocalizedText("c"+i), true); keep.Add(a); } var (heap1, ws1) = Measure(); long after = GC.GetTotalMemory(true); Console.WriteLine($"retained bytes/condition (2000, OffNormal, no address-space indexes): heap {(heap1-heap0)/2000} (GCMemoryInfo), " + $"working set {(ws1-ws0)/2000} (settled); GC.GetTotalMemory delta {(after-before)/2000} (not used, #929)"); GC.KeepAlive(keep); var conds = keep.Cast().ToArray(); var recs = new object[conds.Length]; for (int w = 0; w < 3; w++) { var sw = System.Diagnostics.Stopwatch.StartNew(); for (int i = 0; i < conds.Length; i++) { var c = conds[i]; recs[i] = (c.NodeId, c.ActiveState?.Id?.Value, c.AckedState?.Id?.Value, c.ConfirmedState?.Id?.Value, c.EnabledState?.Id?.Value, c.ShelvingState?.CurrentState?.Value, c.Severity?.Value, c.Message?.Value, c.Quality?.Value, c.Retain?.Value, c.Time?.Value, c.Comment?.Value, c.ClientUserId?.Value, c.ConditionClassId?.Value, c.ActiveState?.TransitionTime?.Value); } sw.Stop(); Console.WriteLine($"snapshot copy of {conds.Length} conditions: {sw.Elapsed.TotalMilliseconds:F2} ms"); } } }