1

i have this code in java and memory in ram almost explodes in just a few seconds. I release the IplImage "imagensMedia ". Why??

EDIT: This function runs many times per second

int largura=1280;
int altura=800;

IplImage[] imagens = new IplImage[5];
IplImage imagensSoma=cvCreateImage(cvSize(largura,altura), 32, 3);
int indiceImagem=0;

for(x=0; x<imagens.length;x++)
   imagens[x]=cvCreateImage(cvSize(largura,altura), 8, 3);

public void imageArrayBuilder() //Constroi Array de Imagens e a Sua Media
{
    int z;
    IplImage imagensMedia =cvCreateImage(cvSize(largura,altura), 8, 3);

    cam.read();
    opencv.copy(cam.get());

    if(imagemTotalFlag)
    {
        cvSub(imagensSoma,imagens[indiceImagem],imagensSoma, null);
        cvAcc(opencv.Buffer, imagensSoma, null);
        cvConvertScale(imagensSoma, imagensMedia, 1.0/imagens.length,0);
        cvCopy(opencv.Buffer,imagens[indiceImagem],null);
        indiceImagem++;
        if(indiceImagem==imagens.length)
            indiceImagem=0;
        opencv.copy(imagensMedia);    
    }
    else
    {
        if(indiceImagem<imagens.length)
        {
            cvCopy(opencv.Buffer,imagens[indiceImagem],null);
            indiceImagem++;
            if(indiceImagem==imagens.length)
            {
                imagemTotalFlag=true;
                for(z = 0; z < imagens.length; z++)
                    cvAcc(imagens[z], imagensSoma, null);
                cvConvertScale(imagensSoma, imagensMedia, 1.0/imagens.length,0);
                indiceImagem=0;
                opencv.copy(imagensMedia);
            }
        }
    }
    cvReleaseImage(imagensMedia);   
}

Someone help me please...

9
  • what do you mean by explodes? does it explode after you release the image, how big is it? Commented Jan 2, 2013 at 20:31
  • This function have a big memory leak. after 20 seconds the program have 1.5GB RAM... oh, i forgot, this fucntion runs many times per second. Commented Jan 2, 2013 at 21:10
  • Images are very large you appear to be copying them to an array which is not being cleared? I would expect a very large footprint. How big is the array supposed to be? 1000 images will easily take 1GB. Commented Jan 2, 2013 at 21:27
  • Sorry i forgot some line codes before the function. i edited the code. The array is fixed, only 5 images. Commented Jan 2, 2013 at 21:30
  • @Luis How big are the images then, in pixels? Commented Jan 2, 2013 at 21:35

2 Answers 2

5

I don't think the concept of a "memory leak" really applies in java, as it is a garbage collected language (references are automatically deleted when no other objects refer to them). The comments have given a few possibilities about where the memory is being used; I would recommend looking into how garbage collection works (see this link for starters). If all else fails, calling System.gc() will run the garbage collector, or more specifically:

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Beyond that, check for infinite loops or infinite recursion.

Hope this helps.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks. But if you use opencv and IplImage in any language you have to release memory..
I tested with System.gc(). It worked, but my processor increased from 15 to 40% now
@LuisCarlos Well I suppose that makes sense as is expending a lot of effort towards removing references... I don't know how to fix it, however. Maybe a Threed.sleep() call every now and then?
Calling System.gc() should make no difference (except on Android, where it has a non-Java-heap side-effect). All it does is cause GC to run more frequently than it would otherwise, chewing up CPU.
@HotLicks "cause GC to run more frequently than it would otherwise" is exactly what needs to happen. Unused objects are taking up memory; this removes them faster.
|
1

From what I understand, the Java wrapper for OpenCV's Mat has a tiny footprint. The underlying native objects may be using gigabytes of memory (it was in my case) and the GC knows nothing about it.

The only 2 options are to:

  • Manually deallocate native memory using Mat.release().
  • Call System.gc() every so often to force garbage collection of unreferenced Mat wrapper objects. Note, if you are using something like VideoCapture to read in frames, you will absolutely want to NOT call System.gc() every frame read as it will destroy performance. I call it about every 20 seconds in my application.

1 Comment

This answer plus the first answer reference to "this link" made it much clearer why I was getting an error and what to do about it. The underlying large native objects (OpenCV detectMultiScale face detection in my case) aren't released until the small Java objects' finalize() is run by gc(). But I have to force gc() since it doesn't see a problem yet because there are still Java megabytes freely available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.