Results 1 to 6 of 6

Thread: How to Calculate the memory occupied by a QMap object

  1. #1
    Join Date
    Dec 2008
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to Calculate the memory occupied by a QMap object

    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.

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

    Default Re: How to Calculate the memory occupied by a QMap object

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2008
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to Calculate the memory occupied by a QMap object

    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

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

    Default Re: How to Calculate the memory occupied by a QMap object

    Quote Originally Posted by ahmedb View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2008
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to Calculate the memory occupied by a QMap object

    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.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to Calculate the memory occupied by a QMap object

    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.

  7. The following user says thank you to squidge for this useful post:

    ahmedb (6th May 2010)

Similar Threads

  1. Replies: 1
    Last Post: 19th February 2010, 11:02
  2. calculate draw time
    By johnsoga in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 02:03
  3. how to calculate difference b/w two times
    By dummystories in forum Newbie
    Replies: 1
    Last Post: 9th March 2009, 13:58
  4. How to calculate Monitor Resolution?
    By ashukla in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2007, 07:28
  5. Calculate QListView content dimensions
    By Mookie in forum Qt Programming
    Replies: 3
    Last Post: 9th June 2007, 18:18

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.