1

If we create 2 variables a followed by b, then how these variable gets destroy in python heap. We want to understand the memory management done by python interpreter.

Akshada
  • 65
  • 7

2 Answers2

2

Simply putting, Python memory manager counts references to the existing variables and if reference count is equal to zero, garbage collector automatically de-allocates the space for that variable.

I'd recommend following resources to better understand the flow:

  1. https://realpython.com/python-memory-management/
  2. https://docs.python.org/3/c-api/memory.html
0

Order is undefined as python has a garbage collector. To trace the order you may add a __del__ method which is known as a destructor method in Python. It is called when there are no references to the object and object is garbage collected.

See What is the __del__ method, How to call it?

Łukasz Ślusarczyk
  • 1,775
  • 11
  • 20