-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMemory usage of each processes
45 lines (41 loc) · 1.53 KB
/
Memory usage of each processes
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
43
44
45
#!/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 = []
list_mem = []
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 ')) ]
Pss = round(sum(pss)/1024)
Private = round(sum(private_page)/1024)
Shared = Pss - Private
Memory_Usage = Private + Shared
pss = []
private_page = []
list_mem += [ [ pid_no,pname_dict.get(pid_no),Private,Shared,Memory_Usage ] ]
except (PermissionError,FileNotFoundError):
pass
print("{:>5} {:>9} {:>28} {:>19} {:>21}".format("PID","Process","Private-Memory(MiB)","Shared-Memory(MiB)","Meomory-Usage(MiB)"))
for pid,comm,prim,shm,memu in list(reversed(sorted(list_mem,key = lambda x: x[4]))):
print("{:6d} {:<15} {:>9} {:>17} {:>22}".format(pid,comm,prim,shm,memu))