Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32

Thread: It seems that Qt does not release unused memory

  1. #21
    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

    An hour ago I implemented a test program that was allocating 15000 QVector<int> objects, filling them with data and reporting the memory usage through reading /proc/pidnum/status, then deallocating them (by deleting the vectors) and repeating the procedure a few times. I can't show you the sources as running the application rendered my computer unresponsive and I had to make a hard reboot (and the program was stored in /tmp). Nevertheless I monitored the memory usage and it was not what I expected. VmRSS (physical memory) stayed the same during subsequent iterations (which was expected), but VmSize (virtual memory) kept rising during each iteration (which wasn't expected) until it ate all the swap space and froze the system solid. All that means one of two (or more?) things - either I made some stupid mistake that caused memory not to be released properly or there is something wrong with memory management either in Qt or the kernel or.... I don't know where (compiler?)... Or I don't know what VmSize is...
    The app looked essentially like this (+reporting mem usage everywhere):

    Qt Code:
    1. int main(){
    2. QVector<QVector<int>*> vecs;
    3. for(int r=0;r<3;r++){
    4. for(int i=0;i<15000;i++){
    5. QVector<int> *v = new QVector<int>;
    6. for(int j=0;j<10240;j++)
    7. (*v) << 7;
    8. vecs << v; // or was it vecs[i] = *v; ?
    9. }
    10. for(int i=0;i<15000;i++){
    11. delete vecs[i];
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    A probable error is not squeezing "vecs" after each iteration, but it shouldn't make it occupy over 2GB of ram (3*15000*sizeof(int) = ~180kB) - at worst it should kill the process on trying to delete an invalid pointer. The funny thing is I have 1,2GB swap + 768MB physical memory in my machine which is less than VmSize was reporting as reserved for the process before the system started freezing.

  2. #22
    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
    10000*sizeof(double) = ~800kB

    BTW. Nobody said resize() actually causes any allocation... Try assigning a value to one of the last elements.
    Ok, I made a too simple test.
    Changing size from 10000 to 1000000 worked as espected. The memory has been allocated and deallocated.
    I'll try to make a better test code! Really sorry for my mistake .

    Quote Originally Posted by marcel View Post
    Have you tested with other tools at all?
    Yes, I tried using directly the /proc dirs.
    # cat /proc/pid_num/status

    Summing up a simple test works as expected, but my program does not (but only with Linux, with Windows works as expected).
    I'm trying to enable/disable some portions of my code to find where the problem arises.

  3. #23
    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

    Or I don't know what VmSize is...
    It is process swap size + the in-use physical memory.

    Regards

  4. #24
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    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. 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.
    I also thought I had a memleak, but after I tested this (loaded a bunch of other applications to fill the memory) my application freed the memory that it no longer used. Thanks wysota!

  5. #25
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    Quote Originally Posted by wysota View Post
    An hour ago I implemented [..]

    Qt Code:
    1. int main(){
    2. QVector<QVector<int>*> vecs;
    3. for(int r=0;r<3;r++){
    4. for(int i=0;i<15000;i++){
    5. QVector<int> *v = new QVector<int>;
    6. for(int j=0;j<10240;j++)
    7. (*v) << 7;
    8. vecs << v; // or was it vecs[i] = *v; ?
    9. }
    10. for(int i=0;i<15000;i++){
    11. delete vecs[i];
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    I couldn't understand much on how you extract the info of memory usage but atleast i found this code faulty.
    You initially allocate 15k QVector<int> objects(with 10k int each) and push it to "vecs" vector and you just "delete" the 15k vectors in first pass. Note you aren't clearing the pointers in the "vect" vector.
    In second pass you append 15k QVector<int> objects i.e now there are 30k pointers in vecs and you delete the first 15k "invalid" pointers which seems wrong.

    Probably you could have simplified code like this. (can you try it ? )

    Qt Code:
    1. int main(){
    2. QVector<QVector<int>*> vecs;
    3. for(int r=0;r<3;r++){
    4. for(int i=0;i<15000;i++){
    5. QVector<int> *v = new QVector<int>;
    6. for(int j=0;j<10240;j++)
    7. (*v) << 7;
    8. vecs << v; // or was it vecs[i] = *v; ?
    9. }
    10. qDeleteAll(vecs);
    11. //You missed this clear statement
    12. vecs.clear();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Of course you might have just done a cut and paste mistake !
    Last edited by Gopala Krishna; 25th November 2007 at 18:07.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #26
    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

    qDeleteAll(vecs) is equivalent to for(...) delete vecs[i].

  7. #27
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    Quote Originally Posted by wysota View Post
    qDeleteAll(vecs) is equivalent to for(...) delete vecs[i].
    I know
    I just wanted to say you missed vecs.clear();
    Last edited by Gopala Krishna; 25th November 2007 at 18:52. Reason: spelling error
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  8. #28
    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

    Oh, you meant clear()... No, I didn't miss it. The vector goes out of scope and so it gets cleared anyway. But even if I did miss clear, it would only make my app hold an additional of 45000*sizeof(int*) = ~180kB of memory.

  9. #29
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    Quote Originally Posted by wysota View Post
    Oh, you meant clear()... No, I didn't miss it. The vector goes out of scope and so it gets cleared anyway. But even if I did miss clear, it would only make my app hold an additional of 45000*sizeof(int*) = ~180kB of memory.
    Sorry , but have a look at the code again. vecs is declared before the outermost for loop so it wont get cleared.
    I am not sure how it relates here but deleting an invalid pointer does result in unexpected behavior.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #30
    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, I understand your point. But note that I wrote this wasn't the actual code. In the real app I was probably using the index operator to fill the vector, otherwise delete vecs[i] would have crashed the application during the second iteration of the loop (because of trying to delete an unallocated block of memory). And if you read my original post again, you'll see this line:
    Quote Originally Posted by wysota
    A probable error is not squeezing "vecs" after each iteration, but it shouldn't make it occupy over 2GB of ram (3*15000*sizeof(int) = ~180kB) - at worst it should kill the process on trying to delete an invalid pointer.

  11. #31
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

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

    Quote Originally Posted by wysota View Post
    Yes, I understand your point. But note that I wrote this wasn't the actual code. In the real app I was probably using the index operator to fill the vector, otherwise delete vecs[i] would have crashed the application during the second iteration of the loop (because of trying to delete an unallocated block of memory). And if you read my original post again, you'll see this line:
    That is the reason i put this is in one of the post above
    Of course you might have just done a cut and paste mistake !
    Anyways back to topic
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  12. #32
    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

    There was no cut&paste. I had the program written in /tmp which is a ramdisk. And I had to reboot my computer after running the application. Guess the rest

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.