1

I have discovered a memory leak in my application. Each time I call Process.GetProcesses the memory usage of my application grows and does not release until the application has been shut down. Since I am needing to call this about every 10 seconds the application only can run a few hours before it crashes running out of memory. I found this article on this site but doesn't look like much of a resolution ever came of it. Process.GetProcessesByName(String, String) Memory Leak

Here is my code:

Public Shared Function GetProcessInfo() As List(Of ClientResources_OBJECT.ProcessInfo)

        Try


            Dim ProcessInfoList As New List(Of ClientResources_OBJECT.ProcessInfo)

            For Each Proc As Process In Process.GetProcesses

                Dim PI As New ClientResources_OBJECT.ProcessInfo
                With PI
                    .Name = Proc.ProcessName
                    .PID = Proc.Id
                    .Responding = Proc.Responding
                End With
                ProcessInfoList.Add(PI)

                'cleanup resources
                Proc.Dispose()
                Proc = Nothing
                PI = Nothing

            Next

            PerformanceCounter.CloseSharedResources()
            GC.Collect()

            Return ProcessInfoList

        Catch ex As Exception
            Dim st As New StackTrace(True)
            st = New StackTrace(ex, True)
            Console.WriteLine(Err.Description, Err.Number,       System.Reflection.MethodInfo.GetCurrentMethod.Name, st.GetFrame(0).GetFileLineNumber().ToString)
        End Try

        Return Nothing

    End Function

Somebody mentioned in the other question that:

WARNING: This is just a very dirty quickfix, but use reflection to kill em off.

Accessing private variables: Can I change a private readonly field in C# using reflection?

Example using static class: Using Reflection to set a static variable value before object's initialization? (C#)

You can use variations of typeof(Process).GetFields(BindingFlags.Static | BindingFlags.NonPublic) to find the field etc.

I believe a quick fix is warrented since the behaviour of Process is obviously not correct.

I'm not really sure of what that means can anybody assist or does anybody know how to overcome this problem?

Thank you

Community
  • 1
  • 1
Willy
  • 137
  • 13
  • Do you `Dispose` the process instance properly? – Sriram Sakthivel Oct 06 '14 at 21:25
  • Yes. From my code above I call Proc.Dispose() and then Proc = Nothing – Willy Oct 06 '14 at 21:29
  • 1
    Use a memory profiler to see what is holding the memory. – Sriram Sakthivel Oct 06 '14 at 21:31
  • I have even tried assigning Process.GetProcesses to it's own variable and disposing that when finished with it and it has the same result. Even tried re-writing the function and using WMI to get the processes on the localhost and it had the same memory leak. From my research it seems that this is a bug in the .net framework. I don't think it will be solvable using conventional methods such as dispose – Willy Oct 06 '14 at 21:32
  • Microsoft also knows this is an issue yet doesn't fix it https://connect.microsoft.com/VisualStudio/feedback/details/768814/memory-leak-in-process-getprocessesbyname-string-string they said the fix was to call PerformanceCounter.CloseSharedResources but i do that in my code and it doesn't work – Willy Oct 06 '14 at 21:43
  • Do you clear `ProcessInfoList` somewhere? – Brian Rasmussen Oct 06 '14 at 21:55
  • I can't clear it because it's what im returning. – Willy Oct 06 '14 at 22:00
  • 3
    I get that. My point is if you hold on to that list, then obviously the memory will not be freed. In any case, you should find out what objects are the reason for the memory increase. One way is to use WinDbg/SOS or a dedicated memory profiler. – Brian Rasmussen Oct 06 '14 at 22:25

0 Answers0