Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to reduce use of heap section in c++?
Forum Updated to NodeBB v4.3 + New Features

How to reduce use of heap section in c++?

Scheduled Pinned Locked Moved Solved C++ Gurus
13 Posts 6 Posters 2.0k Views 4 Watching
  • 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by Qt embedded developer
    #1

    I have implemented one c++ project where due to so many things happen, my heap size increase screen by screen and make my app crash at runtime.

    I have 4 years of experience. But still I am stuck in this type of situation.

    Task given to me stop random crash of app at runtime.

    But as I said there is background and foreground so many things are going on C++ code.
    Like sending data to server in background and do foreground ui and ticket creation also.

    Now I need expert person advice how to reduce use of heap and make it free?

    JonBJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      I have implemented one c++ project where due to so many things happen, my heap size increase screen by screen and make my app crash at runtime.

      I have 4 years of experience. But still I am stuck in this type of situation.

      Task given to me stop random crash of app at runtime.

      But as I said there is background and foreground so many things are going on C++ code.
      Like sending data to server in background and do foreground ui and ticket creation also.

      Now I need expert person advice how to reduce use of heap and make it free?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @Qt-embedded-developer
      You absolutely should follow the others' advice about using tools to examine your allocated/non-freed memory.

      But a "higher-level" answer to your issue is: to keep heap usage under control, make sure you release your heap-allocated objects as soon as you no longer need them (and release all of their memory correctly). Your "my heap size increase screen by screen and make my app crash at runtime." may indicate that you are keeping previous "screens"-worth of data in existence while you move around in your program, when you could be releasing that data.

      Q 1 Reply Last reply
      2
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by fcarney
        #2

        Are you running in Linux or Window or ?
        In Linux you can run Valgrind on the app to see where you are allocating memory and not freeing it. In Windows you can run Heob. Qt has instructions in their docs on running these.

        Both Valgrind and Heob are integrated into Qt Creator if you follow the documentation instructions on using them.

        Edit: https://doc.qt.io/qtcreator/creator-analyzer.html

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #3

          If you have not spent time here I would suggest getting really familiar with that page. I remember there being a page that talked about memory, but I cannot find it right now. That page is invaluable for the topics it covers.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Konstantin Tokarev
            wrote on last edited by
            #4

            Are you talking about C++-level heap usage, i.e. memory used by data in your application, allocated via new or malloc, or size of heap section as observed by operating system? These two may be very different, this is called heap fragmentation and it may be quite severe when your application performs lots of allocations and deallocations at run time.

            Easiest way to estimate amount of heap fragmentation is to compare heap usage reported by C allocator (e.g. malloc_info() in case of glibc allocator) with heap usage reported by OS (e.g. sum of [heap] in /proc/$pid/smaps). If you don't have memory leaks in your application but have significant heap fragmentation, it can be fought e.g. by strategic use of memory pools in code which is known to cause most of repeated allocations/deallocations.

            1 Reply Last reply
            0
            • Q Qt embedded developer

              I have implemented one c++ project where due to so many things happen, my heap size increase screen by screen and make my app crash at runtime.

              I have 4 years of experience. But still I am stuck in this type of situation.

              Task given to me stop random crash of app at runtime.

              But as I said there is background and foreground so many things are going on C++ code.
              Like sending data to server in background and do foreground ui and ticket creation also.

              Now I need expert person advice how to reduce use of heap and make it free?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @Qt-embedded-developer
              You absolutely should follow the others' advice about using tools to examine your allocated/non-freed memory.

              But a "higher-level" answer to your issue is: to keep heap usage under control, make sure you release your heap-allocated objects as soon as you no longer need them (and release all of their memory correctly). Your "my heap size increase screen by screen and make my app crash at runtime." may indicate that you are keeping previous "screens"-worth of data in existence while you move around in your program, when you could be releasing that data.

              Q 1 Reply Last reply
              2
              • JonBJ JonB

                @Qt-embedded-developer
                You absolutely should follow the others' advice about using tools to examine your allocated/non-freed memory.

                But a "higher-level" answer to your issue is: to keep heap usage under control, make sure you release your heap-allocated objects as soon as you no longer need them (and release all of their memory correctly). Your "my heap size increase screen by screen and make my app crash at runtime." may indicate that you are keeping previous "screens"-worth of data in existence while you move around in your program, when you could be releasing that data.

                Q Offline
                Q Offline
                Qt embedded developer
                wrote on last edited by Qt embedded developer
                #6

                @JonB Hi i heard from senior that there is problem with query that increase the heap usage. So if this is problem then why it increase it and How to free this type of heap memory usage ?

                I am using sqlite3 functions to execute query in c++.

                JonBJ 1 Reply Last reply
                0
                • Q Qt embedded developer

                  @JonB Hi i heard from senior that there is problem with query that increase the heap usage. So if this is problem then why it increase it and How to free this type of heap memory usage ?

                  I am using sqlite3 functions to execute query in c++.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @Qt-embedded-developer said in How to reduce use of heap section c++?:

                  Hi i heard from senior that there is problem with query that increase the heap usage

                  Who knows what this might mean or whether it is correct. Go ask your senior, we cannot answer from this speculation.

                  Q 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Qt-embedded-developer said in How to reduce use of heap section c++?:

                    Hi i heard from senior that there is problem with query that increase the heap usage

                    Who knows what this might mean or whether it is correct. Go ask your senior, we cannot answer from this speculation.

                    Q Offline
                    Q Offline
                    Qt embedded developer
                    wrote on last edited by Qt embedded developer
                    #8

                    @JonB
                    Yes you are correct. i have checked the heap size from screen to screen that increase due to sqlite dynamic instance.

                    actually i have created dynamic instance of SQLite database and so i can not create and destroy its instance for new query execution so when i am executing query at that time this dynamic allocation get increase as we execute queries.

                    artwawA 1 Reply Last reply
                    0
                    • Q Qt embedded developer

                      @JonB
                      Yes you are correct. i have checked the heap size from screen to screen that increase due to sqlite dynamic instance.

                      actually i have created dynamic instance of SQLite database and so i can not create and destroy its instance for new query execution so when i am executing query at that time this dynamic allocation get increase as we execute queries.

                      artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on last edited by
                      #9

                      @Qt-embedded-developer said in How to reduce use of heap section c++?:

                      i have created dynamic instance of SQLite database and so i can not create and destroy its instance for new query execution

                      Please elaborate, my crystal ball tells me you're not using SQLite implementation in Qt properly.

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      Q 1 Reply Last reply
                      2
                      • artwawA artwaw

                        @Qt-embedded-developer said in How to reduce use of heap section c++?:

                        i have created dynamic instance of SQLite database and so i can not create and destroy its instance for new query execution

                        Please elaborate, my crystal ball tells me you're not using SQLite implementation in Qt properly.

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on last edited by
                        #10

                        @artwaw Hi i am not using qt i am using eclipse as my product support this
                        C-language Interface Specification for SQLite

                        JonBJ 1 Reply Last reply
                        0
                        • Q Qt embedded developer

                          @artwaw Hi i am not using qt i am using eclipse as my product support this
                          C-language Interface Specification for SQLite

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #11

                          @Qt-embedded-developer
                          So this has nothing to do with Qt classes, go look at the code you are using or ask at a suitable forum for that code.

                          1 Reply Last reply
                          1
                          • M Offline
                            M Offline
                            mpergand
                            wrote on last edited by
                            #12

                            Looking at some old code of mine about sqlite, i see:
                            sqlite3_finalyse(stmt);
                            each time i make a query.

                            And also: sqlite3_free(errMsg)

                            Check in your code that you're doing those things right.
                            Read carefully the sqlite docs, they clearly explain what to do.

                            The application must finalize every prepared statement in order to avoid resource leaks. It is a grievous error for the application to try to use a prepared statement after it has been finalized. Any use of a prepared statement after it has been finalized can result in undefined and undesirable behavior such as segfaults and heap corruption.

                            Q 1 Reply Last reply
                            4
                            • M mpergand

                              Looking at some old code of mine about sqlite, i see:
                              sqlite3_finalyse(stmt);
                              each time i make a query.

                              And also: sqlite3_free(errMsg)

                              Check in your code that you're doing those things right.
                              Read carefully the sqlite docs, they clearly explain what to do.

                              The application must finalize every prepared statement in order to avoid resource leaks. It is a grievous error for the application to try to use a prepared statement after it has been finalized. Any use of a prepared statement after it has been finalized can result in undefined and undesirable behavior such as segfaults and heap corruption.

                              Q Offline
                              Q Offline
                              Qt embedded developer
                              wrote on last edited by Qt embedded developer
                              #13

                              @mpergand said in How to reduce use of heap section c++?:

                              sqlite3_free

                              I am not using sqlite3_prepare_v2 i am using sqlite3_exec for execute the sqlite query.
                              And also i am not using the sqlite3_free(errMsg).

                              I have seen that there is as we continue execute queries my heap continuously get increasing .

                              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