I am just curious to know about that in python if we set any variable equals to None. Will it free up the space allocated for it? Also if we use del function Will it free up the space allocated to the variable? Is it just like free function in C++?
Asked
Active
Viewed 33 times
0
-
2No, Python has automatic memory management based on reference counting. It is nothing like C++. `del` simply *deletes the variable*, decreasing the reference count. This may or may not result in an object being reclaimed. `None` is just like any other object, and setting a variable to `None` decreases the reference count of the previous object referenced by that variable. – juanpa.arrivillaga Jul 06 '18 at 08:59
-
@Pain That was just by mistake. I have edited the question. – shivank01 Jul 06 '18 at 09:01
-
I see. So you try to refer in this link: https://stackoverflow.com/questions/19473185/what-is-a-none-value @shivank01 – xPain Jul 06 '18 at 09:08
-
@juanpa.arrivillaga I think the question you have referenced is actually different. I am asking whether putting the python variable equals to None actually free up the memory or not? – shivank01 Jul 06 '18 at 09:16
-
1@shivank01 yes, the answers to that question *go very much in depth* into CPython's memory management strategy. Your answers lie there. And I already told you, there is nothing *special* about `None`, there is no difference between doing `x = []; x = object()` and `x = []; x = None`. You are simply changing what the variables reference. When an object's reference count reaches zero, it will be reclaimed – juanpa.arrivillaga Jul 06 '18 at 09:18
-
@juanpa.arrivillaga Thank you very much for the answer. By the way I would suggest you to put your comment as answer and remove the duplicate as it will be helpful for the stackoverflow community searching for the specific answer to this question. – shivank01 Jul 06 '18 at 09:37
-
@juanpa.arrivillaga, If `it will be reclaimed when an object's reference count reaches zero` then why is this happening. Initially the RAM is 2GB in my machine. After running this `exec("a = [] \nfor i in range(100000000): \n \t a.append(i)")` the RAM is 5GB. After `a=None` the RAM size is 4GB. Then where is the remaining (4-2) 2GB of RAM. I am thinking when we assign None the reference count will be decreased to the list which we assigned initially. Then won't garbage collection run at that instance. – Ram Idavalapati Jul 06 '18 at 10:10
-
@RamIdavalapati read [this](http://effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-delete-a-large-object.htm). Read more [here too](https://stackoverflow.com/questions/15455048/releasing-memory-in-python) – juanpa.arrivillaga Jul 06 '18 at 10:20
-
@RamIdavalapati note, if you are on Python 2, then python keeps a "free list" of integers. Essentially, as an optimization, it keeps a bunch of pre-allocated empty integer objects around, the thought being you frequently create `int` objects, and this avoids the overhead of re-allocating a new `int` object. Unfortunately, this list might not get cleared. – juanpa.arrivillaga Jul 06 '18 at 10:29
-
@juanpa.arrivillaga Please take a look at my previous comment and think about it. – shivank01 Jul 06 '18 at 10:58
-
@juanpa.arrivillaga, yes. In python3 I am getting the memory back when I assign variable to None or delete it but not in python2 – Ram Idavalapati Jul 06 '18 at 11:01