1

In Python, if two variables reference to the same object, does the total size (memory allocation) double or is the total size just the size of the object?

As variables reference to objects, does that mean the same object is not created twice if it's referenced twice?

I tried to get the __sizeof__() but I'm unsure how should I study this myself.

a = "string"
b = a 

Now calling the __sizeof__() method on the string referenced by a will output 55 bytes in Jupyter notebook. B will do the same obviously because it references the same object.

But is the total size of the objects used here 1x or 2x size of object "string"? I don't really trust the file size of a .py or .ipynb on this one to get an accurate answer.


Edit: when does Python allocate new memory for identical strings? addresses only strings. The answer to this question depends partially on the type of the object (especially small integers).

Objects can be either 1) equal in value or 2) equal in object id, respectively == and is checks. For equal in value, the two references might point to the same object or two different objects (of equal value). For equal in object id, the two references point only to one object.

In the first case the total size is two references + one or two objects depending on the implementation. For the second case, there's only two references and one object.

For small integers (and small strings), Python caches them automatically so they are always sharing a reference even if two object assignments are done. Both == and is will evaluate to True.

Same value, same object:

S1 = 'string'
S2 = 'string'
S1 == S2, S1 is S2
#Out: (True, True)

Same value, different object:

S1 = 'a longer string'
S2 = 'a longer string'
S1 == S2, S1 is S2
#Out: (True, False)
raitalt1
  • 13
  • 3
  • There is only one string object in memory. – Code-Apprentice Mar 22 '19 at 21:12
  • 1
    You shouldn't trust the size of the .py file - it has virtually nothing to do with how much memory your program will take up when it's run. – Nathan Vērzemnieks Mar 22 '19 at 21:19
  • Thinking about the space taken by variables isn't very useful - variables technically take space, but the really meaningful space consumption in a Python program is almost always by *objects*, not variables. Variables are just a way to hold references to objects. – user2357112 Mar 22 '19 at 21:21
  • @cglacet, you tell me.. The other answer seems very complicated for a beginner and I'm trying to understand the total memory allocation of shared references in a simple case. I can remove my question if it seems to be too similar. – raitalt1 Mar 22 '19 at 22:18
  • @cglacet That doesn't look like an appropriate duplicate for this question. – Mark Rotteveel Mar 23 '19 at 08:42

1 Answers1

0

Neither, although 1x is closer. The total size is the sum of the target object (your string) plus two references (one address reference -- typically one word of storage -- each). You have a, b, and the string to which they refer.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • "the target variable" is a misleading way to describe a string object, since it's not a variable at all. – user2357112 Mar 22 '19 at 21:22
  • @user2357112: thanks -- I let my brain run far ahead of my fingers. – Prune Mar 22 '19 at 21:28
  • Strings are not any object, what you say is true for objects in general, but not necessarily true for strings. – cglacet Mar 22 '19 at 21:28
  • @cglacet: [Strings are objects in Python.](https://ideone.com/Qj78kC) Python has no concept of non-object primitives. Even an int is an object. – user2357112 Mar 22 '19 at 21:29
  • You're right, my bad. I'm still trying to understand how strings are handled in memory, that actually seems a bit complex: https://stackoverflow.com/questions/2123925/when-does-python-allocate-new-memory-for-identical-strings – cglacet Mar 22 '19 at 21:44