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.

2

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

exactly which variable will get deleted first?
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?

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.