Luis Carlos Luis Carlos - 3 months ago 21
Java Question

Memory leaks ram java program with opencv and processing

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...

Answer

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.