Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to measure the memory usage of a class?
QtWS25 Last Chance

How to measure the memory usage of a class?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 3.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChanchoCiego
    wrote on last edited by
    #1

    Hello

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

    Thanks a lot

    Best regards

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utcenter
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChanchoCiego
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • U Offline
            U Offline
            utcenter
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChanchoCiego
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • U Offline
                U Offline
                utcenter
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved