-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpidmon.py
More file actions
46 lines (37 loc) · 1.08 KB
/
Copy pathpidmon.py
File metadata and controls
46 lines (37 loc) · 1.08 KB
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
46
"""
Created on 5 May 2015
@author: paulross
A simple memory monitoring tool.
"""
import sys
import time
import psutil
def memory_monitor(process_id: int, frequency: float = 1.0) -> None:
"""Print out memory usage of a process at regular intervals."""
proc = psutil.Process(process_id)
print(proc.memory_info_ex())
prev_mem = None
while True:
try:
mem = proc.memory_info().rss / 1e6
if prev_mem is None:
print('{:10.3f} [Mb]'.format(mem))
else:
print('{:10.3f} [Mb] {:+10.3f} [Mb]'.format(mem, mem - prev_mem))
prev_mem = mem
time.sleep(frequency)
except KeyboardInterrupt:
try:
input(' Pausing memMon, <cr> to continue, Ctrl-C to end...')
except KeyboardInterrupt:
print('\n')
return
def main() -> int:
if len(sys.argv) < 2:
print('Usage: python pidmon.py <PID>')
return -1
pid = int(sys.argv[1])
memory_monitor(pid)
return 0
if __name__ == '__main__':
exit(main())