-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_plot.py
More file actions
64 lines (51 loc) · 1.6 KB
/
Copy pathmemory_plot.py
File metadata and controls
64 lines (51 loc) · 1.6 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import datetime
import os
LOG_FILE = "memory.log"
leak_threshold = 20 # MB over 30s
# Ensure log file exists
if not os.path.exists(LOG_FILE):
with open(LOG_FILE, "w") as f:
f.write("")
def read_memory_log():
times = []
usage = []
try:
with open(LOG_FILE, "r") as f:
for line in f:
parts = line.strip().split(",")
if len(parts) != 2:
continue
time_str, mem_str = parts
dt = datetime.datetime.strptime(
time_str.strip(), "%Y-%m-%d %H:%M:%S.%f"
)
times.append(dt)
usage.append(float(mem_str.strip()))
except Exception as e:
print(f"Error reading log: {e}")
return times, usage
fig, ax = plt.subplots()
(line,) = ax.plot([], [], lw=2)
warning_text = ax.text(
0.5, 0.9, "", transform=ax.transAxes, color="red", ha="center", fontsize=12
)
def update(frame):
times, usage = read_memory_log()
if not times:
return (line,)
line.set_data(times, usage)
ax.relim()
ax.autoscale_view()
if len(usage) >= 6 and (usage[-1] - usage[-6]) > leak_threshold:
warning_text.set_text("⚠️ Memory Leak Suspected!")
else:
warning_text.set_text("")
return line, warning_text
ani = animation.FuncAnimation(fig, update, interval=3000)
plt.title("Live Memory Usage (MB)")
plt.xlabel("Time")
plt.ylabel("Used Memory")
plt.tight_layout()
plt.show()