sgjava / opencvmem

OpenCV Java Memory Management
31 stars 0 forks source link

OpenCV 4.7.0 Memory Leak (DNN?) #1

Open brunohpg opened 1 year ago

brunohpg commented 1 year ago

Hi,

I have an application basic use DNN and lot of memory leak using opencv 4.7.0.

Thank you for your fix. I had some improvements, however, there is still a lot of memory leaks.

I made a small script to correct the original source and would like to share:

export OPENCV_VERSION=4.7.0
sed -i "s/final long nativeObj;/long nativeObj;/g" opencv-${OPENCV_VERSION}/modules/java/generator/templates/java_class.prolog
sed -i "s/delete(nativeObj);/free(); } public void free() { if (nativeObj != 0) { delete(nativeObj); nativeObj = 0; }/g" opencv-${OPENCV_VERSION}/modules/java/generator/gen_java.py

sed -i "s/n_delete(nativeObj);/free();/g" opencv-${OPENCV_VERSION}/modules/core/misc/java/src/java/core+Mat.java
sed -i "s/final long nativeObj;/long nativeObj; public void free() { if (nativeObj != 0) { release(); n_delete(nativeObj); nativeObj = 0; }}/g" opencv-${OPENCV_VERSION}/modules/core/misc/java/src/java/core+Mat.java
sed -i "s/%s_mat.release();/%s_mat.free();/g" opencv-${OPENCV_VERSION}/modules/java/generator/gen_java.py

sed -i "s/mi.release();/mi.free();/g" opencv-${OPENCV_VERSION}/modules/java/generator/src/java/org/opencv/utils/Converters.java

The result is in this JAR/ZIP with sources: opencv-470.zip

I couldn't identify the location where there is a memory leak, but the process accumulates a lot of memory over time.

I checked all my objects and didn't find any without "free". Do we know of any other memory leak points?

sgjava commented 1 year ago

They are hard to find, so I'd just build a program and run Valgrind on it to detect the native leaks.

On Fri, Aug 4, 2023 at 8:53 AM brunohpg @.***> wrote:

Hi,

I have an application basic use DNN and lot of memory leak using opencv 4.7.0.

Thank you for your fix. I had some improvements, however, there is still a lot of memory leaks.

I made a small script to correct the original source and would like to share:

export OPENCV_VERSION=4.7.0 sed -i "s/final long nativeObj;/long nativeObj;/g" opencv-${OPENCV_VERSION}/modules/java/generator/templates/java_class.prolog sed -i "s/delete(nativeObj);/free(); } public void free() { if (nativeObj != 0) { delete(nativeObj); nativeObj = 0; }/g" opencv-${OPENCV_VERSION}/modules/java/generator/gen_java.py

sed -i "s/n_delete(nativeObj);/free();/g" opencv-${OPENCV_VERSION}/modules/core/misc/java/src/java/core+Mat.java sed -i "s/final long nativeObj;/long nativeObj; public void free() { if (nativeObj != 0) { release(); n_delete(nativeObj); nativeObj = 0; }}/g" opencv-${OPENCV_VERSION}/modules/core/misc/java/src/java/core+Mat.java sed -i "s/%s_mat.release();/%s_mat.free();/g" opencv-${OPENCV_VERSION}/modules/java/generator/gen_java.py

sed -i "s/mi.release();/mi.free();/g" opencv-${OPENCV_VERSION}/modules/java/generator/src/java/org/opencv/utils/Converters.java

The result is in this JAR/ZIP with sources: opencv-470.zip https://github.com/sgjava/opencvmem/files/12260557/opencv-470.zip

I couldn't identify the location where there is a memory leak, but the process accumulates a lot of memory over time.

I checked all my objects and didn't find any without "free". Do we know of any other memory leak points?

— Reply to this email directly, view it on GitHub https://github.com/sgjava/opencvmem/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4RCZ4BR4AL5NM33Y25DM3XTTWFRANCNFSM6AAAAAA3EFOMKA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Steven P. Goldsmith