public static void CancelFullGCNotification() { GC.CancelFullGCNotification(); }
private static void CancelProc() { Thread.Sleep(500); GC.CancelFullGCNotification(); }
/// <summary> /// 取消通知 /// </summary> public static void Cancel() { finalExit = true; GC.CancelFullGCNotification(); }
public static void Main(string[] args) { try { // Register for a notification. GC.RegisterForFullGCNotification(10, 10); Console.WriteLine("Registered for GC notification."); checkForNotify = true; bAllocate = true; // Start a thread using WaitForFullGCProc. Thread thWaitForFullGC = new Thread(new ThreadStart(WaitForFullGCProc)); thWaitForFullGC.Start(); // While the thread is checking for notifications in // WaitForFullGCProc, create objects to simulate a server workload. try { int lastCollCount = 0; int newCollCount = 0; while (true) { if (bAllocate) { load.Add(new byte[1000]); newCollCount = GC.CollectionCount(2); if (newCollCount != lastCollCount) { // Show collection count when it increases: Console.WriteLine("Gen 2 collection count: {0}", GC.CollectionCount(2).ToString()); lastCollCount = newCollCount; } // For ending the example (arbitrary). if (newCollCount == 500) { finalExit = true; checkForNotify = false; break; } } } } catch (OutOfMemoryException) { Console.WriteLine("Out of memory."); } // <Snippet7> finalExit = true; checkForNotify = false; GC.CancelFullGCNotification(); // </Snippet7> } catch (InvalidOperationException invalidOp) { Console.WriteLine("GC Notifications are not supported while concurrent GC is enabled.\n" + invalidOp.Message); } }
public void Cancel() { GC.CancelFullGCNotification(); watcherThread.Join(); }
static void Main(string[] arguments) { /* * bool isServerGC = GCSettings.IsServerGC; * int pointerSize = IntPtr.Size; * int numberProcs = Environment.ProcessorCount; * Console.WriteLine("{0} on {1}-bit with {2} procs", * (isServerGC ? "Server" : "Workstation"), * ((pointerSize == 8) ? 64 : 32), * numberProcs); * * Console.WriteLine(GC.MaxGeneration); * * // Listing 15-1 * GC.Collect(0); * Console.WriteLine($"{GC.CollectionCount(0)} {GC.CollectionCount(1)} {GC.CollectionCount(2)}"); * GC.Collect(1); * Console.WriteLine($"{GC.CollectionCount(0)} {GC.CollectionCount(1)} {GC.CollectionCount(2)}"); * GC.Collect(2); * Console.WriteLine($"{GC.CollectionCount(0)} {GC.CollectionCount(1)} {GC.CollectionCount(2)}");*/ // Listing 15-6 Console.WriteLine("Hello world!"); var process = Process.GetCurrentProcess(); Console.WriteLine($"{process.PrivateMemorySize64:N0}"); Console.WriteLine($"{process.WorkingSet64:N0}"); Console.WriteLine($"{process.VirtualMemorySize64:N0}"); Console.WriteLine($"{GC.GetTotalMemory(true):N0}"); //Console.WriteLine(process.PagedMemorySize64 / 1024); //Console.WriteLine(process.PagedSystemMemorySize64 / 1024); //Console.WriteLine(process.NonpagedSystemMemorySize64 / 1024); Console.ReadLine(); //GC.Collect(); //Console.ReadLine(); /* * { * var args = new object[5]; * var mi = typeof(GC).GetMethod("GetMemoryInfo", BindingFlags.Static | BindingFlags.NonPublic); * mi.Invoke(null, args); * uint highMemLoadThreshold = (uint) args[0]; * ulong totalPhysicalMem = (ulong) args[1]; * uint lastRecordedMemLoad = (uint) args[2]; * UIntPtr lastRecordedHeapSize = (UIntPtr) args[3]; * UIntPtr lastRecordedFragmentation = (UIntPtr) args[4]; * } * * { * var args = new object[2]; * var mi = typeof(MemoryFailPoint).GetMethod("GetMemorySettings", BindingFlags.Static | BindingFlags.NonPublic); * mi.Invoke(null, args); * } * * // Listing 15-14 * try * { * using (MemoryFailPoint failPoint = new MemoryFailPoint(sizeInMegabytes: 1024)) * { * // Do calculations * } * } * catch (InsufficientMemoryException e) * { * Console.WriteLine(e); * throw; * } */ { // Region 15-13 GC.RegisterForFullGCNotification(10, 10); Thread startpolling = new Thread(() => { while (true) { GCNotificationStatus s = GC.WaitForFullGCApproach(1000); if (s == GCNotificationStatus.Succeeded) { Console.WriteLine("GC is about to begin"); } else if (s == GCNotificationStatus.Timeout) { continue; } // ... // react to full GC, for example call code disabling current server from load balancer // ... s = GC.WaitForFullGCComplete(10_000); if (s == GCNotificationStatus.Succeeded) { Console.WriteLine("GC has ended"); } else if (s == GCNotificationStatus.Timeout) { Console.WriteLine("GC took alarming amount of time"); } } }); startpolling.Start(); GC.CancelFullGCNotification(); } }