Suppose I have a variable x and then try del x. Does this free the allocated memory immediately, or will it still wait for the garbage collector to collect the object at some later point (like in Java)?
-
The point of garbage collection is that you don't have to worry about when the memory is freed. So why are you worrying about it?Waleed Khan– Waleed Khan2013-02-19 23:44:50 +00:00Commented Feb 19, 2013 at 23:44
-
2I'm processing large volume of traffic and observed memory leak issues. I'm almost certain the issue is not in the python script, but just trying to make sure.totoromeow– totoromeow2013-02-20 01:26:48 +00:00Commented Feb 20, 2013 at 1:26
4 Answers
The del statement doesn't reclaim memory. It removes a reference, which decrements the reference count on the value. If the count is zero, the memory can be reclaimed. CPython will reclaim the memory immediately, there's no need to wait for the garbage collector to run.
In fact, the garbage collector is only needed for reclaiming cyclic structures.
As Waleed Khan says in his comment, Python memory management just works, you don't have to worry about it.
12 Comments
del reference causes the memory to be freed, so garbage collection is getting called, and (though there are no time guarantees on this) the interpreter is giving that memory back to the system. If I instead read my table and don't assign it to a variable, then it is an island, and I have to call gc.collect() explicitly to find it and free.del x doesn't do anything different for garbage collection than x = None does. It just removes a reference from x's value.Also, the del statement seems to be a little bit faster than assigning None (similar to Java's style assigning null to a variable to free its memory ...).
To compare:
import time, math
def measure_del():
start = time.time()
for i in range(0,int(math.pow(10,8))):
a = "123"
del a # <--- !!!
end = time.time()
print(end-start)
def measure_none():
start = time.time()
for i in range(0,int(math.pow(10,8))):
a = "123"
a = None # <--- !!!
end = time.time()
print(end-start)
results in (running in idle3.4):
>>> measure_del()
3.9930295944213867
>>> measure_del()
3.7402305603027344
>>> measure_del()
3.8423104286193848
>>> measure_del()
3.753770351409912
>>> measure_del()
3.7772741317749023
>>> measure_del()
3.815058946609497
>>> measure_none()
4.052351236343384
>>> measure_none()
4.130320072174072
>>> measure_none()
4.082390069961548
>>> measure_none()
4.100180625915527
>>> measure_none()
4.071730375289917
>>> measure_none()
4.136169672012329
3 Comments
Regarding delete: Sometimes you have to work on large datasets where you have to compute memory-intensive operations and store a large amount of data into a variable in a recursive manner. To save RAM, when you finish your entire operation, you should delete the variable if you are no more using it outside the recursive loop. You can use the command
del varname followed by Python’s garbage collector gc.collect()
Regarding speed: Speed is the most important in applications such as financial applications with a regulatory requirement. You have to make sure that the speed of operation is completed within the expected timeframe.