-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.py
38 lines (31 loc) · 1006 Bytes
/
memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import psutil
from ctypes import *
from ctypes.wintypes import *
def size(byte):
# this the function to convert bytes into more suitable reading format.
# Suffixes for the size
for x in ["B", "KB", "MB", "GB", "TB"]:
if byte < 1024:
return f"{byte:.2f} {x}"
byte = byte / 1024
def usage():
mem = psutil.virtual_memory()
return {
'total_available': size(mem.total),
'available': size(mem.available),
'used': size(mem.used),
'percent': mem.percent
}
def clear():
for proc in psutil.process_iter():
try:
clear_pid(proc.pid)
clear_pid(proc.pid)
except:
pass
def clear_pid(pid):
handle = windll.kernel32.OpenProcess(DWORD(0x1F0FFF), False, DWORD(pid))
if handle != 0:
windll.kernel32.SetProcessWorkingSetSizeEx(handle, c_size_t(-1), c_size_t(-1), DWORD(0x1))
windll.psapi.EmptyWorkingSet(handle)
windll.kernel32.CloseHandle(handle)