Search code examples
c#multithreadingwindows-servicesweak-references

Is static List<WeakReference> needed?


I'm reverse engineering a old windows service that was written in vb.net using ILSpy. I want to rewrite the legacy service in C#. The original service leaks memory.

When I have a look at the source there is the following declaration in the service class:

private static List<WeakReference> __ENCList = new List<WeakReference>();

This list is used only used in the constructor as follows(imineRun is the service class):

List<WeakReference> _ENCList = imineRun.__ENCList;
            Monitor.Enter(_ENCList);
            try
            {
                imineRun.__ENCList.Add(new WeakReference(this));
            }
            finally
            {
                Monitor.Exit(_ENCList);
            }

Could this list be causing the memory leaks? Is this list needed and what is its purpose?


Solution

  • Since it is used nowhere in your code, it isn't a necessary part of the service. Now, I took a look around as I've seen this before in IL output and apparently you've stumbled across Edit-and-Continue code which was left in the service. Per the blog post it apparently can be a culprit for high memory usage.

    Having modules built in debug mode running on a Production server is never a good idea. And as it turned out in this case, the debug mode modules combined with the fact that these modules implement the __ENCLIST helper class for Visual Studio’s Edit and Continue feature.

    I'll bet it was compiled with full debug support rather than as a release executable.

    You can safely ignore this code in your port.