Ask Your Question
3

warpPerspective in python memory leak?

asked 2014-07-11 21:38:11 -0600

xingzhong gravatar image

updated 2014-07-14 12:48:55 -0600

Hi, there,

Each time I call Python API warpPerspective, the memory usages are increasing simultaneously. I suspect that this function does not properly free its memory in Python's garbage collection system.

The following code will eat my entire memory eventually. It seems that each time rot have new reference, the old memory did not get free. My opencv version: 3.0.0-tp compiled on Ubuntu 12.04

I am not familiar with the garbage collection. Is this a bug, or I am doing something wrong here?

def main():
    M = np.random.randn(3,3)
    img = np.random.randint(255, size=(1024, 1024, 3))
    while(1):
        rot = cv2.warpPerspective(img, M, (1024, 1024))
        time.sleep(1)

Update: I am also use memory_profiler to checkout what happens in my memory. It seems that the first couple of warpPerspective do have memory collect. Their memory increases are zero. But then after that each time we call warpPerspective, it increase around 3Mb memory cost.

Line # Mem usage Increment Line Contents

25   55.258 MiB    0.000 MiB   @profile
26                             def main():
27   55.301 MiB    0.043 MiB       M = np.random.randn(3,3)
28   55.301 MiB    0.000 MiB       MM = 1024
29                                 #hp = hpy()
30   79.512 MiB   24.211 MiB       court = np.random.randint(255, size=(MM, MM, 3))
31   79.699 MiB    0.188 MiB       court = np.random.randint(255, size=(MM, MM, 3))
32   79.879 MiB    0.180 MiB       court = np.random.randint(255, size=(MM, MM, 3))
33  104.051 MiB   24.172 MiB       court = np.random.randint(255, size=(MM, MM, 3))
34  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
35  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
36  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
37  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
38  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
39  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
40  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
41  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
42  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
43  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
44  104.051 MiB    0.000 MiB       court = np.random.randint(255, size=(MM, MM, 3))
45  104.066 MiB    0.016 MiB       court = np.uint8(court)
46  108.719 MiB    4.652 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
47  108.773 MiB    0.055 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
48  108.773 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
49  108.773 MiB ...
(more)
edit retag flag offensive close merge delete

Comments

1

+1 - How did you create that memory usage table?

Abid Rahman K gravatar imageAbid Rahman K ( 2014-07-14 20:20:53 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-07-14 13:08:22 -0600

xingzhong gravatar image

Problem Solved. Because of the numpy memory allocation, excessive memory usage do not directly get free from system 's virtual memory.

The Solution is simple :). Instead of creating and destroying new memory in loop, we use in-memory API.

The following code have stable memory usage pattern.

def main():
    M = np.random.randn(3,3)
    img = np.random.randint(255, size=(4096, 4096, 3))
    img = np.uint8(img)
    rot = np.zeros_like(img)
    while(1):
        cv2.warpPerspective(img, M, (4096, 4096), dst=rot)
        time.sleep(1)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-07-11 21:38:11 -0600

Seen: 1,299 times

Last updated: Jul 14 '14