Freeing up system resources
I'm getting a collection of processes, adding them to a ListView, and deleting them from it when needed. All of this is done in a timer.
The problem, is that after a while, the app freezes. I think this is because I'm not disposing any variables.
Here's my code:
VB.NET Code:
Private _processes As New List(Of String)()
Dim killedProcs As New List(Of String)(_processes)
Dim processes As Process() = System.Diagnostics.Process.GetProcesses()
For Each proc As Process In processes
Dim procId As String = proc.Id.ToString()
If Not _processes.Contains(procId) Then
_processes.Add(procId)
Dim lvi As New ListViewItem(proc.ProcessName)
If strImageKey <> String.Empty Then
lvi.ImageKey = strImageKey
End If
lvi.Name = procId
Try
'Add Process ID to lvwProcess
lvi.SubItems.Add(proc.Id.ToString())
Catch ex As Exception
End Try
Try
'Add CPU usage to lvwProcess
lvi.SubItems.Add("")
Catch ex As Exception
End Try
Try
'Add memory usage to lvwProcess
lvi.SubItems.Add(proc.WorkingSet64 / 1024 & " K")
Catch ex As Exception
End Try
Try
'Add the companies name to the lvwProcess, also known as the Vendor Name
lvi.SubItems.Add(proc.MainModule.FileVersionInfo.CompanyName)
Catch ex As Exception
End Try
Try
lvi.SubItems.Add(proc.MainModule.FileVersionInfo.FileDescription)
Catch ex As Exception
End Try
Try
'Adds the image path to the lvwProcess
lvi.SubItems.Add(proc.MainModule.FileName)
Catch ex As Exception
End Try
lvwProcess.Items.Add(lvi)
Else
killedProcs.Remove(procId)
End If
Next
'remove the killed procs in the list view
For Each procId As String In killedProcs
lvwProcess.Items.RemoveByKey(procId)
Next
I tried disposing of my process list, but the IDE wouldn't let me. That's the only thing I thought would need disposing since it is what hold all of the processes.
Any ideas?
Thanks
Re: Freeing up system resources
I decided to try and force the GC to run and see if that would help. I'm currently doing a test on two different PCs. One with the old and one with the new to see what happens.
Re: Freeing up system resources
So that didn't work. Which makes sense. I didn't really expect it to. The garbage collector reclaims unused memory and I never dispose of anything =/
I was also thinking I could declare the variables outside of my sub. As it now stands, I recreating my variables each second the timer ticks and that may be causing the issue.
I'm not sure if that will cause other problems, but I'm going to try it and see what I get.
Re: Freeing up system resources
So that didn't work either.
I also tried calling the .Clear on my process list and that only caused the process list to be added over and over.
Re: Freeing up system resources
Yeah... I'm getting nothing here. It's probably the way I'm gathering the processes that's causing so much overhead.
I've seen other applications do what I am with very little overhead, but I think that's one of the negative affects of using a .NET language.
I wonder if using mostly unmanaged code via APIs will do anything for me. Better to try and than, I guess :)