; 以自身脚本进程为例
MsgBox % GetWorkingSetPrivateSize(PID := DllCall("GetCurrentProcessId"))

; EmptyMem("", 5000)  ; 延时5秒后清理内存

; 清理进程占用内存
EmptyMem(PID="" ,Period:=""){
  if Period is Number
  {   ; 实现异步延时清理进程占用内存
    s:=Func(A_ThisFunc).Bind(PID, "Asynchronous")
    SetTimer %s%, % "-" Period
    Return
  }
  pid:=!PID ? DllCall("GetCurrentProcessId") : pid
  , h:=DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", pid)
  , DllCall("SetProcessWorkingSetSize", "UInt", h, "Int", -1, "Int", -1)
  , DllCall("CloseHandle", "Int", h)
}

; 获取程序所占用内存  By 蜜獾哥
GetWorkingSetPrivateSize(PID,Units:= "K"){
  if (DllCall("ntdll.dll\NtQuerySystemInformation", "int", 0x5, "ptr", 0, "uint", 0, "uint*", size) != 0) {
    size := VarSetCapacity(buf, size << 1, 0)
    if (DllCall("ntdll\NtQuerySystemInformation", "int", 0x5, "ptr", &buf, "uint", size, "uint*", 0) != 0){
      memory:=(ErrorLevel := 2) & 0
      return (Units="M"&&memory?(memory//1024) . "M":memory "K")
    }
    addr := &buf
    while (addr) {
      if (NumGet(addr + 80, "ptr") = PID){
        memory:=NumGet(addr + 8, "int64")//1024
        return (Units="M"&&memory?(memory//1024) . "M":memory "K")
      }
      if !(NumGet(addr + 0, "uint"))
        break
      addr+= NumGet(addr + 0, "uint")
    }
  }
  memory:=(ErrorLevel:= 1) & 0
  return (Units="M"&&memory?(memory//1024) . "M":memory "K")
}
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。