Reputation: 153
We have variable in our script which has values like this:
result = {abc: [], xyz: []}
Now we create another variable like:
abc = result['abc']
So what will be the memory usage by this script. Will all the data from result will be copied in abc variable or just reference of that data? Will this increase my memory usage?
Thanks in advance.
Upvotes: 1
Views: 791
Reputation: 11070
Yes, all variables increase memory usage. This is because references need to store the memory address of what they refer to. However, references use a negligible about of memory (4 bytes on a 32-bit machine, 8 bytes on a 64-bit machine) compared to copying/creating a new list. Please see Ned Batchelder 's article on how this all works.
To answer your question on whether or not a copy of the list will be made: no. Whenever you assign a variable to another variable of an object, the object is not copied. As chepner answered in a comment on your question, abc
and result['abc']
both point to the same list, so the list is not copied in memory.
(For future visitors: The original question has many comments that may be useful. If you can, I recommend you read through those too.)
Upvotes: 1