Skip to main content
gamedev.net
🔒 Locked

help with a python memory leak

Started by kikkler 2 replies 600+ views
Original Post
kikkler
kikkler
i have a function that declares a numpy array. it looks something like def foo(): A = some array ... the problem - whenever i call foo(), the memory usage gets bumped and pretty soon I'm getting a MemoryError exception. i tried putting del A at the end of the function, but it doesn't seem to help very much. at each run of foo() A may have different dimensions (depends on some other variables) so i cannot (i think?) declare it only once and use it afterwards. any ideas? thanks
God is a concept by which we measure our pain
smr
smr
We're going to need to see the actual code of the function and the code where you call foo().

The most likely cause is that you're creating a circular reference to the object. Since CPython implements a reference counted garbage collector, if you create two objects that reference each other, they'll never get destroyed unless you manually destroy the reference yourself. This usually means you have to set the reference = None once you're ready for the object to be collected.

EDIT: Correction: python collects cycles as long as there isn't a __del__ method defined on the object. Otherwise it cannot know which __del__ should be invoked first.

[Edited by - smr on January 10, 2007 11:18:52 AM]
kikkler
kikkler
ok, here is the function -
ratingsMatrixTo(i,j) builds some (N x 2) matrix based on the values of i and j, N is function(i, j) as well
AVG is a known vector

def correlationFunction(i, j):    if (i == j): return 0    R = ratingsMatrixTo(i,j)    I = R[:, 0] - AVG[i-1]    J = R[:, 1] - AVG[j-1]        II = dot(I, I)    JJ = dot(J, J)    result = dot(I, J) / N.sqrt(II*JJ)    del R, I, J    return result

i dont think where i'm calling it from is relevant - when finishing with correlationFunction i ditch the inner matrix and vectors and just return a number. let me know if you need it though
thanks
God is a concept by which we measure our pain
Vorpy
Vorpy
I don't think anything in there should be causing a leak. What does the ratingsMatrixTo function do?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.