using System.Diagnostics; using System.Runtime; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text.Json; using Opc.Ua.Security.Certificates; var mode = args[0]; var directory = args[1]; Directory.CreateDirectory(directory); using var key = RSA.Create(2048); var request = new CertificateRequest("CN=NativeMemoryControl", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var original = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1)); var pfx = original.Export(X509ContentType.Pfx); // Certificate.From takes ownership of original; only wrapped disposes that native certificate. using var wrapped = Certificate.From(original); void Sample(int i, string kind) { var live = GC.GetTotalMemory(true); GC.WaitForPendingFinalizers(); using var p = Process.GetCurrentProcess(); p.Refresh(); Console.WriteLine(JsonSerializer.Serialize(new { kind, i, pid = Environment.ProcessId, mode, live, rss = p.WorkingSet64, methods = JitInfo.GetCompiledMethodCount(), gc = GC.GetGCMemoryInfo().TotalCommittedBytes })); Console.Out.Flush(); } Sample(0, "ready"); while (!File.Exists(Path.Combine(directory, "go"))) Thread.Sleep(50); for (int i = 1; i <= 1000; i++) { using X509Certificate2 copy = mode switch { "pfx" => System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(pfx, null, X509KeyStorageFlags.Exportable), "sdk-copy" => wrapped.AsX509Certificate2(), "shared-copy" => (X509Certificate2)typeof(X509Certificate2).GetConstructor(new[] { typeof(X509Certificate) })!.Invoke(new object[] { original }), _ => System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadCertificate(original.RawData) }; if (i % 100 == 0) Sample(i, "sample"); } Sample(1000, "done"); while (!File.Exists(Path.Combine(directory, "stop"))) Thread.Sleep(50);