Try to search your question here, if you can't find : Ask Any Question Now ?

Memory leak issue with Python 3.5.3 under Linux

HomeCategory: stackoverflowMemory leak issue with Python 3.5.3 under Linux
Avatarkundan asked 2 weeks ago

When running a graphic application with Python 3.5.3 under Linux on a raspberry pi 3 board the application memory usage grows steadily and the board eventually crushes after a time period of approximately five minutes.

I have recently modified a Python script that previously only worked with Python 2.7.13
but didn’t work with Python 3 (Python version: 3.5.3). Backward compatibility with Python 2 is not required.
As part of this process I had to install two new libraries that specifically used by
Python 3 (python3-pyside and python3-pyqtgraph). Now the application seems to be working
with Python 3.5.3 but I’m seeing memory leak (this is running on a Raspberry pi 3 board under Linux, Python version is 3.5.3).
It takes about five minutes for the board to crash completely.

I was able to narrow down the problem to a single line of code, please see below:

imageType_DisplayImageFinal = QtGui.QImage(imageType_DisplayImage, 
imageType_DisplayImage.shape[1], imageType_DisplayImage.shape[0], 
imageType_DisplayImage.strides[0], QtGui.QImage.Format_RGB888)

The code works fine in Python 2.7.13 so I have a feeling that the problem is not with the Python code itself but with the newly installed libraries (python3-pyside and python3-pyqtgraph) but not 100% sure.

Does anybody have an idea why this is happening and how to fix it? Perhaps a different “pyside” version should be installed. Will “PySide2” make a difference?
Thank you in advance for your help and time and please let me know if you need additional information.

It seems that the problem is resulted by the following line of code:

imageType_DisplayImageFinal = QtGui.QImage(imageType_DisplayImage, 
imageType_DisplayImage.shape[1], imageType_DisplayImage.shape[0], 
imageType_DisplayImage.strides[0], QtGui.QImage.Format_RGB888)

As mentioned earlier the application seems to be working fine with Python 3.5.3 but only for a limited time due to memory leak. As time goes on the application’s memory usage grows steadily and the board crushes after about 5 minutes. How can I prevent the memory leak from happening?

Here is minimal source code:

def display_video_stream(self):
    _, imageType_RawImage = self.capture.read()
    height = self.myWidget.lblVideoStream.height()
    width = self.myWidget.lblVideoStream.width()

    imageType_RawImage = imageType_RawImage[0:480, 90:570]
    Center = (240,240)
    M = cv2.getRotationMatrix2D(Center, 90, 1.0)
    imageType_RawImage = cv2.warpAffine(imageType_RawImage, M, (480, 480))
    imageType_RawImage = imageType_RawImage[self.imageShift1:self.imageShift2, self.imageShift3:self.imageShift4]               #square 480 x 480 image from 640 x 480 image
    imageType_RawImage = cv2.cvtColor(imageType_RawImage, cv2.COLOR_BGR2RGB)            #Convert the color to display format
    imageType_DisplayImage = imageType_RawImage                        #Split off image for display purposes.

    #imageType_DisplayImage = cv2.bitwise_and(self.staticMask, imageType_DisplayImage)
    imageType_DisplayImage = self.image_resize(imageType_DisplayImage, height = height, width = width)

    imageType_DisplayImageFinal = QtGui.QImage(imageType_DisplayImage, imageType_DisplayImage.shape[1], imageType_DisplayImage.shape[0], imageType_DisplayImage.strides[0], QtGui.QImage.Format_RGB888)
    self.myWidget.lblVideoStream.setPixmap(QtGui.QPixmap.fromImage(imageType_DisplayImageFinal))
1 Answers
Best Answer
AvatarAmit answered 2 weeks ago
Your Answer

0 + 4 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com