Does python do similar objects in adjacent memory cells?

Does Python store similar objects in memory cells closer to each other? Since the id such objects, such as lists and tuples , is closer to each other than an object of type str .

+4
source share
1 answer

No, except, of course, a coincidence. Although this is very specific to the implementation and the environment, and in fact there are memory management schemes that allocate page-size memory areas for objects of the same type, no Python implementations that I know of demonstrate the behavior you are describing. With the possible exception of small numbers, which are sometimes cached under the hood and are likely to be located next to each other.

What you see may be due to the fact that string literals are created during import (part of the constants in byte code) and interned, while lists and tuples (which do not contain literals) are created when the code is run. If a heap of memory is allocated between them (especially if it is not freed), the state of the heap can be quite different, since completely different addresses are issued during the check.

+4
source

Source: https://habr.com/ru/post/1480717/


All Articles