Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to memory locations in Python when you overwrite a variable?

In [1]: class T(object):
   ...:     pass
   ...:

In [2]: x = T()

In [3]: print(x)
<__main__.T object at 0x03328E10>

In [4]: x = T()

In [5]: print(x)
<__main__.T object at 0x03345090>

When is the memory location allocated to the first T() object (0x03328E10) freed? Is it when the variable x is overwritten or when the garbage collector is ran or when the script ends?

I'm assuming it's when the garbage collector runs but I don't know how to test that assumption.

like image 913
Virgiliu Avatar asked Apr 13 '14 16:04

Virgiliu


People also ask

Are the memory addresses of variables the same in Python?

Surprisingly, the memory addresses that both variables point to are the same. Look at another example. I defined two variables ( str1 and str2) and assigned a string object ( Python) to both of them. The memory addresses that both variables point to are the same. If you test the same thing for boolean objects, you will see a similar observation.

How are variables stored in Python – stack or heap?

How are variables stored in Python – Stack or Heap? Memory allocation can be defined as allocating a block of space in the computer memory to a program. In Python memory allocation and deallocation method is automatic as the Python developers created a garbage collector for Python so that the user does not have to do manual garbage collection.

How does Python store data in computer memory?

Computer memory is like a cupboard filled with storage jars. Using Python, you put values in the jars and then you put a label, a variable, on the jar, so you can find your value later.

How does memory management work in Python?

Since the backend for Python is in C, the developers of the Python programming language carefully implemented memory management for python such that we do not have to deal with it. Memory management in python, however, does involve a private heap, and thus it must be handled carefully.


2 Answers

Python memory management for the most part is done via reference counting, rather than garbage collection. When the reference count goes to zero, the memory is available for reallocation.

The garbage collector only comes into play when you have circular references, which isn't the case here.

like image 193
Daniel Roseman Avatar answered Oct 07 '22 04:10

Daniel Roseman


First of all, it depends on what Python implementation you are using. CPython, the reference implementation, does reference counting, that means your assumption is right. You can test that assumption by implementing __del__ method.

However it’s a bad practice to avoid. For example, PyPy, IronPython, and Jython do garbage collection instead, so your assumption is not guaranteed.

So how can resource finalization be implemented in Python? Use context managers instead. Context managers are RAII feature Python offers.

like image 38
minhee Avatar answered Oct 07 '22 04:10

minhee