Search code examples
pythonpython-3.xmemorydata-structuresmemory-management

How does memory allocated for different datatype in python?


I notice that, for integer and string:

a=1 ; b=1
c='abc' ; d='abc'
print(id(a), id(b))
print(id(c), id(d))

In this circumstance, a would share same memory address with b, c would share same memory address with d

On the other hand, for list, set ...etc.:

e=[1,2,3] ; f=[1,2,3]
g=(1,2,3) ; h=(1,2,3)
i={1,2,3} ; j={1,2,3}
k={"a":1,"b":2} ; l={"a":1,"b":2}
print(id(e), id(f))
print(id(g), id(h))
print(id(i), id(j))
print(id(k), id(l))

In this circumstance, e won't share same memory address with f, g won't share same memory address with h ... etc.

So I want to ask:

  • Is there any principle of how python allocated memory for different datatype?
  • Does python official document explain about this ? (I can't find any)

Thank you


Solution

  • Only a partial answer specific to integers: Because of the frequency of use, Python caches then reuses certain integer values in memory. For example, the numbers 1-256 are stored that way which is why different variables pointing at those values will have the same id. Larger numbers, for example 257, are dynamically allocated and will not share the same id despite being immutable.

    Example:

    a = 256
    b = 256
    print(id(a), id(b))
    
    a = 257
    b = 257
    print(id(a), id(b))
    

    Output:

    2038199456 2038199456
    21831936 21832816