Are there any events that tell the application when garbage collection?

I am trying to find a way to find out about garbage collection. Either when it started, completed or is in the process. I really need some kind of event related to the collection itself (I think).

My problem is that I have a WeakEventManager (written from scratch) and I have cleanup methods that remove WeakReferences that are no longer live (WeakReferences are in the dictionary).

The problem is that I need to know when it is time to "clean up." It would be nice to clean when the collector completes his task. Even if it is after garbage collection, at least the next collection will delete these old objects.

+3
source share
5 answers

Here, the class I used to log into SmartInspect that the GC happened. You can easily change what this class does.

To get started, just call GCLog.Register();.

#region File Header
// This file Copyright © 2007 Lasse Vågsæther Karlsen, All rights reserved.
//
// $Id: GCLog.cs 135 2008-05-28 11:28:37Z lassevk $
#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Gurock.SmartInspect;

#endregion

namespace PresentationMode
{
    /// <summary>
    /// This class is used to get a running log of the number of garbage collections that occur,
    /// when running with logging.
    /// </summary>
    public sealed class GCLog
    {
        #region Construction & Destruction

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="GCLog"/> is reclaimed by garbage collection.
        /// </summary>
        ~GCLog()
        {
            SiAuto.Main.LogMessage("GARBAGE COLLECTED");
            if (!AppDomain.CurrentDomain.IsFinalizingForUnload() && !Environment.HasShutdownStarted)
                new GCLog();
        }

        #endregion

        #region Public Static Methods

        /// <summary>
        /// Registers this instance.
        /// </summary>
        public static void Register()
        {
#if DEBUG
            if (SiAuto.Si.Enabled)
                new GCLog();
#endif
        }

        #endregion
    }
}
+4
source

You can control the .NET performance counter object. There are calculations for the number of collections of generators 0, 1 and 2.

Typically, in a GC-based system, direct access to the GC is a bit of an anti-pattern. You, most likely (given the limited description), are best at lazily clearing when trying to use a cleared WeakReference.

+7
source

System.GC RegisterForFullGCNotification, , , .

, , , .

. :

GC.RegisterForFullGCNotification Method

+6

, , ,

new MyGcMonitor(this);  // don't store result

, MyGcMonitor Finalizer (), GC fase . , , .

, , , . , GC MyGcMonitor. , delegate.invoke GC.WaitForPendingFinalizers().

MyGcMonitor () .

+1
source

In a low priority thread, you can check if the application needs to be cleaned with this simple static method.

    private static bool NeedsCleaning ()
    {
        if (DummyRef.IsAlive) {
            return false;
        }
        DummyRef = new WeakReference (new object ());
        return true;
    }
0
source

Source: https://habr.com/ru/post/1704389/


All Articles