I developed an Android application using Opencv for heavy image processing.

Later in the development I will use some sort of sensors to detect the presence of the object then start the processing. So for this matter I mimicked this sensor with a boolean variable and set it to false by default and on a button click I change it to true to start the detection.

By default the camera shows the normal object and when I click the button the preview changes with the processed image.

The main problem is when there is no image processing (in the case of false value) the memory usage hits 1.2G and the application crashes.

And in the case of image processing (the true case) the memory usage goes between 0.3G and 0.7G.

For both cases I'm calling the java garbage collector using System.gc()

The weird thing is I thought when there is heavy image process there will be some memory leaks, but it's the opposite.

Here is my code in onCameraFrame

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    // FPS
    final int fps = (int) (frameCounter * 1e9 / (System.nanoTime() - lastNanoTime));
    Runnable fpsUpdater = new Runnable() {
        public void run() {
            Log.d("fpsssssss", "" + fps);
    new Handler(Looper.getMainLooper()).post(fpsUpdater);
    frameCounter = 0;
    lastNanoTime = System.nanoTime();

    imgSource = inputFrame.rgba().t();
    Core.transpose(imgSource, imgSource);

    //imgSource.convertTo(enhancedMat, imgSource.type(), 1.3);

    if (canProcess) {
        if (counter_frame == 3) {
            output = detectLargestContour(imgSource);
            counter_frame = 0;
    } else {
        output = imgSource;


    return output;

Can someone explain to me what's happening?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.