10

I'm using a FileSystemWatcher (in an ASP.NET web app) to monitor a file for changes. The watcher is set up in the constructor of a Singleton class, e.g:

private SingletonConstructor()
{
    var fileToWatch = "{absolute path to file}";
    var fsw = new FileSystemWatcher(
        Path.GetDirectoryName(fileToWatch),
        Path.GetFileName(fileToWatch));
    fsw.Changed += OnFileChanged;
    fsw.EnableRaisingEvents = true;
}

private void OnFileChanged(object sender, FileSystemEventArgs e)
{
    // process file...
}

Everything works fine so far. But my question is:

Is it safe to setup the watcher using a local variable (var fsw)? Or should I keep a reference to it in a private field to prevent it from being garbage collected?

M4N
  • 94,805
  • 45
  • 217
  • 260

1 Answers1

11

In the example above FileSystemWatcher is kept alive only because the property EnableRaisingEvents is set to true. The fact that the Singleton class has an event handler registered to FileSystemWatcher.Changed event does not have any direct bearing on fsw being eligible for Garbage collection. See Do event handlers stop garbage collection from occurring? for more information.

The following code shows that with EnableRaisingEvents set to false, the FileSystemWatcher object is garbage collected: Once GC.Collect() is called, the IsAlive property on the WeakReference is false.

class MyClass
{
    public WeakReference FileSystemWatcherWeakReference;
    public MyClass()
    {
        var fileToWatch = @"d:\temp\test.txt";
        var fsw = new FileSystemWatcher(
            Path.GetDirectoryName(fileToWatch),
            Path.GetFileName(fileToWatch));
        fsw.Changed += OnFileChanged;
        fsw.EnableRaisingEvents = false;
        FileSystemWatcherWeakReference = new WeakReference(fsw);
    }

    private void OnFileChanged(object sender, FileSystemEventArgs e)
    {
        // process file... 
    }

}

class Program
{
    static void Main(string[] args)
    {
        MyClass mc = new MyClass();
        GC.Collect();
        Console.WriteLine(mc.FileSystemWatcherWeakReference.IsAlive);
    }
}
Community
  • 1
  • 1
Muath Ali
  • 186
  • 1
  • 5
  • I can find no documentation to say that `EnableRaisingEvents` has any effect on the garbage collection. I thought a local field was needed to ensure the `FileSystemWatcher` wasn't garbage collected. – Lukazoid Feb 03 '12 at 00:32
  • I've edited my answer with an example that shows FileSystemWatcher is garbage collected if EnableRaisingEvents is false. – Muath Ali Feb 03 '12 at 01:02
  • Thanks for that, I do wish that functionality was documented on the MSDN, it looks like it would be too easy to end up with some hanging `FileSystemWatcher`s. – Lukazoid Feb 03 '12 at 01:14
  • Thank you Muath for your beautiful answer. However, could you please explain why when "EnableRaisingEvents is set to true" the object is not collected by GC? – Greg Z. Aug 02 '18 at 19:44