Items with no label
3335 Discussions

Pyrealsense Python memory leak - how to optimize and manage memory? Ho to "release" frame in a while(true)?

MCami1
Novice
1,888 Views

Hi, i'm having some troubles with the pyrealsense library.

I'm using linux 64bit on a i5 (7th gen), 4GB Ram, with a R200 camera.

As i run the software, the memory used by the python application starts growing constantly.

Any suggestion on how to optimize memory within a while(true) application?

Thanks.

5 Replies
MartyG
Honored Contributor III
978 Views

I don't know if this will be relevant to your particular problem (I do not develop in Pyrealsense) but a Pyrealsense developer with a memory leak said "I found the memory leak issue was to do with numpy being required to be explicitly told to take ownership of the data. This, however, allows one frame to be returned and then on the next frame an access violation occurs."

They eventually found a fix for the leak and posted some code to GitHub.

https://github.com/binraker/PyRealSense GitHub - binraker/PyRealSense: Python control and capture things

MCami1
Novice
978 Views

Thanks, i've already seen that post. But the python library the user referrers to (https://github.com/binraker/PyRealSense https://github.com/binraker/PyRealSense), is not the "official" python porting linked on the librealsense github (https://github.com/toinsson/pyrealsense https://github.com/toinsson/pyrealsense).

I cannot find a way to install that alternative python wrapper.

0 Kudos
MartyG
Honored Contributor III
978 Views

I apologize that I am somewhat fumbling in the dark on this case due to my lack of Pyrealsense knowledge.

In another Python memory leak case (not Pyrealsense specifically), someone advised the person with the problem that "you are creating new objects in your while loop. Take now for example, you create a variable and then you assign a new object to it that only lives in that loop. If you declare the variables before your loop, the same object will be overwritten instead of re-created. By just declaring the variables ahead of time with name = None you will be able to make sure you reuse these variables".

"Any variables you want to not create a new instance of but just overwrite the old one. Python does all the memory management for you but you do have to give it something to work with. Typically variables will live inside of the loop only, but by never leaving the loop I can imagine they are not properly discarded. If you debug your code in something like intellij you can also see what objects are in memory, this will help you find the culprit as well."

0 Kudos
MCami1
Novice
978 Views

Already tried those solutions.

As i get the frame from the device, memory usage starts growing. At each while cycle i set the variables to None, but memory still grows.

Any ideas of what can cause this behaviour?

How does the camera feed is manganed in C++? Is there any similar problem?

Thanks for the help.

0 Kudos
MartyG
Honored Contributor III
978 Views

Looking outside of Pyrealsense, when memory leaks have occurred with RealSense projects it was because the developer had not put Release() at the end of their scripts to release the memory. Using Release() fixed the leak.

A developer called Hengel said about how they had fixed their project:

***********

Instead of

PXCImage2CVMat(projection->CreateDepthImageMappedToColor(sample->depth, sample->color),

PXCImage::PIXEL_FORMAT_DEPTH, &depthImage);

The code should be

PXCImage image = projection->CreateDepthImageMappedToColor(sample->depth, sample->color);

PXCImage2CVMat(image, PXCImage::PIXEL_FORMAT_DEPTH, &depthImage);

image->Release();

0 Kudos
Reply