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

Mat::t() Android, After give time gives out of the memory #4961

Closed
opencv-pushbot opened this Issue Jul 27, 2015 · 3 comments

Comments

3 participants
@opencv-pushbot
Contributor

opencv-pushbot commented Jul 27, 2015

Transferred from http://code.opencv.org/issues/4262

|| Karoly Somodi on 2015-04-04 14:52
|| Priority: Normal
|| Affected: branch '2.4' (2.4-dev)
|| Category: android
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: ARM / Android

Mat::t() Android, After give time gives out of the memory

<pre><code class="java">
    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Mat mRgba = inputFrame.rgba().clone();
        Mat mRgbaT = mRgba.t();
        Core.flip(mRgba.t(), mRgbaT, 1);
        Imgproc.resize(mRgbaT, mRgbaT, mRgba.size());
        return mRgbaT;
    }
</code></pre>


04-04 16:39:16.723    1115-1834/com.bitflags.kioba.augmented D/FpsMeter﹕ 6.42 FPS@1280x960
04-04 16:39:16.735    1115-1115/com.bitflags.kioba.augmented D/JavaCameraView﹕ Preview Frame received. Frame size: 1843200
04-04 16:39:16.801    1115-1115/com.bitflags.kioba.augmented D/JavaCameraView﹕ Preview Frame received. Frame size: 1843200
04-04 16:39:16.841    1115-1834/com.bitflags.kioba.augmented D/CameraBridge﹕ mStretch value: 1.125
04-04 16:39:16.853    1115-1834/com.bitflags.kioba.augmented D/FpsMeter﹕ 6.42 FPS@1280x960
04-04 16:39:16.868    1115-1115/com.bitflags.kioba.augmented D/JavaCameraView﹕ Preview Frame received. Frame size: 1843200
+04-04 16:39:16.885    1115-1834/com.bitflags.kioba.augmented E/cv::error()﹕ OpenCV Error: Insufficient memory (Failed to allocate 4915204 bytes) in void* cv::OutOfMemoryError(std::size_t), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/alloc.cpp, line 52
04-04 16:39:16.886    1115-1834/com.bitflags.kioba.augmented E/org.opencv.core.Mat﹕ Mat::n_1t() caught cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/alloc.cpp:52: error: (-4) Failed to allocate 4915204 bytes in function void* cv::OutOfMemoryError(std::size_t)
04-04 16:39:16.898    1115-1834/com.bitflags.kioba.augmented E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1798
    Process: com.bitflags.kioba.augmented, PID: 1115
    CvException [org.opencv.core.CvException: cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/alloc.cpp:52: error: (-4) Failed to allocate 4915204 bytes in function void* cv::OutOfMemoryError(std::size_t)
    ]
            at org.opencv.core.Mat.n_t(Native Method)
            at org.opencv.core.Mat.t(Mat.java:2353)
            at com.bitflags.kioba.augmented.surface.onCameraFrame(surface.java:119)
            at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
            at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
            at java.lang.Thread.run(Thread.java:818)
04-04 16:39:16.934    1115-1635/com.bitflags.kioba.augmented E/IMemory﹕ cannot map BpMemoryHeap (binder=0x9f60b3c0), size=11059200, fd=35 (Out of memory)
04-04 16:39:16.934    1115-1635/com.bitflags.kioba.augmented A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xffffffff in tid 1635 (Binder_4)+
04-04 16:39:16.941    1115-1115/com.bitflags.kioba.augmented D/JavaCameraView﹕ Disconnecting from camera
04-04 16:39:16.941    1115-1115/com.bitflags.kioba.augmented D/JavaCameraView﹕ Notify thread
04-04 16:39:16.941    1115-1115/com.bitflags.kioba.augmented D/JavaCameraView﹕ Wating for thread

History

Karoly Somodi on 2015-04-04 19:00
A newer chrash i guess im not alloved to use Mat::t()  ...
it is actually *2.4.10 target* but cnat set it anywhere ...


04-04 20:57:41.406   9852-10581/com.bitflags.kioba.augmented E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-2240
    Process: com.bitflags.kioba.augmented, PID: 9852
    CvException [org.opencv.core.CvException: cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/core/src/alloc.cpp:52: error: (-4) Failed to allocate 8294404 bytes in function void* cv::OutOfMemoryError(std::size_t)
    ]
            at org.opencv.core.Mat.n_t(Native Method)
            at org.opencv.core.Mat.t(Mat.java:2353)
            at com.bitflags.kioba.augmented.surface.onCameraFrame(surface.java:120)
            at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
            at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
            at java.lang.Thread.run(Thread.java:818)
Steven Punte on 2015-05-18 01:19
I had success at resolving this problem by adding Mat.release() to every OpenCV Matrix object at of course locations where each object was no longer needed.  I currently operate on release 2.4.9.
@alalek

This comment has been minimized.

Contributor

alalek commented Dec 14, 2015

JVM doesn't know anything about of underlying native resources (for example, image buffers and their sizes). So Java heap size usage is not computer properly and GC is not called.
There are some working options:

  1. Try to manually release Mat objects.
  2. Invoke System.gc() every few frames.

There is nothing to fix in OpenCV, closing.

@alalek alalek closed this Dec 14, 2015

@davvyy00

This comment has been minimized.

davvyy00 commented Jan 16, 2016

Clone() doesn't seem to release native memory, I changed my code and used copyTo() and it solved the problem
Looking at this code you don't need clone?
Replace -- Mat mRgba = inputFrame.rgba().clone();
with -- Mat mRgba = new mRgba(); // declare globally
mRgba = inputFrame.rgba();
also declare Mat mRgba globally

@alalek

This comment has been minimized.

Contributor

alalek commented Jan 16, 2016

clone() creates a new Java object. Old object with native contents is still alive and will not be released until new GC cycle.
copyTo() doesn't create a new Java object, just replaces the native contents (with releasing of old native data)

So all these problems come from Java memory management for native objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment