PDA

View Full Version : program doesn't seem to free memory, linux



JeanC
1st September 2008, 10:07
Hello,
My program uses 2 QList's for it's task.

When it is done it goes idle untill the next task, there I try to clean up:



while (!imglist.isEmpty())
imglist.removeFirst();
while (!reglist.isEmpty())
reglist.removeFirst();
imglist.clear();
reglist.clear();


But it looks likes the memory is not being freed, top reports a whopping 45 mb still in use.
I tried to use the lists with delete and new but I couldn't find the right syntax.
In stead of


for (int i = 0; i < count; i++)
{
reglist[i].translate(-minx, -miny);
imglist[i] = imglist[i].copy(QRect(minx, miny, width, height));
}

Use something like:


for (int i = 0; i < count; i++)
{
reglist[i]->translate(-minx, -miny);
*imglist[i] = imglist[i]->copy(QRect(minx, miny, width, height));
}

I tried various versions with at() and [] but kept getting errors. :(

I am most curious though to why it is not releasing all that memory in the first place.

Thanks.

blukske
1st September 2008, 10:52
The amount of memory that top reports is the amount of memory that the operating system has reserved for your process. That memory is available to your program. Not all of it has to be allocated though. When you allocate memory, and later free it, Linux in general does not automatically make that memory available to all other applications, only to the running proces. In case you find out how to release it to all other running processes let me know :)

To test this, allocate a big chunk of memory after freeing the lists. You should see that the amount of memory in top does not increase.

aamer4yu
1st September 2008, 11:23
What is imglist ?? what is its type / declaration ??

A simple way to delete is -

qDeleteAll(imglist);
imglist.clear();

:)

JeanC
1st September 2008, 12:15
@aamer4yu, below is imglist, so it's not a list with pointers. Thanks anyway, I didn't know function qDeleteAll().


QList <QImage> imglist;


@bluske
If what you say is true, then what happens if another process request memory and there's only more or less swap left, will it still not use the freed memory from that one process?

wysota
1st September 2008, 12:56
The memory will be released if another process requests it and the operating system decides it is better to give it from your pool instead of for example swapping something else to free memory that had been allocated.

JeanC
1st September 2008, 13:06
The memory will be released if another process requests it and the operating system decides it is better to give it from your pool instead of for example swapping something else to free memory that had been allocated.

That's good enough for me I guess.

Thanks all.