Page 1 of 1

Memory usage problem

Posted: 2012-09-11T04:47:07-07:00
by Mihran
Hi everyone!

I have folowing code.

i

Code: Select all

nt main(int argc, char *argv[]) 
{
    Image* arr[256];
    
    for(int i = 0; i < 256; i++)
    {
        Image* t = new Image(_pictures_background);
        t->read("files/1.png");
        arr[i] = t;
    }        
    std::string str;
    cout << "Memory allocated. Press enter key to continue...";
    cin >> str;
    
    for(int i = 0; i < 256; i++)
    {        
        delete arr[i];
        arr[i] = NULL;
    }
     
    cout << "Memory deallocated. Press enter key to continue...";
    cin >> str;
    return 0;
}
At first it allocates aprox. 2Gb of memory, when I delete created images memory is not actualy freed, process still use 2Gb of memory. Valgrind shows what that memory still rechable.
How can I free that memory?

Thanks in advice!

Re: Memory usage problem

Posted: 2012-09-11T04:54:34-07:00
by magick
You can force the image pixels to disk rather than memory with cacheThreshold(). It will be slower but the memory footprint will be smaller.

Its up to the compiler and OS to return memory after its freed. A garbage collection cycle may needed and even then the OS may not return it. Linux, for example, generally will only return large chunks of memory but keep smaller chunks even when its freed (sbrk).

Re: Memory usage problem

Posted: 2012-09-11T05:04:12-07:00
by Mihran
Thanks for replay.

I have a filling that it is not OS related issue. Looks like IM use its own memory allocation mechanism. And it is not free it it and keep it for reuse. I don`t want to use cache it is to slow for my case(I`m processing 100s of images ).