How to compare memory used by 2 processes

1

Hello. I am developing a program in vb.net that finds some processes, takes the PID from them, and then collects some information about them.

The problem is when you have 2 equal processes. In that case, I would need it to see which one is currently using more memory, and delete the other one from the listbox. My code for finding the PID's:

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 2 items in ListBox2, it, via the process PIDs, would see which one uses the most memory, and would define this as "ProcID". Could you help me?

Thank you in advance.

    
asked by anonymous 14.11.2018 / 00:12

1 answer

1

Your code can be largely optimized, if what I have noticed is correct.
I have restructured it to 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 WorkingSet64 property returns the amount of memory allocated to a given process, so I ordered the list out there and just put in the new ListBox that PID.

Note that the process name in Timer is just an example (because X in X time will result in it).

    
14.11.2018 / 17:31