How to compare memory used by 2 processes

Asked

Viewed 40 times

1

Hello. I’m developing a Vb.net program that finds some processes, takes their PID, and then collects some information about them.

The problem is when you have two equal processes. In that case, I would need him to see which of the two is using the most memory at the moment, and delete the other from the listbox. My code to find the Pids:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.TopMost = True
    Me.Focus()

    For Each Proc As Process In Process.GetProcesses()
        ListBox1.Items.Add(Proc.ProcessName)
        ListBox2.Items.Add(Proc.Id)
        Proc.Start()
    Next

Private Sub Proc_Tick(sender As Object, e As EventArgs) Handles Proc.Tick
'variáveis
Dim actualProcess as String
Dim ListBox1Items as String
Dim actualProcessExists as Boolean = False
Dim ProcID as Integer
'----------------------------------------------
actualProcess = "explorer"
    ListBox1Items = ListBox1.Items.Count - 1

'Checando se o processo existe
    For ia As Integer = ListBox1Items To 0 Step -1
        ListBox1.SelectedIndex = ia
        If ListBox1.SelectedItem = actualProcess Then
            actualProcessExists = True
        Else
            Proc.Stop()
            Proc2.Start()
        End If
    Next
'----------------------------------------------------------

'Adicionando PID à outra ListBox

    If actualProcessExists = True Then
For i As Integer = ListBox1Items To 0 Step -1
    ListBox1.SelectedIndex = ia
        If ListBox1.SelectedItem = actualProcess Then
        ListBox2.SelectedIndex = ListBox1.SelectedIndex
        ProcID = ListBox2.SelectedItem
        ListBox3.Items.Add(ProcID)
    Else
   Proc.Stop()
   Proc2.Start()
End if
End Sub

Well, what I needed was that when I saw two items on Listbox2, he, by the PID’s of the processes, saw which uses more memory, and defined this one as "Procid". Could you help me?

Thank you in advance.

  • I edited it. I forgot to put the "Form_load" in the code, then I couldn’t understand the code of the timer...

  • Because it uses two ListBox? What is the purpose of your code?

  • In the first, all processes that are currently running will be added. In the second, the PIDS of these processes will be added. Then using FOR, it changes the selectedindex of listbox1, which contains all processes, and checks if the selected item matches the "actualProcess" variable, if it matches, it will set the selectedindex of listbox2 to the same selectedindex of listbox1, and then we’ll have the pid of the process. And then the process pid will be added to listbox3. (I edited the code)

1 answer

1


Your code can be greatly optimized, if what I understand is correct.
I restructured it into the following:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    TopMost = True
    Focus()
    PreencheListBox()
    Proc.Start()
End Sub

Private Sub PreencheListBox()
    ListBox1.DataSource = Process.GetProcesses().OrderBy(Function(r) r.ProcessName).ToList()
    ListBox1.ValueMember = "Id"
    ListBox1.DisplayMember = "ProcessName"
End Sub

Private Sub Proc_Tick(sender As Object, e As EventArgs) Handles Proc.Tick
    PreencheOutraListBox("explorer")
End Sub

Private Sub PreencheOutraListBox(ByVal processName As String)
    Dim processesSource = CType(ListBox1.DataSource, List(Of Process))
    Dim processes = processesSource?.Where(Function(r) String.Compare(r.ProcessName, processName) = 0).ToList()

    If processes?.Count > 0 Then
        Dim process = processes.OrderByDescending(Function(r) r.WorkingSet64).FirstOrDefault()

        If Not process Is Nothing Then
            ListBox2.Items.Add(process.Id)
        End If
    End If
End Sub

The estate WorkingSet64 returns the amount of memory allocated for a given process, so I sort the list around and just put it in the new ListBox that PID.

Please note that the name of the case in Timer is just one example (because X in X time will result in the same).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.