Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

warpPerspective in python memory leak?

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.

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)

warpPerspective in python memory leak?

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.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)

warpPerspective in python memory leak?

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    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
50  108.773 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
51  108.777 MiB    0.004 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
52  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
53  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
54  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
55  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
56  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
57  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
58  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
59  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
60  111.367 MiB    2.590 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
61  114.398 MiB    3.031 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
62  117.426 MiB    3.027 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
63  120.719 MiB    3.293 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
64  123.496 MiB    2.777 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
65  126.461 MiB    2.965 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
66  129.496 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
67  132.461 MiB    2.965 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
68  135.488 MiB    3.027 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
69  138.516 MiB    3.027 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
70  141.547 MiB    3.031 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
71  144.582 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
72  147.617 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
73  150.578 MiB    2.961 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
74  153.613 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
75  156.648 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
click to hide/show revision 4
problem might found

warpPerspective in python memory leak?

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    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
50  108.773 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
51  108.777 MiB    0.004 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
52  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
53  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
54  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
55  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
56  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
57  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
58  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
59  108.777 MiB    0.000 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
60  111.367 MiB    2.590 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
61  114.398 MiB    3.031 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
62  117.426 MiB    3.027 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
63  120.719 MiB    3.293 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
64  123.496 MiB    2.777 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
65  126.461 MiB    2.965 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
66  129.496 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
67  132.461 MiB    2.965 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
68  135.488 MiB    3.027 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
69  138.516 MiB    3.027 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
70  141.547 MiB    3.031 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
71  144.582 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
72  147.617 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
73  150.578 MiB    2.961 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
74  153.613 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))
75  156.648 MiB    3.035 MiB       rot = cv2.warpPerspective(court, M, (MM, MM))

Update again: I might figure out where is the problem. As far as I know, the issues are not from opencv. It's from numpy's memory allocation. From this post , for some reasons I am not fully understand, if we excessively create big memory usage, python do not direct give out the memory to C allocator.

Consuming large memory should not be rare in CV applications, so I am still seeking for details of this problem.