0

The official document says, "The tracemalloc module is a debug tool to trace memory blocks allocated by Python."

Python uses memory management as Arena and stores through pools and blocks.

However, even if python actually free the block, python do not return the memory directly to the os. All blocks in the pool must be free to return the memory.

So is it possible that tracemalloc only tracks Python's free, but doesn't know that memory is returned to the actual os, and that Python is actually using more memory than it is tracked?

1 Answer 1

1

Yes, tracemalloc does not tell you how much memory Python has taken from the OS

It only tells you the memory that Python has currently allocated to store variables.

  1. Python requests memory from the OS in arenas that are typically 256k, much bigger than the variables themselves.

  2. When a block (part of a pool) is no longer needed by Python, but other blocks in that pool are still in use, Python cannot release the pool to the OS. It can't release the pool until all pools in the arena are free. It is rare for that to happen, because there is almost always something in an arena that is still needed. An exception might be a function that creates large temporary variables and then discards them all without making any other use of that arena.

More information here:

https://rushter.com/blog/python-memory-managment/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.