View Full Version : free up the memory used by QHash

22nd June 2009, 06:09
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;

22nd June 2009, 06:21
Simply calling clear wont delete the objects of the pointers.
Try using qDeleteAll

22nd June 2009, 07:49
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.

22nd June 2009, 08:37
QHash newHash=oldHash;//this should be very fast because Qt uses shared classes
oldHash.clear();//so that oldHash can be reused

deleteNewHashInAnotherThread(newHash);//this function will start a thread to qDeleteAll()
oldHash=//load or do whatever...

22nd June 2009, 14:11
I am not very much aware of Multithreading but I am using like this.

QHash<unsigned long, DOSDIR*>newHash = OldHash;

MyThread myThread;

Thread Class:

class MyThread:public QThread
MyTHread(QObject *parent = 0);
void run();
QHash<unsigned long, DOSDIR*>MyHash;

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

void MyThread::NewThreadToFree()
///start the thread
void MyThread::run()

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.

22nd June 2009, 14:17
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...

22nd June 2009, 14:27
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.

22nd June 2009, 14:44
Instead create your own thread you can also consider to use QtConcurrent or QtConcurrentMap.

22nd June 2009, 20:13
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.