vb.net Get PrivateMemorySize64 and process id sort by memory size

366 views Asked by At

I've got the following:

Dim plist() As Process = Process.GetProcesses()

    For Each prs As Process In plist

        ListBox1.Items.Add(prs.ProcessName + "   (" + (prs.PrivateMemorySize64 / 1024000).ToString() + " MB)")

But id really like to get it to list it by memory size if possible? If anyone has any ideas it would be much appreciated

1

There are 1 answers

1
Zeddy On BEST ANSWER

Here is a basic sample, you will have to customise it to your needs.

Public Class Form1
'
Dim pList() As Process = Nothing
'
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    '
    Dim pItem As Process = Nothing
    Dim pCounter As Integer = Nothing
    '
    pList = Process.GetProcesses()
    For pCounter = 0 To pList.Count - 1
        pItem = pList(pCounter)
        ListBox1.Items.Add(pItem.ProcessName + "   (" + (pItem.PrivateMemorySize64 / 1024000).ToString() + " MB)")        '
    Next
    'sort list
    SortProcessList()
    '
End Sub
'
Sub SortProcessList()
    '
    Dim i As Integer
    Dim j As Integer
    Dim pTmp As Process
    '
    'sort list based on lowest usage first
    For i = 0 To pList.Count - 2
        For j = i + 1 To pList.Count - 1
            If pList(i).PrivateMemorySize64 > pList(j).PrivateMemorySize64 Then
                pTmp = pList(i)
                pList(i) = pList(j)
                pList(j) = pTmp
            End If
        Next
    Next
    ReloadSortedList()
    '
End Sub

Sub ReloadSortedList()
    '
    Dim pItem As Process = Nothing
    Dim pCounter As Integer = Nothing
    '
    ListBox1.Items.Clear()
    For pCounter = 0 To pList.Count - 1
        pItem = pList(pCounter)
        ListBox1.Items.Add(pItem.ProcessName + "   (" + (pItem.PrivateMemorySize64 / 1024000).ToString() + " MB)")        '
    Next
    'tidy up
    pItem = Nothing
    pCounter = Nothing
    '
End Sub

End Class