Repository navigation
Expand file tree
/
Copy pathmemory_profile.py
More file actions
54 lines (39 loc) 路 1.79 KB
/
Copy pathmemory_profile.py
File metadata and controls
54 lines (39 loc) 路 1.79 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
"""
While CPU profiling (`cProfile`) identifies execution bottlenecks, memory profiling
diagnoses RAM consumption, high allocations, and memory leaks.
Python's built-in `tracemalloc` module tracks memory blocks allocated by Python.
By capturing snapshots before and after operations, developers can compute allocation
deltas line by line to pinpoint which statements consume the most heap space.
"""
import tracemalloc
def allocate_payload(count: int) -> list[str]:
"""Allocate a collection of formatted string records."""
return [f"record_payload_{i}" for i in range(count)]
def main() -> None:
# 1. Start tracking memory allocations
tracemalloc.start()
assert tracemalloc.is_tracing() is True
# 2. Take a baseline snapshot before creating objects
snapshot_before = tracemalloc.take_snapshot()
# 3. Perform an operation that allocates memory on the heap
payload = allocate_payload(5_000)
# 4. Take a second snapshot to evaluate allocation delta
snapshot_after = tracemalloc.take_snapshot()
# 5. Measure current vs peak memory during the tracing session
current_bytes, peak_bytes = tracemalloc.get_traced_memory()
assert current_bytes > 0
assert peak_bytes >= current_bytes
# 6. Compare snapshots grouped by source line number to find top consumers
stats = snapshot_after.compare_to(snapshot_before, "lineno")
assert len(stats) > 0
# The top allocation difference should be our list comprehension in allocate_payload
top_stat = stats[0]
assert top_stat.size_diff > 0
assert top_stat.count_diff > 0
assert "memory_profile.py" in top_stat.traceback.format()[0]
# Clean up memory reference and stop tracing
del payload
tracemalloc.stop()
assert tracemalloc.is_tracing() is False
if __name__ == "__main__":
main()