php-opencv / php-opencv

opencv 4.5+ with dnn module for php 7/8

Home Page:https://github.com/php-opencv/php-opencv-examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak in php-opencv?

Jeppelelle opened this issue · comments

Sorry for making yet another thread but i found what seems to be a memory issue.

I was working yesterday & today and noticed that my server became very slow after a couple of minutes & it turned out that everytime i used php-opencv on my website the memory usage increased by 200mb per image that was processed, the memory did not get released & after about 30 images all my memory was allocated & the server started using swap memory, so it became really slow:

https://i.imgur.com/YgyeXDl.png (php-fpm uses almost 8GiB of memory in total)

OpenCV has some memory leak issues when using Mat functions/objects & without using delete[] and/or release() properly & since php-opencv uses Mat for blobFromImage etc i checked the sourcecode, primarily "opencv_mat.cc" but it seems like the function "opencv_mat_free_obj()" should release/delete the memory properly of obj->mat & in "opencv_dnn.cc" i added "delete new_obj->mat;" for blobFromImage (just to try to debug the memory leak), i re-compiled & installed the .so again & restarted my server but i still had memory leaks.

I tested installing php-opencv on 3 different Debian10 servers & all of them had the same issue, i asked a friend that uses Ubuntu 18.04.03 if he had the same issue & he said they have, they "solved" it by changing the pool settings to:

pm = ondemand
pm.process_idle_timout = 10s
pm.max_requests 20

These settings incredible low for a production server so the performance suffers alot

So, have anyone else noticed a memory leak on your computer/server when using php-opencv?

I will read through the sourcecode again & see if there is any memory related problems i can find but when i checked earlier today i could not see any obvious issues so maybe there is an issue with ZEND instead? Or the interface between php-opencv and ZEND?

All my servers have default settings & everything is installed from the official repositories (apt install) & all .ini files are unchanged from default

I'm not c++ developer. If you fix it please send PR.

Sure! I have not looked at the code since last time, will take a look in a couple of days when i have time & if i find the issue i will let you know.

But maybe its better if i just fork it & update it for 7.4 and FFI, would be good to not be limited to PHP7.2, will see

I thought about it but:
FFI supports C API but C++ doesn't.
opencv 3.4 supports C API but opencv 4.0 doesn't.