I have a simple Cython extension that starts tracemalloc, allocates some memory, and prints the memory allocation snapshot details:
import tracemalloc
tracemalloc.start()
def test():
arr = []
for i in range(10000):
arr.append(i)
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('traceback')
for stat in top_stats:
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
for line in stat.traceback.format():
print(line)
However when I compile the extension and run it in the interpreter, I can't see the function names or line numbers where memory was allocated. Instead it is all combined into one <stdin> entry:
9744 memory blocks: 349.5 KiB
File "<stdin>", line 1
13 memory blocks: 9.3 KiB
File "<unknown>", line 0
1 memory blocks: 0.1 KiB
File "<frozen importlib._bootstrap>", line 241
When I run the same code as a regular Python module, I can see the details:
9743 memory blocks: 266.4 KiB
File "test.py", line 7
for i in range(10000):
1 memory blocks: 83.1 KiB
File "test.py", line 8
arr.append(i)
12 memory blocks: 9.3 KiB
File "<unknown>", line 0
1 memory blocks: 0.1 KiB
File "test.py", line 5
def test():
Detailed function names and line numbers are still shown for exception stack traces in Cython extensions. Why aren't they shown for the tracemalloc statistics?