Python memory management

I declared and initialised variable a with value 10, and variable b with value 20, i could see id of each variable get vary, but when i change the value of variable b to 10, i could see the id of b is same as a. is this due to garbage collection of Pythonn, could you give more insight of gabage collection in pyton?

image

  1. How can i identify which memory management(cpython, jython, etc…) is getting used in my environment(windows. spark cluster etc…)
  2. Is every time if i create new object, will it check for same value if already object exist and make the reference on the same.

Thanks,
karthick

Maybe have a read of: id(object)

Thanks @rob42
yes, if the value are different, id are also getting different. but what i am trying to understand is if the value are same if we are declaring the different object with same value make will reference to same memory location?, is python evrery time during memory allocation looks all decalared variable if the same value present and then allocate the new mormory location?

Could you suggest any visualization tool to get know the memory in computer likt jmeter etc… is there any tool or software available for python?

You’re welcome.

It has nothing to do (so far as I know) with the values that said objects ‘points’ to:

Two objects with non-overlapping lifetimes may have the same id() value.

Mapping computer memory back in the day (I’m talking 40 years back) was not hard, but with modern systems, it’s not so easy, as they use randomized memory locations, for security reasons.

Note: this is at the edges of my understanding, but I don’t think I’m too far off, although I stand to be corrected.

Python allocates “small” integers from -5 to 256 in CPython at startup, which is why id() is returning the same value for 10: Integer Objects — Python 3.11.2 documentation

1 Like

@dtrodrigues , but i could see the happening in for string value, i have mentioned the example in atached images, could you explain me why?

is there any complete structured book available for study memory management in python?

is there any tool available to see the memory allocation in computer?

Python, the language, does not have memory management other than as described for id. (Note that mandating a constant id during the object lifetime is an issue for implementations that have relocating garbage collection.) The CPython implementation currently interns at least some strings that look like identifiers and not strings that do not. Such private implementation details can vary from release to release.

>>> s1 = 'abd'
>>> s2 = 'abd'
>>> id(s1), id(s2)
(2497948216432, 2497948216432)  # same
>>> s3 = 'a mix 3*'
>>> s4 = 'a mix 3*'
>>> id(s3), id(s4)
(2497954955056, 2497954952560)  # different

Now explain this current behavior (which I believe was not always thus).

>>> i1 = 1234; i2 = 1234
>>> id(i1), id(i2)
(2497948955472, 2497948955472)  # same
>>> i3 = 4567
>>> i4 = 4567
>>> id(i3), id(i4)
(2497948955312, 2497948949392)  # different

I don’t know the reasoning behind this. Naive use of id is a good way to confuse oneself.

1 Like

Are you just curious or is there a problem that you are trying to solve behind these questions?

When 1234 is used twice in the same physical line of input, the same object gets opportunistically reused. I think(?) this is specific to the REPL, a sort of local, temporary interning.

No. It’s interning.

Every Python value is an object. For efficiency purposes, CPython
preallocates a small range of ints (-5 to 25 maybe), and you’d always
see the same ids for these values (in a given programme run). CPython
also interns small strings on the premise that they are likely to be
reused, so for small strings. You can even intern some strings yourself:

Note that the id() is associated with a value, not a variable name.

It’s a performance efficiency thing; think of it like a small cache for
things thought likely to be reused.

Cheers,
Cameron Simpson cs@cskk.id.au

Since 3.0, ints -5 to 256 are interned at startup to make all byte values + the count 256 preallocated.

At startup, there are 1139 references to 0. sys.getrefcount(0) is 1000001140. Ignore the leading 1 used for internal purposes and subtract 1 for the 0 passed to the function to get 1139.