fsociety
fsociety

Reputation: 1860

How to delete an object in python function?

I am working with very large numpy/scipy arrays that take up a huge junk of memory. Suppose my code looks something like the following:

def do_something(a):
  a = a / a.sum() #new memory is allocated
  #I don't need the original a now anylonger, how to delete it?
  #do a lot more stuff

#a = super large numpy array
do_something(a)
print a #still the same as originally (as passed by value)

So I am calling a function with a huge numpy array. The function then processes the array in some way or the other, but the original object is still kept in memory. Is there any way to free the memory inside the function; deleting the reference does not work.

Upvotes: 1

Views: 3654

Answers (2)

dfranca
dfranca

Reputation: 5322

Python works with a simple GC algorithm, basically it has a reference counting (it has a generational GC too, but that's not the case), that means that every reference to the object increment a counter, and every object out of scope decrement the scope. The memory is deallocated only after the counter reach 0.

so while you've a reference to that object, it'll keep on memory.

In your case the caller of do_something still have a reference to the object, if you want that this variable gone you can reduce the scope of that variable.

If you suspect of memory leaks you can set the DEBUG_LEAK flag and inspect the output, more info here: https://docs.python.org/2/library/gc.html

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121564

What you want cannot be done; Python will only free the memory when all references to the array object are gone, and you cannot delete the a reference in the calling namespace from the function.

Instead, break up your problem into smaller steps. Do your calculations on a with one function, delete a then, then call another function to do the rest of the work.

Upvotes: 3

Related Questions