Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How to measure the memory usage of a class?

    General and Desktop
    3
    7
    2514
    Loading More Posts
    • 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
      ChanchoCiego last edited by

      Hello

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

      Thanks a lot

      Best regards

      1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        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 Reply Quote 0
        • U
          utcenter last edited by

          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 Reply Quote 0
          • C
            ChanchoCiego last edited by

            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 Reply Quote 0
            • U
              utcenter last edited by

              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 Reply Quote 0
              • C
                ChanchoCiego last edited by

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

                1 Reply Last reply Reply Quote 0
                • U
                  utcenter last edited by

                  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 Reply Quote 0
                  • First post
                    Last post