-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathTotal Memory Usage
42 lines (38 loc) · 1.46 KB
/
Total Memory Usage
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
39
40
41
42
#!/usr/bin/python3
import os
os.chdir('/proc/')
collect_pid = []
pname_pid = []
for i in os.listdir():
if i.isdigit():
collect_pid += [i]
for pid in collect_pid:
try:
fopen = open('/proc/'+str(pid)+'/status')
fread = fopen.readlines()[0].strip().lstrip('Name:').lstrip()
pname_pid += [[int(pid),fread]]
fopen.close()
except (PermissionError, FileNotFoundError):
pass
pname_dict = dict(pname_pid)
pss = []
private_page = []
for pid_no in sorted(pname_dict.keys()):
try:
osmaps = open('/proc/'+str(pid_no)+'/smaps')
for text in osmaps.readlines():
text = text.rstrip()
if text.startswith('Pss'):
pss += [ int(text.lstrip('Pss:').lstrip().rstrip('kB ')) ]
if text.startswith('Private'):
private_page += [ int(text.lstrip ('Private_Clean:').lstrip('Private_Dirty:').lstrip().rstrip('kB ')) ]
except (PermissionError, FileNotFoundError):
pass
Pss = round(sum(pss)/1024)
Private = round(sum(private_page)/1024)
Shared = Pss - Private
Memory_Usage = Private + Shared
print('PSS:', Pss,'MiB')
print('Private Memory:', Private,'MiB')
print('Shared Memory:', Shared,'MiB')
print('Total Memory Usage:', Memory_Usage,'MiB')