Results 1 to 6 of 6

Thread: program doesn't seem to free memory, linux

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default program doesn't seem to free memory, linux

    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:

    Qt Code:
    1. while (!imglist.isEmpty())
    2. imglist.removeFirst();
    3. while (!reglist.isEmpty())
    4. reglist.removeFirst();
    5. imglist.clear();
    6. reglist.clear();
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. for (int i = 0; i < count; i++)
    2. {
    3. reglist[i].translate(-minx, -miny);
    4. imglist[i] = imglist[i].copy(QRect(minx, miny, width, height));
    5. }
    To copy to clipboard, switch view to plain text mode 

    Use something like:
    Qt Code:
    1. for (int i = 0; i < count; i++)
    2. {
    3. reglist[i]->translate(-minx, -miny);
    4. *imglist[i] = imglist[i]->copy(QRect(minx, miny, width, height));
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    May 2006
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: program doesn't seem to free memory, linux

    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.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: program doesn't seem to free memory, linux

    What is imglist ?? what is its type / declaration ??

    A simple way to delete is -
    Qt Code:
    1. qDeleteAll(imglist);
    2. imglist.clear();
    To copy to clipboard, switch view to plain text mode 


  4. #4
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: program doesn't seem to free memory, linux

    @aamer4yu, below is imglist, so it's not a list with pointers. Thanks anyway, I didn't know function qDeleteAll().
    Qt Code:
    1. QList <QImage> imglist;
    To copy to clipboard, switch view to plain text mode 

    @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?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: program doesn't seem to free memory, linux

    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.

  6. #6
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: program doesn't seem to free memory, linux

    Quote Originally Posted by wysota View Post
    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.

Similar Threads

  1. Replies: 19
    Last Post: 21st January 2008, 09:13
  2. QT Program debug,GDB on Linux
    By darpan in forum General Programming
    Replies: 1
    Last Post: 26th January 2007, 22:02
  3. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19
  4. Replies: 2
    Last Post: 28th December 2006, 12:38
  5. Memory question - scribble example program
    By Franco9 in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2006, 03:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.