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
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.
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...
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:
Option Explicit
Private Sub Command1_Click()
Debug.Print GetProcessMemory("vb6.exe")
End Sub
Private Function GetProcessMemory(ByVal app_name As String) As String
Dim Process As Object
Dim dMemory As Double
For Each Process In GetObject("winmgmts:").ExecQuery("Select WorkingSetSize from Win32_Process Where Name = '" & app_name & "'")
dMemory = Process.WorkingSetSize
Next
If dMemory > 0 Then
GetProcessMemory = ResizeKb(dMemory)
Else
GetProcessMemory = "0 Bytes"
End If
End Function
Private Function ResizeKb(ByVal b As Double) As String
Dim bSize(8) As String, i As Integer
bSize(0) = "Bytes"
bSize(1) = "KB" 'Kilobytes
bSize(2) = "MB" 'Megabytes
bSize(3) = "GB" 'Gigabytes
bSize(4) = "TB" 'Terabytes
bSize(5) = "PB" 'Petabytes
bSize(6) = "EB" 'Exabytes
bSize(7) = "ZB" 'Zettabytes
bSize(8) = "YB" 'Yottabytes
For i = UBound(bSize) To 0 Step -1
If b >= (1024 ^ i) Then
ResizeKb = ThreeNonZeroDigits(b / (1024 ^ _
i)) & " " & bSize(i)
Exit For
End If
Next
End Function
Private Function ThreeNonZeroDigits(ByVal value As Double) As Double
If value >= 100 Then
ThreeNonZeroDigits = FormatNumber(value)
ElseIf value >= 10 Then
ThreeNonZeroDigits = FormatNumber(value, 1)
Else
ThreeNonZeroDigits = FormatNumber(value, 2)
End If
End Function
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:
Option Explicit
Private Sub Command1_Click()
Debug.Print GetProcessMemory("vb6.exe")
End Sub
Private Function GetProcessMemory(ByVal app_name As String) As String
Dim Process As Object
Dim dMemory As Double
For Each Process In GetObject("winmgmts:").ExecQuery("Select WorkingSetSize from Win32_Process Where Name = '" & app_name & "'")
dMemory = Process.WorkingSetSize
Next
If dMemory > 0 Then
GetProcessMemory = ResizeKb(dMemory)
Else
GetProcessMemory = "0 Bytes"
End If
End Function
Private Function ResizeKb(ByVal b As Double) As String
Dim bSize(8) As String, i As Integer
bSize(0) = "Bytes"
bSize(1) = "KB" 'Kilobytes
bSize(2) = "MB" 'Megabytes
bSize(3) = "GB" 'Gigabytes
bSize(4) = "TB" 'Terabytes
bSize(5) = "PB" 'Petabytes
bSize(6) = "EB" 'Exabytes
bSize(7) = "ZB" 'Zettabytes
bSize(8) = "YB" 'Yottabytes
For i = UBound(bSize) To 0 Step -1
If b >= (1024 ^ i) Then
ResizeKb = ThreeNonZeroDigits(b / (1024 ^ _
i)) & " " & bSize(i)
Exit For
End If
Next
End Function
Private Function ThreeNonZeroDigits(ByVal value As Double) As Double
If value >= 100 Then
ThreeNonZeroDigits = FormatNumber(value)
ElseIf value >= 10 Then
ThreeNonZeroDigits = FormatNumber(value, 1)
Else
ThreeNonZeroDigits = FormatNumber(value, 2)
End If
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.
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
Re: Individual Process Memory Usage
this shows all process info in a richtext box.
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!
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
Re: Individual Process Memory Usage
The code is for VB6 not VB.Net
The message you refer to is a warning not an error
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
Re: Individual Process Memory Usage... urgent reply needed
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:
Option Explicit
Private Sub Command1_Click()
Debug.Print GetProcessMemory("vb6.exe")
End Sub
Private Function GetProcessMemory(ByVal app_name As String) As String
Dim Process As Object
Dim dMemory As Double
For Each Process In GetObject("winmgmts:").ExecQuery("Select WorkingSetSize from Win32_Process Where Name = '" & app_name & "'")
dMemory = Process.WorkingSetSize
Next
If dMemory > 0 Then
GetProcessMemory = ResizeKb(dMemory)
Else
GetProcessMemory = "0 Bytes"
End If
End Function
Private Function ResizeKb(ByVal b As Double) As String
Dim bSize(8) As String, i As Integer
bSize(0) = "Bytes"
bSize(1) = "KB" 'Kilobytes
bSize(2) = "MB" 'Megabytes
bSize(3) = "GB" 'Gigabytes
bSize(4) = "TB" 'Terabytes
bSize(5) = "PB" 'Petabytes
bSize(6) = "EB" 'Exabytes
bSize(7) = "ZB" 'Zettabytes
bSize(8) = "YB" 'Yottabytes
For i = UBound(bSize) To 0 Step -1
If b >= (1024 ^ i) Then
ResizeKb = ThreeNonZeroDigits(b / (1024 ^ _
i)) & " " & bSize(i)
Exit For
End If
Next
End Function
Private Function ThreeNonZeroDigits(ByVal value As Double) As Double
If value >= 100 Then
ThreeNonZeroDigits = FormatNumber(value)
ElseIf value >= 10 Then
ThreeNonZeroDigits = FormatNumber(value, 1)
Else
ThreeNonZeroDigits = FormatNumber(value, 2)
End If
End Function
I tried this code in visual basic 2010, but it always shows 0bytes... please modify this code for visual basic 2010
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.