Results 1 to 5 of 5

Thread: Freeing up system resources

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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:
    1. Private _processes As New List(Of String)()
    2.  
    3.         Dim killedProcs As New List(Of String)(_processes)
    4.         Dim processes As Process() = System.Diagnostics.Process.GetProcesses()
    5.         For Each proc As Process In processes
    6.  
    7.             Dim procId As String = proc.Id.ToString()
    8.             If Not _processes.Contains(procId) Then
    9.                 _processes.Add(procId)
    10.                 Dim lvi As New ListViewItem(proc.ProcessName)
    11.                 If strImageKey <> String.Empty Then
    12.                     lvi.ImageKey = strImageKey
    13.                 End If
    14.  
    15.                 lvi.Name = procId
    16.                 Try
    17.                     'Add Process ID to lvwProcess
    18.                     lvi.SubItems.Add(proc.Id.ToString())
    19.                 Catch ex As Exception
    20.  
    21.                 End Try
    22.                 Try
    23.                     'Add CPU usage to lvwProcess
    24.                     lvi.SubItems.Add("")
    25.                 Catch ex As Exception
    26.  
    27.                 End Try
    28.                 Try
    29.                     'Add memory usage to lvwProcess
    30.                     lvi.SubItems.Add(proc.WorkingSet64 / 1024 & " K")
    31.                 Catch ex As Exception
    32.  
    33.                 End Try
    34.                 Try
    35.                     'Add the companies name to the lvwProcess, also known as the Vendor Name
    36.                     lvi.SubItems.Add(proc.MainModule.FileVersionInfo.CompanyName)
    37.                 Catch ex As Exception
    38.  
    39.                 End Try
    40.                 Try
    41.                     lvi.SubItems.Add(proc.MainModule.FileVersionInfo.FileDescription)
    42.                 Catch ex As Exception
    43.  
    44.                 End Try
    45.                 Try
    46.                     'Adds the image path to the lvwProcess
    47.                     lvi.SubItems.Add(proc.MainModule.FileName)
    48.                 Catch ex As Exception
    49.  
    50.                 End Try
    51.  
    52.                 lvwProcess.Items.Add(lvi)
    53.  
    54.             Else
    55.                 killedProcs.Remove(procId)
    56.             End If
    57.  
    58.         Next
    59.  
    60.         'remove the killed procs in the list view
    61.         For Each procId As String In killedProcs
    62.             lvwProcess.Items.RemoveByKey(procId)
    63.         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
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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.
    Last edited by weirddemon; Feb 26th, 2010 at 10:48 AM.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width