Results 1 to 9 of 9

Thread: free up the memory used by QHash

  1. #1
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question free up the memory used by QHash

    Hello All,

    I am using Qt 4.4.3 on my Mac Intel (Tiger 10.4.11).

    I am using a QHash<unsigned long,DOSDIR*>myHash for storing a large data which have around 10 Lacs records.

    DOSDIR is a structure which is having a number no items.
    Now I am using myHash .clear (); to clear all contents but I suspects that it is not clearing all the memory used by QHash.

    So it there is a better way to free up the memory used by QHash.

    If anybody knows then please help me out;

  2. #2
    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: free up the memory used by QHash

    Simply calling clear wont delete the objects of the pointers.
    Try using qDeleteAll

  3. The following user says thank you to aamer4yu for this useful post:

    vishal.chauhan (22nd June 2009)

  4. #3
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: free up the memory used by QHash

    I used qDeleteAll and it is working but now I got a new problem.
    qDeleteAll is taking very long time to clear up the memory since I have more than 10 Lac item.

  5. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: free up the memory used by QHash

    Qt Code:
    1. QHash newHash=oldHash;//this should be very fast because Qt uses shared classes
    2. oldHash.clear();//so that oldHash can be reused
    3.  
    4. deleteNewHashInAnotherThread(newHash);//this function will start a thread to qDeleteAll()
    5. oldHash=//load or do whatever...
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: free up the memory used by QHash

    I am not very much aware of Multithreading but I am using like this.

    Qt Code:
    1. QHash<unsigned long, DOSDIR*>newHash = OldHash;
    2. OldHash.clear();
    3.  
    4. MyThread myThread;
    5. myThread.NewThreadToFree(newHash);
    To copy to clipboard, switch view to plain text mode 


    Thread Class:
    Qt Code:
    1. class MyThread:public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. MyTHread(QObject *parent = 0);
    6. NewThreadToFree();
    7. void run();
    8. private:
    9. QHash<unsigned long, DOSDIR*>MyHash;
    10. };
    To copy to clipboard, switch view to plain text mode 

    ... receiving the newHash in a myHash ////


    Qt Code:
    1. void MyThread::NewThreadToFree()
    2. {
    3. ///start the thread
    4. }
    5. void MyThread::run()
    6. {
    7. qDeleteAll(myHash);
    8. }
    To copy to clipboard, switch view to plain text mode 

    It is again look like hanging (although it is running) ,but due to the large item in the QHash
    qDeleteAll is taking long time to clear the memory.

  7. #6
    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: free up the memory used by QHash

    How much time is it taking ? 10 lakhs is a big number.. so it might take some time..
    if u want to perform some operations while it is being deleted, make you own function to delete. delete one object, perform some other function or call qApp->processEvents() to prevent gui from hanging...

  8. #7
    Join Date
    Dec 2006
    Posts
    211
    Thanks
    27
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: free up the memory used by QHash

    It is taking a time between 5-10 min and If i free up the memory again and again it is taking more and more time to free up.

  9. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: free up the memory used by QHash

    Instead create your own thread you can also consider to use QtConcurrent or QtConcurrentMap.

  10. #9
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: free up the memory used by QHash

    Consider using a own memory management for your records, e.g. a list of unused records. Put deleted records in the unused list, and get them from there instead of creating new records.

    I think this will save much object creation and deletion time.

Similar Threads

  1. Memory debugging in windows
    By txandi in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2009, 13:45
  2. program doesn't seem to free memory, linux
    By JeanC in forum Qt Programming
    Replies: 5
    Last Post: 1st September 2008, 12:06
  3. What to free or not to free, that is the question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 05:04
  4. what is free store in C++ memory?
    By Masih in forum General Programming
    Replies: 6
    Last Post: 2nd July 2007, 22:25
  5. Replies: 2
    Last Post: 13th February 2006, 15:42

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.