Results 1 to 13 of 13

Thread: Individual Process Memory Usage

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    18

    Individual Process Memory Usage

    I searched through the forums and I didn't find anything except one to tell either the global memory usage or the CPU usage.

    Well, I heard that this is difficult to do in Visual Basic, but I am searching to get the individual amount of memory used by a process, which is then displayed in a caption.

    Then, the process's memory is re-read and if the change in the memory usage is over...say...1000 or whatnot the process is terminated.

    I've got the main parts of it down, but I'm having a difficult time figuring out how to get the amount of memory just one single process is using.


    For an example, say you have a list which lists all the process currently active on your computer. You click on a process, and it shows you how much memory it is currently using (like in the task manager) - then it re-reads this after a while and writes the new memory usage. If the difference between the new and the old memory usages is greater than a certain number, just kill off the process.

    But, I can't just figure out how to do it! Mind you, compared to most people here, I'm a newbie. ^_^


    Thanks in advance, and remember, Darkness is your friend! =D

  2. #2
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Re: Individual Process Memory Usage

    if you shell tasklist.exe, it will output a list of all processes, along with the mem usage.
    you can redirect this dos output to a text file, and manipulate from there.
    simple and effective, but requires diskaccess, which may or may not be a concern.

    you could also use the consoole module in the FAQ section to avoid disk usage, at the cost of complexity.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    18

    Re: Individual Process Memory Usage

    I'm afraid that doesn't help me very much, I didn't see anything that I knew how to use...

  4. #4
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Individual Process Memory Usage

    This gets a certain Process Memory Usage ..
    It uses the EXE name in this case .. you can change accordingly. .

    If you have 2 of the same EXE open then it uses the first one.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Debug.Print GetProcessMemory("vb6.exe")
    5. End Sub
    6.  
    7. Private Function GetProcessMemory(ByVal app_name As String) As String
    8.     Dim Process As Object
    9.     Dim dMemory As Double
    10.     For Each Process In GetObject("winmgmts:").ExecQuery("Select WorkingSetSize from Win32_Process Where Name = '" & app_name & "'")
    11.         dMemory = Process.WorkingSetSize
    12.     Next
    13.     If dMemory > 0 Then
    14.         GetProcessMemory = ResizeKb(dMemory)
    15.     Else
    16.         GetProcessMemory = "0 Bytes"
    17.     End If
    18. End Function
    19.  
    20. Private Function ResizeKb(ByVal b As Double) As String
    21.     Dim bSize(8) As String, i As Integer
    22.     bSize(0) = "Bytes"
    23.     bSize(1) = "KB" 'Kilobytes
    24.     bSize(2) = "MB" 'Megabytes
    25.     bSize(3) = "GB" 'Gigabytes
    26.     bSize(4) = "TB" 'Terabytes
    27.     bSize(5) = "PB" 'Petabytes
    28.     bSize(6) = "EB" 'Exabytes
    29.     bSize(7) = "ZB" 'Zettabytes
    30.     bSize(8) = "YB" 'Yottabytes
    31.     For i = UBound(bSize) To 0 Step -1
    32.         If b >= (1024 ^ i) Then
    33.             ResizeKb = ThreeNonZeroDigits(b / (1024 ^ _
    34.                 i)) & " " & bSize(i)
    35.             Exit For
    36.         End If
    37.     Next
    38. End Function
    39.  
    40. Private Function ThreeNonZeroDigits(ByVal value As Double) As Double
    41.     If value >= 100 Then
    42.         ThreeNonZeroDigits = FormatNumber(value)
    43.     ElseIf value >= 10 Then
    44.         ThreeNonZeroDigits = FormatNumber(value, 1)
    45.     Else
    46.         ThreeNonZeroDigits = FormatNumber(value, 2)
    47.     End If
    48. End Function
    Last edited by rory; Oct 16th, 2006 at 02:06 AM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    18

    Re: Individual Process Memory Usage

    Quote Originally Posted by rory
    This gets a certain Process Memory Usage ..
    It uses the EXE name in this case .. you can change accordingly. .

    If you have 2 of the same EXE open then it uses the first one.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Debug.Print GetProcessMemory("vb6.exe")
    5. End Sub
    6.  
    7. Private Function GetProcessMemory(ByVal app_name As String) As String
    8.     Dim Process As Object
    9.     Dim dMemory As Double
    10.     For Each Process In GetObject("winmgmts:").ExecQuery("Select WorkingSetSize from Win32_Process Where Name = '" & app_name & "'")
    11.         dMemory = Process.WorkingSetSize
    12.     Next
    13.     If dMemory > 0 Then
    14.         GetProcessMemory = ResizeKb(dMemory)
    15.     Else
    16.         GetProcessMemory = "0 Bytes"
    17.     End If
    18. End Function
    19.  
    20. Private Function ResizeKb(ByVal b As Double) As String
    21.     Dim bSize(8) As String, i As Integer
    22.     bSize(0) = "Bytes"
    23.     bSize(1) = "KB" 'Kilobytes
    24.     bSize(2) = "MB" 'Megabytes
    25.     bSize(3) = "GB" 'Gigabytes
    26.     bSize(4) = "TB" 'Terabytes
    27.     bSize(5) = "PB" 'Petabytes
    28.     bSize(6) = "EB" 'Exabytes
    29.     bSize(7) = "ZB" 'Zettabytes
    30.     bSize(8) = "YB" 'Yottabytes
    31.     For i = UBound(bSize) To 0 Step -1
    32.         If b >= (1024 ^ i) Then
    33.             ResizeKb = ThreeNonZeroDigits(b / (1024 ^ _
    34.                 i)) & " " & bSize(i)
    35.             Exit For
    36.         End If
    37.     Next
    38. End Function
    39.  
    40. Private Function ThreeNonZeroDigits(ByVal value As Double) As Double
    41.     If value >= 100 Then
    42.         ThreeNonZeroDigits = FormatNumber(value)
    43.     ElseIf value >= 10 Then
    44.         ThreeNonZeroDigits = FormatNumber(value, 1)
    45.     Else
    46.         ThreeNonZeroDigits = FormatNumber(value, 2)
    47.     End If
    48. End Function

    I love you.

    If you don't mind me asking, how did you find this? o_o

    If you wrote it yourself, you're truly amazing!

    Thank you very much, I appreciate it.

  6. #6
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Individual Process Memory Usage

    The process stuff itself is scripting .. lots of searching and testing ... i just threw the above code together myself right now though .. if you want all the Process Fields they are posted below ..

    The other 2 Functions found them on the web somewhere ... just had to edit them some from the original. Actually been using them in ASP (VBscript) so had to edit them back to VB6.


    Process.Caption
    Process.CommandLine
    Process.CreationClassName
    Process.CreationDate
    Process.CSCreationClassName
    Process.CSName
    Process.Description
    Process.ExecutablePath
    Process.Handle
    Process.HandleCount
    Process.KernelModeTime
    Process.MaximumWorkingSetSize
    Process.MinimumWorkingSetSize
    Process.Name
    Process.OSCreationClassName
    Process.OSName
    Process.OtherOperationCount
    Process.OtherTransferCount
    Process.PageFaults
    Process.PageFileUsage
    Process.ParentProcessId
    Process.PeakPageFileUsage
    Process.PeakVirtualSize
    Process.Priority
    Process.PrivatePageCount
    Process.ProcessId
    Process.QuotaNonPagedPoolUsage
    Process.QuotaPagedPoolUsage
    Process.QuotaPeakNonPagedPoolUsage
    Process.QuotaPeakPagedPoolUsage
    Process.ReadOperationCount
    Process.ReadTransferCount
    Process.SessionId
    Process.ThreadCount
    Process.UserModeTime
    Process.VirtualSize
    Process.WindowsVersion
    Process.WorkingSetSize
    Process.WriteOperationCount
    Process.WriteTransferCount

  7. #7
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: Individual Process Memory Usage

    this shows all process info in a richtext box.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    18

    Re: Individual Process Memory Usage

    Quote Originally Posted by rory
    this shows all process info in a richtext box.

    Again, I truly owe you one.

    Thank you very, very, very much!

  9. #9
    New Member
    Join Date
    Jun 2012
    Posts
    2

    Re: Individual Process Memory Usage

    Hi, I know this hasn't been looked at for a while but i just stumbled across it and and its pretty much what I've been looking for the only problem is when i tired to use it i got this error,

    Function 'ResizeKb' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.

    Sorry im kinda new to this, please could you let me know where i went wrong?
    if it helps im trying to use it in visual studio 2010



    Thanks Jason

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Individual Process Memory Usage

    The code is for VB6 not VB.Net
    The message you refer to is a warning not an error

  11. #11
    New Member
    Join Date
    Jun 2012
    Posts
    2

    Re: Individual Process Memory Usage

    Ok yes thats fair enough but is there any way to get it to work in VB.Net? because at the moment its only returning 0 bytes? sorry i realise this is not VB6 its just very close to what ive been looking for.


    Thanks Jason

  12. #12
    New Member
    Join Date
    Jun 2013
    Posts
    1

    Re: Individual Process Memory Usage... urgent reply needed

    Quote Originally Posted by rory View Post
    This gets a certain Process Memory Usage ..
    It uses the EXE name in this case .. you can change accordingly. .

    If you have 2 of the same EXE open then it uses the first one.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Debug.Print GetProcessMemory("vb6.exe")
    5. End Sub
    6.  
    7. Private Function GetProcessMemory(ByVal app_name As String) As String
    8.     Dim Process As Object
    9.     Dim dMemory As Double
    10.     For Each Process In GetObject("winmgmts:").ExecQuery("Select WorkingSetSize from Win32_Process Where Name = '" & app_name & "'")
    11.         dMemory = Process.WorkingSetSize
    12.     Next
    13.     If dMemory > 0 Then
    14.         GetProcessMemory = ResizeKb(dMemory)
    15.     Else
    16.         GetProcessMemory = "0 Bytes"
    17.     End If
    18. End Function
    19.  
    20. Private Function ResizeKb(ByVal b As Double) As String
    21.     Dim bSize(8) As String, i As Integer
    22.     bSize(0) = "Bytes"
    23.     bSize(1) = "KB" 'Kilobytes
    24.     bSize(2) = "MB" 'Megabytes
    25.     bSize(3) = "GB" 'Gigabytes
    26.     bSize(4) = "TB" 'Terabytes
    27.     bSize(5) = "PB" 'Petabytes
    28.     bSize(6) = "EB" 'Exabytes
    29.     bSize(7) = "ZB" 'Zettabytes
    30.     bSize(8) = "YB" 'Yottabytes
    31.     For i = UBound(bSize) To 0 Step -1
    32.         If b >= (1024 ^ i) Then
    33.             ResizeKb = ThreeNonZeroDigits(b / (1024 ^ _
    34.                 i)) & " " & bSize(i)
    35.             Exit For
    36.         End If
    37.     Next
    38. End Function
    39.  
    40. Private Function ThreeNonZeroDigits(ByVal value As Double) As Double
    41.     If value >= 100 Then
    42.         ThreeNonZeroDigits = FormatNumber(value)
    43.     ElseIf value >= 10 Then
    44.         ThreeNonZeroDigits = FormatNumber(value, 1)
    45.     Else
    46.         ThreeNonZeroDigits = FormatNumber(value, 2)
    47.     End If
    48. End Function
    I tried this code in visual basic 2010, but it always shows 0bytes... please modify this code for visual basic 2010

  13. #13
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Individual Process Memory Usage

    @ harish3092 and (in case you're still interested) Nethacker-010

    Have a look at the GetProcessMemoryInfo API function. And since you're both using VB.NET, try asking in the VB.NET forum.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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