Can you modify this script to add the memory usage of ALL processes named "chrome.exe"? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbquest2
Posts: 10
Joined: 21 Aug 2017, 05:57

Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

21 Aug 2017, 13:01

Hi everyone:

I followed the example here, and simply copied the example by @jNizM:

Code: Select all

MsgBox % GetProcessMemory_All("chrome.exe")
Into the code pasted by @kon.

My resulting complete code is:

Code: Select all

;=================================================================================================

============
; Func: GetProcessMemory_Private
; Get the number of private bytes used by a specified process.  Result is in K by default, but can 

also be in
; bytes or MB.
;
; Params:
;   ProcName    - Name of Process (e.g. Firefox.exe)
;   Units       - Optional Unit of Measure B | K | M.  Defaults to K (Kilobytes)
;
; Returns:
;   Private bytes used by the process
;-------------------------------------------------------------------------------------------------

------------
GetProcessMemory_Private(ProcName, Units="K") {
    Process, Exist, %ProcName%
    pid := Errorlevel

    ; get process handle
    hProcess := DllCall( "OpenProcess", UInt, 0x10|0x400, Int, false, UInt, pid )

    ; get memory info
    PROCESS_MEMORY_COUNTERS_EX := VarSetCapacity(memCounters, 44, 0)
    DllCall( "psapi.dll\GetProcessMemoryInfo", UInt, hProcess, UInt, &memCounters, UInt, 

PROCESS_MEMORY_COUNTERS_EX )
    DllCall( "CloseHandle", UInt, hProcess )

    SetFormat, Float, 0.0 ; round up K

    PrivateBytes := NumGet(memCounters, 40, "UInt")
    if (Units == "B")
        return PrivateBytes
    if (Units == "K")
        Return PrivateBytes / 1024
    if (Units == "M")
        Return PrivateBytes / 1024 / 1024
}


;=================================================================================================

============
; Func: GetProcessMemory_All
; Get all Process Memory Usage Counters.  Mimics what's shown in Task Manager.
;
; Params:
;   ProcName    - Name of Process (e.g. Firefox.exe)
;
; Returns:
;   String with all values in KB as one big string.  Use a Regular Expression to parse out the 

value you want.
;-------------------------------------------------------------------------------------------------

------------
GetProcessMemory_All(ProcName) {
    Process, Exist, %ProcName%
    pid := Errorlevel

    ; get process handle
    hProcess := DllCall( "OpenProcess", UInt, 0x10|0x400, Int, false, UInt, pid )

    ; get memory info
    PROCESS_MEMORY_COUNTERS_EX := VarSetCapacity(memCounters, 44, 0)
    DllCall( "psapi.dll\GetProcessMemoryInfo", UInt, hProcess, UInt, &memCounters, UInt, 

PROCESS_MEMORY_COUNTERS_EX )
    DllCall( "CloseHandle", UInt, hProcess )

    list := "cb,PageFaultCount,PeakWorkingSetSize,WorkingSetSize,QuotaPeakPagedPoolUsage"
          . ",QuotaPagedPoolUsage,QuotaPeakNonPagedPoolUsage,QuotaNonPagedPoolUsage"
          . ",PagefileUsage,PeakPagefileUsage,PrivateUsage"

    n := 0
    Loop, Parse, list, `,
    {
        n += 4
        SetFormat, Float, 0.0 ; round up K
        this := A_Loopfield
        this := NumGet( memCounters, (A_Index = 1 ? 0 : n-4), "UInt") / 1024

        ; omit cb
        If A_Index != 1
            info .= A_Loopfield . ": " . this . " K" . ( A_Loopfield != "" ? "`n" : "" )
    }

    Return "[" . pid . "] " . pname . "`n`n" . info ; for everything
}


MsgBox % GetProcessMemory_All("chrome.exe")

Note that this supposedly just works on 32-bit windows, which I have. When I run it it prints this:

picture of what it prints


So I have two issues:

1) It seems to show the memory for ONE of the processes?

2) I don't really get which line shows the memory, and of which process.

Anyway, regardless of that, what I would LIKE from the above code is for it to add up all of the memory of every process named "Chrome".

And just print the sum.

If I can have a pony I'd like a "stop" and "start" where if you click "start" it auto-updates the memory usage every n seconds. And a slider for setting how many seconds "n" is so that I can have the timer fire infrequently if I don't want the program very active and getting foreground.

But forget the pony. How can I just achieve the sum?

For example, in the screenshot pictured the sum should be approximately 3868+496+1344+1228+12512+206408+244+18756+1504+1896+1548+7432+22796+8448+1724+4324+20572+1308+12896+1148+57060+1144+85528+4436+29752+2412+1216+1384+1292+4252+34840+1520+12236 which is 567,524 KB.

I'd the program to print this. And if you want to make magic rainbow ponies, to refresh every n seconds with a slider to input n from 1 to like 200:) But no ponies, no problem.

Thanks so much!
newbquest2
Posts: 10
Joined: 21 Aug 2017, 05:57

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

22 Aug 2017, 21:56

I'd like to give this a bump. Also if you have any other solution please let me know! If it is easy to read the task manager directly (like if you can just read the values in the list) and add it up that way that might work as well. But this (concentrate just on the task manager) this doesn't seem like an easy format to read programmatically.... (to read the listbox or whatever directly).... Any ideas?
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

23 Aug 2017, 06:11

Recently i had a popup blocking all access to close it ,you could only click to redirected to a site claiming there was a virus found.( i reported it to adguard and they included it in their signatures) I could only close it by killing the chrome.exe with the highest memory use so i made this ( unfortunately i did not find the original post anymore that i used modified) .I only needed workingmemory but all other memory values are avaiable in the array so it easy to sum them if you want.

Code: Select all

SetBatchLines -1
ProcessPrivilege(DllCall("Kernel32.dll\GetCurrentProcessId"), "SeDebugPrivilege")

Gui, Add, ListView, x0 y0 w615 h320 Sort gMyListView, PID|Name|PrivateUsage|WorkingSetSize
GuiControl, -Redraw, SysListView321
IL_Destroy(ImageList)
, ImageList := IL_Create()
, LV_Delete()
, LV_SetImageList(ImageList)
for k, v in ProcessEnum()
{
  if instr(v,"chrome")
  {
	if !(IL_Add(ImageList, ProcessPath(k)))
		IL_Add(ImageList, A_WinDir "\explorer.exe")
	LV_Add("Icon" A_Index, k, v, FormatNumComma((i:=ProcessMemoryInfo(k)).PrivateUsage/1024) " KB"
			, FormatNumComma(i.WorkingSetSize/1024) "KB")
			}
}

 LV_ModifyCol(1, "AutoHdr"), LV_ModifyCol(2, "AutoHdr")
GuiControl, +Redraw, SysListView321
Gui, Show, w618 h323, Process Memory Usage
SetTimer, Update, 2000
return

MyListView:
if A_GuiEvent = DoubleClick
{
    LV_GetText(RowText, A_EventInfo,1)  
    msgbox,260,,%rowtext% kill application?
    IfMsgBox, yes
    {
    Process, close,%rowtext%
    sleep 2000

    }
}
return 

GuiEscape:
GuiClose:
ExitApp
return

Update:
SetTimer,, Off
Loop, % LV_GetCount() {
	LV_GetText(PID, A_Index, 1)
	LV_Modify(A_Index,,,, FormatNumComma((i:=ProcessMemoryInfo(PID)).PrivateUsage/1024) " KB"
	, FormatNumComma(i.WorkingSetSize/1024) "KB")
} SetTimer,, On
return

ProcessEnum() {
	dwSize := VarSetCapacity(lppe, A_PtrSize=4?296:304, 0), NumPut(dwSize, lppe, 0, "UInt")
	, hSnapshot := DLLCall("Kernel32.dll\CreateToolhelp32Snapshot", "UInt", 0x00000002, "UInt", 0)
	, DllCall("Kernel32.dll\Process32First", "Ptr", hSnapshot, "Ptr", &lppe), List := [],r:=hSnapshot?0:1
	Loop
		List[NumGet(lppe, 8, "UInt")] := StrGet(&lppe+(A_PtrSize=4?36:44), 260, "CP0")
	until !(DllCall("Kernel32.dll\Process32Next", "Ptr", hSnapshot, "UInt", &lppe))
	return List, DllCall("Kernel32.dll\CloseHandle", "Ptr", hSnapshot), ErrorLevel := r
}

ProcessMemoryInfo(ProcessName) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0010|0x0400, "UInt", 0, "UInt", ProcessId)
	, nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 72 : 40), NumPut(nSize, memCounters)
	if !(DllCall("Psapi.dll\GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize)) {
		memCounters := "", nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 80 : 44), NumPut(nSize, memCounters)
		if !(DllCall("Kernel32.dll\K32GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize))
			return false, ErrorLevel := true
	} i := {}, i.PageFaultCount := NumGet(memCounters, 4, "UInt")
		, i.PeakWorkingSetSize := NumGet(memCounters, 8, "Ptr")
		, i.WorkingSetSize := NumGet(memCounters, A_PtrSize = 8 ? 16 : 12, "Ptr") 
		, i.QuotaPeakPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 24 : 16, "Ptr")
		, i.QuotaPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 32 : 20, "Ptr")
		, i.QuotaPeakNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 40 : 24, "Ptr")
		, i.QuotaNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 48 : 28, "Ptr")
		, i.PagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 56 : 32, "Ptr")
		, i.PeakPagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 64 : 36, "Ptr")
		, i.PrivateUsage := NumGet(memCounters, A_PtrSize = 8 ? 72 : 40, "Ptr")
	return i, DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess), ErrorLevel := false
}

ProcessExist(ProcessName) {
	Process, Exist, %ProcessName%
	return ErrorLevel
}

ProcessPath(ProcessName) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400|0x0010, "UInt", 0, "UInt", ProcessId)
	, FileNameSize := VarSetCapacity(ModuleFileName, (260 + 1) * 2, 0) / 2
	if !(DllCall("Psapi.dll\GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
		if !(DllCall("Kernel32.dll\K32GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
			DllCall("Kernel32.dll\QueryFullProcessImageNameW", "Ptr", hProcess, "UInt", 1, "Str", ModuleFileName, "UIntP", FileNameSize)
	return ModuleFileName, DllCall("Kernel32.dll\CloseHandle", "Ptr", hProcess)
}

FormatNumComma(lpValue, Locale := 0x0400) {
	Locale := Locale="."?0x0456:Locale=","?0x0409:!Locale?0x0400:Locale
	if !(cchNumber := DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0))
		cchNumber := DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0)
    VarSetCapacity(lpNumberStr, cchNumber * 2)
    if !DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
		DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
    return SubStr(lpNumberStr, 1, StrLen(lpNumberStr) - 3)
}

ProcessPrivilege(ProcessName, Privileges) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400, "UInt", 0, "UInt", ProcessId)
	, DllCall("Advapi32.dll\OpenProcessToken", "Ptr", hProcess, "UInt", 0x0020|0x00000008, "UIntP", hToken)
	, PrivilegesLuid := [], i := 0
	Loop, Parse, % Privileges, `,
		DllCall("Advapi32.dll\LookupPrivilegeValueW", "Ptr", 0, "Str", A_LoopField, "Int64P", lpLuid)
		, PrivilegesLuid[A_Index] := lpLuid
	for k, v in PrivilegesLuid
		ti := "", VarSetCapacity(ti, 16, 0), NumPut(1, ti, 0, "UInt"), NumPut(v, ti, 4, "Int64"), NumPut(2, ti, 12, "UInt")
		, i := i+(DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", hToken, "Int", false, "Ptr", &ti, "UInt", 0, "Ptr", 0, "Ptr", 0)?1:0)
	DllCall("kernel32.dll\CloseHandle", "Ptr", hToken), DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess)
	return i, ErrorLevel := PrivilegesLuid.MaxIndex()!=i
}
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

23 Aug 2017, 08:31

Here are a few of this functions -> Process Explorer (but it's unfinshed atm)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
newbquest2
Posts: 10
Joined: 21 Aug 2017, 05:57

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

23 Aug 2017, 13:17

noname wrote:Recently i had a popup blocking all access to close it ,you could only click to redirected to a site claiming there was a virus found.( i reported it to adguard and they included it in their signatures) I could only close it by killing the chrome.exe with the highest memory use so i made this ( unfortunately i did not find the original post anymore that i used modified) .I only needed workingmemory but all other memory values are avaiable in the array so it easy to sum them if you want.
noname, this is GREAT - it works fantastic for me (example). The only thing is, I am as my name here indicates, an AHK newb. I don't know how to "sum up" a simple array in AHK. Since you wrote the code, is there any chance you could add a text field to the bottom that simple lists: "Sum of all memory:" and sums it up?

I also don't quite understand the difference between the different sizes, but I will accept "workingsetsize", it's fine.

So if you could show the modified script I really believe this would not take you long to add.

I'll give you a chance to respond then if I can't figure it out (ahk syntax is a bit hard for me) I will ask the forum. Thanks so much! Obviously anyone else can reply too, then I will modify this to show that I have had the answer. Thank you.
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

24 Aug 2017, 02:47

This is a quick change of the code ,i hope i did not break anything else............
As it uses "format()" you need a recent version of ahk .

Be aware that this still contains the "kill" process when you double click a row !!


Update: Found the original post ! https://autohotkey.com/boards/viewtopic.php?t=9556 so thanks to RhCP and Flipeador :)

Code: Select all

SetBatchLines -1
ProcessPrivilege(DllCall("Kernel32.dll\GetCurrentProcessId"), "SeDebugPrivilege")

Gui, Add, ListView, x0 y0 w615 h320  gMyListView, PID|Name|WorkingSetSize
GuiControl, -Redraw, SysListView321

for k, v in ProcessEnum()
{
if instr(v,"chrome")
	LV_Add("", k, v,"")
}
LV_Add("", "", "","","")
LV_Add("", "sum", "all memory","","")
 LV_ModifyCol(1, "AutoHdr"), LV_ModifyCol(2, "AutoHdr")
GuiControl, +Redraw, SysListView321
Gui, Show, w618 h323, Process Memory Usage
SetTimer, Update, 2000
return

MyListView:
if A_GuiEvent = DoubleClick
{
    LV_GetText(RowText, A_EventInfo,1)  
    msgbox,260,,%rowtext% kill application?
    IfMsgBox, yes
    {
    Process, close,%rowtext%
    sleep 2000

    }
}
return 

GuiEscape:
GuiClose:
ExitApp
return

Update:
c:=sum:=0
SetTimer,, Off
Loop, % LV_GetCount() {
	LV_GetText(PID, A_Index, 1)
	i:=ProcessMemoryInfo(PID)
	if i
	LV_Modify(A_Index,,,, FormatNumComma(i.WorkingSetSize/1024) "KB")
	sum +=i.WorkingSetSize/1024
	
	if (PID="sum")
	LV_Modify(A_Index,,,,Format("{1:0.3f}", sum/1024 )  "KB")
}
SetTimer,, On
return

ProcessEnum() {
	dwSize := VarSetCapacity(lppe, A_PtrSize=4?296:304, 0), NumPut(dwSize, lppe, 0, "UInt")
	, hSnapshot := DLLCall("Kernel32.dll\CreateToolhelp32Snapshot", "UInt", 0x00000002, "UInt", 0)
	, DllCall("Kernel32.dll\Process32First", "Ptr", hSnapshot, "Ptr", &lppe), List := [],r:=hSnapshot?0:1
	Loop
		List[NumGet(lppe, 8, "UInt")] := StrGet(&lppe+(A_PtrSize=4?36:44), 260, "CP0")
	until !(DllCall("Kernel32.dll\Process32Next", "Ptr", hSnapshot, "UInt", &lppe))
	return List, DllCall("Kernel32.dll\CloseHandle", "Ptr", hSnapshot), ErrorLevel := r
}

ProcessMemoryInfo(ProcessName) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0010|0x0400, "UInt", 0, "UInt", ProcessId)
	, nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 72 : 40), NumPut(nSize, memCounters)
	if !(DllCall("Psapi.dll\GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize)) {
		memCounters := "", nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 80 : 44), NumPut(nSize, memCounters)
		if !(DllCall("Kernel32.dll\K32GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize))
			return false, ErrorLevel := true
	} i := {}, i.PageFaultCount := NumGet(memCounters, 4, "UInt")
		, i.PeakWorkingSetSize := NumGet(memCounters, 8, "Ptr")
		, i.WorkingSetSize := NumGet(memCounters, A_PtrSize = 8 ? 16 : 12, "Ptr") 
		, i.QuotaPeakPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 24 : 16, "Ptr")
		, i.QuotaPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 32 : 20, "Ptr")
		, i.QuotaPeakNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 40 : 24, "Ptr")
		, i.QuotaNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 48 : 28, "Ptr")
		, i.PagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 56 : 32, "Ptr")
		, i.PeakPagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 64 : 36, "Ptr")
		, i.PrivateUsage := NumGet(memCounters, A_PtrSize = 8 ? 72 : 40, "Ptr")
	return i, DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess), ErrorLevel := false
}

ProcessExist(ProcessName) {
	Process, Exist, %ProcessName%
	return ErrorLevel
}

ProcessPath(ProcessName) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400|0x0010, "UInt", 0, "UInt", ProcessId)
	, FileNameSize := VarSetCapacity(ModuleFileName, (260 + 1) * 2, 0) / 2
	if !(DllCall("Psapi.dll\GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
		if !(DllCall("Kernel32.dll\K32GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
			DllCall("Kernel32.dll\QueryFullProcessImageNameW", "Ptr", hProcess, "UInt", 1, "Str", ModuleFileName, "UIntP", FileNameSize)
	return ModuleFileName, DllCall("Kernel32.dll\CloseHandle", "Ptr", hProcess)
}

FormatNumComma(lpValue, Locale := 0x0400) {
	Locale := Locale="."?0x0456:Locale=","?0x0409:!Locale?0x0400:Locale
	if !(cchNumber := DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0))
		cchNumber := DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0)
    VarSetCapacity(lpNumberStr, cchNumber * 2)
    if !DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
		DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
    return SubStr(lpNumberStr, 1, StrLen(lpNumberStr) - 3)
}

ProcessPrivilege(ProcessName, Privileges) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400, "UInt", 0, "UInt", ProcessId)
	, DllCall("Advapi32.dll\OpenProcessToken", "Ptr", hProcess, "UInt", 0x0020|0x00000008, "UIntP", hToken)
	, PrivilegesLuid := [], i := 0
	Loop, Parse, % Privileges, `,
		DllCall("Advapi32.dll\LookupPrivilegeValueW", "Ptr", 0, "Str", A_LoopField, "Int64P", lpLuid)
		, PrivilegesLuid[A_Index] := lpLuid
	for k, v in PrivilegesLuid
		ti := "", VarSetCapacity(ti, 16, 0), NumPut(1, ti, 0, "UInt"), NumPut(v, ti, 4, "Int64"), NumPut(2, ti, 12, "UInt")
		, i := i+(DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", hToken, "Int", false, "Ptr", &ti, "UInt", 0, "Ptr", 0, "Ptr", 0)?1:0)
	DllCall("kernel32.dll\CloseHandle", "Ptr", hToken), DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess)
	return i, ErrorLevel := PrivilegesLuid.MaxIndex()!=i
}
newbquest2
Posts: 10
Joined: 21 Aug 2017, 05:57

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

25 Aug 2017, 07:37

noname wrote:This is a quick change of the code ,i hope i did not break anything else............
Heeeey, @noname , thanks! It does seem to be working. But I don't understand something. Why is it still listing processes in the listbox, after I've closed the last Chrome window? The task manager is alphabetized by process name in this screenshot, as you can see there is no more "chrome" (which would be between autohotkey.exe and conhost, as that's where it goes alphabetically) - but there are still some in the listbox? But your sum function correctly prints out 0.000 kb? So what is happening here.... (just to be clear, I've closed every Chrome window - there are none open at all when I too the screenshot.) Thanks!
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?  Topic is solved

25 Aug 2017, 09:16

My original code was only to get a snapshot to find the highest value not really to continuous monitor the processes in real time.
Here is a version for realtime monitoring ( hope nothing else is not working anymore.......... )

Code: Select all

SetBatchLines -1
ProcessPrivilege(DllCall("Kernel32.dll\GetCurrentProcessId"), "SeDebugPrivilege")

Gui, Add, ListView, x0 y0 w300 r20  gMyListView, PID|Name|WorkingSetSize
Gui, Show, w300 , Process Memory Usage
SetTimer, Update, 3000
return

MyListView:
if A_GuiEvent = DoubleClick
{
    LV_GetText(RowText, A_EventInfo,1)  
    msgbox,260,,%rowtext% kill application?
    IfMsgBox, yes
      {
      Process, close,%rowtext%
      sleep 2000
      }
}
return 

GuiEscape:
GuiClose:
ExitApp



update:
LV_Delete()
sum:=0
for k, v in ProcessEnum()
    {
    if instr(v,"chrome")
      {
      i:=ProcessMemoryInfo(k)
      LV_Add("", k, v,FormatNumComma(i.WorkingSetSize/1024) "KB")
      sum +=i.WorkingSetSize/1024
      }
    }
LV_Add("", "", "","","")
LV_Add("", "sum", "all memory", Format("{1:0.3f}", sum/1024 )  "KB")

LV_ModifyCol(1, "AutoHdr"), LV_ModifyCol(2, "AutoHdr")
 
return



ProcessEnum() {
	dwSize := VarSetCapacity(lppe, A_PtrSize=4?296:304, 0), NumPut(dwSize, lppe, 0, "UInt")
	, hSnapshot := DLLCall("Kernel32.dll\CreateToolhelp32Snapshot", "UInt", 0x00000002, "UInt", 0)
	, DllCall("Kernel32.dll\Process32First", "Ptr", hSnapshot, "Ptr", &lppe), List := [],r:=hSnapshot?0:1
	Loop
		List[NumGet(lppe, 8, "UInt")] := StrGet(&lppe+(A_PtrSize=4?36:44), 260, "CP0")
	until !(DllCall("Kernel32.dll\Process32Next", "Ptr", hSnapshot, "UInt", &lppe))
	return List, DllCall("Kernel32.dll\CloseHandle", "Ptr", hSnapshot), ErrorLevel := r
}

ProcessMemoryInfo(ProcessName) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0010|0x0400, "UInt", 0, "UInt", ProcessId)
	, nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 72 : 40), NumPut(nSize, memCounters)
	if !(DllCall("Psapi.dll\GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize)) {
		memCounters := "", nSize := VarSetCapacity(memCounters, A_PtrSize = 8 ? 80 : 44), NumPut(nSize, memCounters)
		if !(DllCall("Kernel32.dll\K32GetProcessMemoryInfo", "Ptr", hProcess, "UInt", &memCounters, "UInt", nSize))
			return false, ErrorLevel := true
	} i := {}, i.PageFaultCount := NumGet(memCounters, 4, "UInt")
		, i.PeakWorkingSetSize := NumGet(memCounters, 8, "Ptr")
		, i.WorkingSetSize := NumGet(memCounters, A_PtrSize = 8 ? 16 : 12, "Ptr") 
		, i.QuotaPeakPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 24 : 16, "Ptr")
		, i.QuotaPagedPoolUsage := NumGet(memCounters, A_PtrSize = 8 ? 32 : 20, "Ptr")
		, i.QuotaPeakNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 40 : 24, "Ptr")
		, i.QuotaNonPagedPoolUsage := NumGet( memCounters, A_PtrSize = 8 ? 48 : 28, "Ptr")
		, i.PagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 56 : 32, "Ptr")
		, i.PeakPagefileUsage := NumGet( memCounters, A_PtrSize = 8 ? 64 : 36, "Ptr")
		, i.PrivateUsage := NumGet(memCounters, A_PtrSize = 8 ? 72 : 40, "Ptr")
	return i, DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess), ErrorLevel := false
}

ProcessExist(ProcessName) {
	Process, Exist, %ProcessName%
	return ErrorLevel
}

ProcessPath(ProcessName) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400|0x0010, "UInt", 0, "UInt", ProcessId)
	, FileNameSize := VarSetCapacity(ModuleFileName, (260 + 1) * 2, 0) / 2
	if !(DllCall("Psapi.dll\GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
		if !(DllCall("Kernel32.dll\K32GetModuleFileNameExW", "Ptr", hProcess, "Ptr", 0, "Str", ModuleFileName, "UInt", FileNameSize))
			DllCall("Kernel32.dll\QueryFullProcessImageNameW", "Ptr", hProcess, "UInt", 1, "Str", ModuleFileName, "UIntP", FileNameSize)
	return ModuleFileName, DllCall("Kernel32.dll\CloseHandle", "Ptr", hProcess)
}

FormatNumComma(lpValue, Locale := 0x0400) {
	Locale := Locale="."?0x0456:Locale=","?0x0409:!Locale?0x0400:Locale
	if !(cchNumber := DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0))
		cchNumber := DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Ptr", 0, "Int", 0)
    VarSetCapacity(lpNumberStr, cchNumber * 2)
    if !DllCall("kernel32.dll\GetNumberFormatEx", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
		DllCall("kernel32.dll\GetNumberFormatW", "UInt", Locale, "UInt", 0, "Str", lpValue, "Ptr", 0, "Str", lpNumberStr, "Int", cchNumber)
    return SubStr(lpNumberStr, 1, StrLen(lpNumberStr) - 3)
}

ProcessPrivilege(ProcessName, Privileges) {
	ProcessId := InStr(ProcessName, ".")?ProcessExist(ProcessName):ProcessName
	, hProcess := DllCall("Kernel32.dll\OpenProcess", "UInt", 0x0400, "UInt", 0, "UInt", ProcessId)
	, DllCall("Advapi32.dll\OpenProcessToken", "Ptr", hProcess, "UInt", 0x0020|0x00000008, "UIntP", hToken)
	, PrivilegesLuid := [], i := 0
	Loop, Parse, % Privileges, `,
		DllCall("Advapi32.dll\LookupPrivilegeValueW", "Ptr", 0, "Str", A_LoopField, "Int64P", lpLuid)
		, PrivilegesLuid[A_Index] := lpLuid
	for k, v in PrivilegesLuid
		ti := "", VarSetCapacity(ti, 16, 0), NumPut(1, ti, 0, "UInt"), NumPut(v, ti, 4, "Int64"), NumPut(2, ti, 12, "UInt")
		, i := i+(DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", hToken, "Int", false, "Ptr", &ti, "UInt", 0, "Ptr", 0, "Ptr", 0)?1:0)
	DllCall("kernel32.dll\CloseHandle", "Ptr", hToken), DllCall("kernel32.dll\CloseHandle", "Ptr", hProcess)
	return i, ErrorLevel := PrivilegesLuid.MaxIndex()!=i
}
newbquest2
Posts: 10
Joined: 21 Aug 2017, 05:57

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

26 Aug 2017, 22:06

noname wrote:...
@noname - thank you! This version you made for me does everything I want. :) Since you've put some effort into this do you want to tell me what 'workingsetsize' really means? That is the part that includes both the part that is used in the page file and the part that is in physical memory, right? And the reason it doesn't match task manager is that task manager only shows physical memory, right?
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

27 Aug 2017, 04:14

Image

If you open Resource Monitor ( win10 pro ) you can see the tooltip with the explanation of memory parameters.It seems taskmanager gives commit values and setworking is the actually used physical memory.

But i am only an amateur ... so i can only give what i see onscreen or google ;)
newbquest2
Posts: 10
Joined: 21 Aug 2017, 05:57

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

28 Aug 2017, 20:23

noname wrote:Image

If you open Resource Monitor ( win10 pro ) you can see the tooltip with the explanation of memory parameters.It seems taskmanager gives commit values and setworking is the actually used physical memory.

But i am only an amateur ... so i can only give what i see onscreen or google ;)
but in your picture all the workingset values are bigger than the commit values! so confusing...
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Can you modify this script to add the memory usage of ALL processes named "chrome.exe"?

28 Aug 2017, 23:34

The commit charge for each process does not include other major contributions to the process's virtual address space, such as mapped files. For this reason, the process's working set (the portion of its address space that can be referenced without incurring a page fault) may be larger than its contribution to total commit charge, and the total commit charge is not inclusive of the total memory (physical or virtual) actually in use.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 193 guests