Page 1 of 2 12 LastLast
Results 1 to 20 of 32

Thread: It seems that Qt does not release unused memory

  1. #1
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default It seems that Qt does not release unused memory

    Hi,
    I have this problem: I load a lot of data into memory, then I delete it and reload other data, and so on.

    Example (actually it is more complex):
    ---
    open file FILE1
    elaborate FILE1 data
    show results to the user
    close file FILE1 and delete elaborated data

    open file FILE2
    elaborate FILE2 data
    show results to the user
    close file FILE2 and delete elaborated data
    ---

    If FILE1 data requires 10 MB of RAM and FILE2 30 MB of RAM, I get this sequence:
    open FILE1 -> RAM = 10 MB
    close FILE1 -> RAM = 10 MB (not released?)
    open FILE2 -> RAM = 30 MB
    close FILE2 -> RAM = 30 MB (not released?)

    If (opposite) FILE1 data requires 30 MB of RAM and FILE2 10 MB of RAM, I get this sequence:
    open FILE1 -> RAM = 30 MB
    close FILE1 -> RAM = 30 MB (not released?)
    open FILE2 -> RAM = 30 MB
    close FILE2 -> RAM = 30 MB (not released?)

    I am deleting everything, at least I think so, I also made some tests.
    Moreover if this is not the case in both the conditions I would end up with 40 MB of RAM occupied, but this never happens.

    My understanding is that Qt knows what has been freed and reuses that memory when needed, but it does not release the memory to the OS once it has been allocated.
    I saw this behaviour with both Linux and Windows; both Qt3 and Qt4.

    Can someone explain me why this behaviour? Is there a way to avoid it?
    Thanks for any help!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    If FILE1 data requires 10 MB of RAM and FILE2 30 MB of RAM, I get this sequence:
    open FILE1 -> RAM = 10 MB
    close FILE1 -> RAM = 10 MB (not released?)
    open FILE2 -> RAM = 30 MB
    close FILE2 -> RAM = 30 MB (not released?)
    Opening a file does not implicitly loads the file data in memory.
    The operating system just provides a file handle to the calling process.

    Therefore the memory leaks you mention are caused by your code.
    Maybe you can post the code?

    Regards

  3. #3
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    Sorry, I thought it was clear that:

    open FILE1
    close FILE1

    summed up the operations described above:

    open file FILE1
    elaborate FILE1 data
    show results to the user
    close file FILE1 and delete elaborated data

    Posting the code is very difficult, it is a big project!
    Anyway I'm pretty sure everything is deleted!
    I use almost automatic variables and QObjects with parents, so deletion is not necessary. In the cases in which I use pointers to data allocated in the heap (very seldom) I checked the code and there is a delete for every new.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: It seems that Qt does not release unused memory

    Quote Originally Posted by iw2nhl View Post
    Posting the code is very difficult, it is a big project!
    Then perhaps you could prepare a small example which reproduces the problem?
    J-P Nurmi

  5. #5
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    Quote Originally Posted by jpn View Post
    Then perhaps you could prepare a small example which reproduces the problem?
    It's not an easy task, because I don't know what part of the program causes it. Anyway I'll try...

    I checked also with Valgrind and no memory leaks have been shown.

    Then I did this test:
    imagine that I need to load FILE1 and FILE2
    FILE1 loaded takes 30 MB of RAM, FILE2 loaded takes 10 MB of RAM
    now, if I load FILE1 and FILE2, the program occupies 40 MB of RAM
    but if I load FILE1 and unload it, then load FILE2 and unload it, the program occupies 30 MB of RAM

    This validates my first description: the memory previously used by FILE1 is marked as deleted and reused by FILE1, but never released to the OS.
    If there was a memory leak, I would find 40 MB of RAM occupied in both the cases described above, but it's not the case.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    How do you measure the memory used by your application?

  7. #7
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    Quote Originally Posted by jacek View Post
    How do you measure the memory used by your application?
    I use KDE System Guard 1.2.0, part of KDE 3.5.7: in the process table I read the columns "VmSize" and "VmRss".
    While on Windows 2000 I use "Task Manager", with "VM Size" and "Mem Usage" columns.

    I noticed a difference between Linux and Windows: Linux never releases the memory, while Windows releases most of it, but not all.
    Eg.:
    If the program, just started takes 10 MB of RAM and I load data for 30 MB (total 40 MB), when I unload the data, the program occupies 12 MB. From this point on, when I load and unload a file, it returns always to 12 MB (and never to 10 MB).
    On Linux instead it remains always at the peak memory usage reached (in this example 40 MB), irrespective of what I load/unload.

  8. #8
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    How do you save your data?
    In a QList? Do you delete the container or just empty it?

    Maybe Qt doesn't frees the memory of the container but keeps it as a buffer.

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

    Default Re: It seems that Qt does not release unused memory

    Quote Originally Posted by iw2nhl View Post
    On Linux instead it remains always at the peak memory usage reached (in this example 40 MB), irrespective of what I load/unload.
    As far as I know this is a normal behaviour. Your kernel prefers to have the block of memory assigned to a given process in case it wants more memory at some later point in time. If you ran out of memory, it'd be freed and assigned to the process being in need of that memory. Try opening a large file, processing it and then repeating with a smaller file. You'll probably notice the memory footprint remains the same, which means the memory for the smaller file was allocated from within the previously deallocated chunk.

  10. #10
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    Quote Originally Posted by wysota View Post
    Try opening a large file, processing it and then repeating with a smaller file. You'll probably notice the memory footprint remains the same, which means the memory for the smaller file was allocated from within the previously deallocated chunk.
    This is exactly what is happening.

    I could try to use all available memory with another program and see if the memory footprint is reduced.
    I'll try it asap!
    Thanks

  11. #11
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    Ok, I loaded files up to use 304 MB of VmSize (about 270-280 MB of VmRss), then I closed all of them and I started opening every sort of program.
    The result?
    Memory usage of the program was identical for VmSize, while VmRss diminished about of 20-30 MB.
    More than 130 MB of Swap memory were used (it was never happened before).

    I have 512 MB of RAM in my system and a Swap partition of 258 MB.

    I don't know how to interpret the result. Is this a behaviour of the kernel? Is this a Qt bug? Is there something wrong in my code?
    I really don't know

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

    Default Re: It seems that Qt does not release unused memory

    In your case VmSize can go up to over 700MB. I think VmRss is the amount of memory really occupied by the application.

    http://www.mozilla.org/projects/foot...int-guide.html

    BTW. Increase your swap space. It should be about twice as large as the amount of RAM you have in your machine.

  13. #13
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    you can find memory leaks with this very handy tool:
    valgrind (http://valgrind.org/)

    its fairly easy to use....


    niko

  14. #14
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    Quote Originally Posted by wysota View Post
    As far as I know this is a normal behaviour. Your kernel prefers to have the block of memory assigned to a given process in case it wants more memory at some later point in time. If you ran out of memory, it'd be freed and assigned to the process being in need of that memory.
    This is true for Linux, but other Unix systems have differing memory allocation behavior. Try your application under FreeBSD or Solaris for a comparison.

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

    Default Re: It seems that Qt does not release unused memory

    Yes, of course. That's natural as every system uses a different allocator implementation. The author said he was running Linux. Furthermore one Linux based system may behave differently than another, it depends on the kernel it is running (both version and options).

  16. #16
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    Quote Originally Posted by niko View Post
    you can find memory leaks with this very handy tool:
    valgrind (http://valgrind.org/)
    Thanks for the suggestion, but I already used it!
    See my post #5.
    No memory leaks are shown.
    Just a few KB caused by Qt and other linked libraries.

  17. #17
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    I decided to make a very simple test.
    It allocates a QVector, then it shows a message so that you can test memory usage, then the vector is deleted and you can test again memory usage.
    In my system, both VmRss and VmSize do not change (actually just some KB).
    Please, tell me if I'm doing something wrong!

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[]) {
    4. QApplication app(argc, argv);
    5.  
    6. // Memory allocation
    7. QVector<double> *vector = new QVector<double>;
    8. vector->resize(10000);
    9. QMessageBox::information(0, "Memory Allocation Test", "Memory allocated");
    10.  
    11. // Memory deletion
    12. vector->clear();
    13. vector->squeeze();
    14. delete vector;
    15. QMessageBox::information(0, "Memory Allocation Test", "Memory deleted");
    16.  
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    I tested also with a standard vector and it has the same behaviour, so probably it is a kernel bug/feature.

    Qt Code:
    1. double *vector = new double[10000];
    2. delete[] vector;
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: It seems that Qt does not release unused memory

    10000*sizeof(double) = ~800kB

    BTW. Nobody said resize() actually causes any allocation... Try assigning a value to one of the last elements.

  20. #20
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: It seems that Qt does not release unused memory

    On linux, what "ps" or "top" have to say?
    Have you tested with other tools at all?

    What if after closing the files you open an application that takes a lot of memory? Does vmrss drops in any way?
    The vmsize is explainable since the virtual memory manager won't adjust the swap every time a process deallocates some memory.

    I tested also with a standard vector and it has the same behaviour, so probably it is a kernel bug/feature.
    I really doubt that a bug of this magnitude could exist in the memory manager.

Similar Threads

  1. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42
  2. a Text Editor with line numbers...
    By fullmetalcoder in forum Qt Programming
    Replies: 47
    Last Post: 5th April 2006, 11:10

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.