It depends on the implementation and type of variable. For simple objects like int, there are some optimizations. For example, in CPython, a simple int will reuse the same memory, even after del been used. You cannot count on it, but it illustrates that things are harder than they appear.
Remember that when you del you delete the name, not necessarily an object.
For example:
# x is a np.array and contains a lot of data
It will be more precisely formulated as:
# x references a np.array which contains a lot of data
del will decrease the reference count on this object, but even when it drops to zero, it will not be guaranteed that garbage collection will be collected in the near future.
Have a look at the gc module for an explanation and inspiration. Then think again.
If you get out of memory, you probably have a fundamental problem with your design. Most likely you are loading too much data at a time (try using iterators?), Or maybe your code should be better structured.
I just saw your edit. Do you need all this array in memory at the same time? Can you use a generator?
Another alternative is to use a database such as SQLite or perhaps shelve
cdarke
source share