Ram usage

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
noname
Posts: 515
Joined: 19 Nov 2013, 09:15

Re: Ram usage

18 Jan 2018, 09:03

Image

I needed one for chrome to kill some process threads and later added summing up for a user.You can change the name chrome.exe to your process.

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
}
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Ram usage

18 Jan 2018, 13:02

Based on noname's code I've shrinked the Gui to a ToolTip and added a SetTimer-routine so it's refreshing ~ 0.5/sec.
Don't miss to add/c&p the missing section/functions ... ProcessEnum()

Code: Select all

#SingleInstance, Force
SetBatchLines -1

SetTimer, MemCheck, 500
Return


MemCheck:
   ProcessMemory("Chrome") ; TClient
   Return

ProcessMemory(ProcessName) {
   sum:=0
   For k, v in ProcessEnum() {
      If instr(v,ProcessName) {
         i := ProcessMemoryInfo(k)
;        ToolTip % FormatNumComma(i.WorkingSetSize/1024) " KB"
;        Sleep 500
         sum += i.WorkingSetSize/1024
         }
      }
   ToolTip % Format("{1:0.3f}", sum/1024 )  " KB"
   Return
   }
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Ram usage

18 Jan 2018, 19:38

The functions you need (also for Bobo's script) are included in noname's post.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Anput, Nerafius, RandomBoy and 190 guests