Python - Delete (remove from memory) a variable from inside a function?
Asked Answered
M

4

10

I have to load this massive object A (that can weight almsot 10go) that I need to pass to a function, that extracts from it a parameter B to further exert some heavy computations on it.

A = load(file)

def function(A):    
   B = transorm(A)    
   B = compute(B)
   return(B)

In order to free some memory (as I already had a MemoryError), I'd like to remove A from memory right after its transformation to B. I tried del but it doesn't seem to affect the existence of A at the script level. I also tried del global()["A"] but it says that A is not defined as a global variable.

Is there a way to do it? Thanks!

Mariquilla answered 4/1, 2017 at 10:9 Comment(4)
Perhaps if you load A from inside the function() to begin with, you could take advantage of the fact that it will only 'exist' until the function returns, since it will then go out of scope... somebody correct me if I'm off hereJohnette
Have you tried deleting file after calling the function?Melburn
I would really think about choosing another environment for such a task - without automatic garbage collection. Maybe it's possible to not load the whole object in first place?Mundell
you are most probably working with the file in a wrong way, the problem may be in the load fn and how you are handling the file and the "a" problem is just the result..Fractionize
X
3

del A will simply remove A from the local scope of function (see this answer). A will still persist in the global scope. To remove it from the global scope you can either use a closure (and declare global A) or with python3 you can also use the keyword nonlocal. However this only removes the binding from the scope and does not guarantee that the corresponding memory is freed. This happens when the object is garbage collected. You can force garbage collection via the gc module (see this answer).

However if you are running into memory problems, instead of loading the whole data set, you could maybe use a view onto the data set and only process (load) a part of it at a time (i.e. stream-process the data).

Xylina answered 4/1, 2017 at 10:25 Comment(0)
J
0

Perhaps loading the object from inside the function would work here, since A will go out of scope once the function returns and no longer take up the memory in the same way(A will probably still exist in memory, but that memory should now be available for other use again when needed). Maybe try something like this:

f = file                 # assuming file is not the memory hog

def function_A(file):
    A = load(file)       # A is created in the local scope of the function
    return transform(A)  # A will go out of scope, freeing the memory for use

def function_B(file): 
   B = function_A(file)  # when this returns the memory should be available again
   return compute(B)

Then just call function_B(file)

Johnette answered 4/1, 2017 at 10:16 Comment(0)
T
0

I believe reassigning A within the function could achieve the effect you are looking for.

def function(A):
    B = transform(A)
    A = None
    B = compute(B)
    return(B)
Tupungato answered 4/1, 2017 at 10:19 Comment(3)
Setting A = None inside the function will not change the value of A outside the function after it has returned. A = None there only sets a value for A in the local scope.Johnette
Imagine people that you are at a large business convention or meeting. There are many people wearing write-on name-tags/stickers on their shirts. There are many people at this business function, including a Ms. Sarah, and "Mr. None". Initially, Sarah is wearing a name-tag on her shirt on which says "A". Executing the code A = None is like (1) taking the "A" name-tag off Ms. Sarah (2) sticking the "A" name-tag onto Mr. None. Executing the code A = None does not remove Sarah from the convention. Before, Sarah's name-tag said, "A."Gertudegerty
Afterwards, Sarah has no name-tag at all, the A name-tag is moved to Mr. None's shirt .Gertudegerty
C
0

declare the outside variable as global

a = 1

def func():
    global a
    print(a)
    del a

func()
print(a)
Curiosity answered 26/7, 2019 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.