How to measure the memory usage of a class?



  • Hello

    The class that I want to measure memory consumption is a list,

    Thanks a lot

    Best regards


  • Moderators

    a rough approximation would be to "serialize":http://qt-project.org/doc/qt-4.8/datastreamformat.html the list to a bytearray and use the size of the bytearray.

    But this may not work (depending on your datatypes in the list) and is also not 100% reliable if that's the real size in the memory.

    Nevertheless i don't think that there is a easy solution for this.



  • You must take padding and type alignment into consideration too, in order to get the actual memory footprint. In general, sizeof is enough to give the proper size for the class local members, and for dynamically allocated members that vary in size you can multiply the sizeof a single element by the size or even capacity, in case you want to track memory that is allocated but not yet used.



  • Hello

    Thank you very much for the replies,

    By what you me responded, there is a specific and rapid method (which consumes few resources) to know the amount of memory used by a class.

    The context is as follows:

    An application receives data via TCP, data on reception (bandwidth, number of blocks, etc.) are stored in memory, and when memory occupy a predetermined percentage, are stored in SQLite.
    I decided to store them temporarily in memory to not degrade the speed of communication.

    Is there any workaround?

    Thanks a lot

    Best regards



  • bq. An application receives data via TCP, data on reception (bandwidth, number of blocks, etc.) are stored in memory, and when memory occupy a predetermined percentage, are stored in SQLite.
    I decided to store them temporarily in memory to not degrade the speed of communication.

    What does this have to do with how much memory a class uses? It sounds like you are not exactly clear or what you need to do and how.



  • The class would be a QList and data transmission will be stored on it



  • And how hard is it to calculate the size of a QList considering you know its length and the size of a single list element?

    And just to illustrate what was the mistake in your question - you ask how to get the memory usage of a class, which infers ALL ITS INSTANCES. In your case, you want to get the memory usage of a class instance, that is a QList, that stores a collection instances of another class or struct.

    Just keep in mind what I said in my previous post if your list elements have dynamically allocated data.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.