Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in frame.convert_pixel_format #12

Open
emasty opened this issue Aug 16, 2023 · 1 comment
Open

Memory leak in frame.convert_pixel_format #12

emasty opened this issue Aug 16, 2023 · 1 comment

Comments

@emasty
Copy link

emasty commented Aug 16, 2023

Issue
There seems to be a memory leak in frame.py#convert_pixel_format

How to reproduce
Run example asynchronous_grab_opencv.py with camera module 1800 U-2050m. Every received frame increases memory consumption.

Additional information
Memory leak occurs in function convert_pixel_format. Removing the convertion in the sample code eliminates the memory leak.

display = frame.convert_pixel_format(opencv_display_format)

@NiklasKroeger-AlliedVision
Copy link
Collaborator

Similar to #13 I do not believe that this is an actual memory leak, but rather an observation of how Pythons garbage collector works. Please also see my response there.

Currently convert_pixel_format allocates a new frame buffer for the result of the frame conversion and returns this in the form of a new Frame object. That means every time the function is called, new memory is allocated. The mentioned example will simply take the result of this transformation and display it. After displaying the result, the Frame that holds the conversion result is discarded.

This discarded Frame instance (and its corresponding frame buffer) are now no longer referenced and will be freed (at some point) by the garbage collector. But the Python garbage collector has some leeway in when the actual freeing should take place.

Here I have a screenshot of an execution of asynchronous_grab_opencv.py running on my machine.
image
As you can see the memory consumption does increase quite a bit as more and more frames are converted and the frame buffer for these results accumulate. But after some time the garbage collector jumps in and removes these unreferenced buffers. Over time the maximum consumption grows (default behaviour of the Python garbage collector) but at some point it settles and jumps in more or less at consistent times/memory usage.

If you want to have more control over how garbage collection takes place, take a look at the built-in gc module of Python. It allows you to configure how garbage collection takes place. With gc.collect you can even force garbage collection at specific moments.

As mentioned in #13 these limitations are more or less to be expected of Python as garbage collected language. If you need predictable memory consumption I would recommend looking at our VmbC or VmbCPP APIs where memory management is a more manual task. This provides better control over when and where memory is allocated and freed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants