PDA

View Full Version : How to Calculate the memory occupied by a QMap object



ahmedb
5th May 2010, 06:51
Dear all,

I need to know how to calculate the size of QMap object in memory. for example, if I have a QMap<qint32,qint32> with 10,000 items inserted. How much of Ram that object would take?

Thanks in advance.

wysota
5th May 2010, 10:00
With such large quantities of items the weight of the data structure itself becomes irrelevant so you can estimate the footprint as the payload volume only. So in your case it will be about 100kB. For more precise calculation see the Qt Quarterly article about Qt containers.

ahmedb
6th May 2010, 09:29
Thanks for your answer.

I made a console application, and created an instance of QMap<qint32,qint32> and filled with 10,000 items. I got the following results observing my process's"memory usage" in "Windows Task Manager":

1.644 MB ( initially)
1.900 MB ( 10,000 x (qint32, qint32) )
2.148 MB ( 20,000 x (qint32, qint32) )
3.104 MB (+10,000 x (qint32,QString) ) ( all strings are equal = "This is a Texture000" )
4.052 MB (+10,000 x (qint32,QString) ) ( all strings are equal = "This is a Texture000" )
5.004 MB (+10,000 x (qint32,QString) ) ( all strings are equal = "This is a Texture000" )

for the QMap<qint32,qint32> 10,000 items, it took about 256 KB however, it should take about 80KB = ( 10,000 * 8 ) .
and for the QMap<qint32,QString> 10,000 items, it took about 960 KB however, it should take about 468KB = ( 10,000 * 8 + 40*10,000 ) .

why is that?

Thanks in advance

wysota
6th May 2010, 09:34
for the QMap<qint32,qint32> 10,000 items, it took about 256 KB however, it should take about 80KB = ( 10,000 * 8 ) .
No, it would take more, 80kB is just an estimation.


and for the QMap<qint32,QString> 10,000 items, it took about 960 KB however, it should take about 468KB = ( 10,000 * 8 + 40*10,000 ) .
QString is much heavier than int, you can't consider raw data as a good estimation in this case, especially that QString keeps its data as unicode (which immediately doubles memory usage). There is also a matter of using p-impl.

ahmedb
6th May 2010, 10:11
I understand that 80KB is just an estimation, but I got 256KB which is much larger than expected !! and for the QString I already calculated the size assuming every character is two bytes length. and that was not useful as well.

anyway, I needed some equation by which I can get near the real size of the object. but it looks harder than I thought.

Thanks again for your help.

squidge
6th May 2010, 13:26
You can't rely on the results from Windows Task Manager. For example, If you allocate memory but don't write to it, its not regarded as part of your process. The actual allocation will occur when you access that memory.

Under Windows, you can use the GetProcessMemoryInfo API to estimate the amount of memory usage before and after the allocation of an object, but this is again subject to the compiler actually allocating memory from the system, rather than from it's own heap. When you then delete this memory it may not be returned to the system straight away.

A loop allocating 10,000 items and checking the process memory information each time would give you a decent estimate however.