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