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 5 Nov 2021, 20:54 last edited by Qt embedded developer 11 Sept 2021, 11:52
    #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?

    J 1 Reply Last reply 6 Nov 2021, 07:37
    0
    • Q Qt embedded developer
      5 Nov 2021, 20:54

      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?

      J Offline
      J Offline
      JonB
      wrote on 6 Nov 2021, 07:37 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 8 Nov 2021, 10:12
      2
      • F Offline
        F Offline
        fcarney
        wrote on 5 Nov 2021, 21:39 last edited by fcarney 11 May 2021, 21:43
        #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
        • F Offline
          F Offline
          fcarney
          wrote on 5 Nov 2021, 22:28 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 6 Nov 2021, 00:56 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
              5 Nov 2021, 20:54

              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?

              J Offline
              J Offline
              JonB
              wrote on 6 Nov 2021, 07:37 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 8 Nov 2021, 10:12
              2
              • J JonB
                6 Nov 2021, 07:37

                @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 8 Nov 2021, 10:12 last edited by Qt embedded developer 11 Aug 2021, 10:16
                #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++.

                J 1 Reply Last reply 8 Nov 2021, 10:16
                0
                • Q Qt embedded developer
                  8 Nov 2021, 10:12

                  @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++.

                  J Offline
                  J Offline
                  JonB
                  wrote on 8 Nov 2021, 10:16 last edited by JonB 11 Aug 2021, 10:16
                  #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 8 Nov 2021, 10:24
                  0
                  • J JonB
                    8 Nov 2021, 10:16

                    @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 8 Nov 2021, 10:24 last edited by Qt embedded developer 11 Aug 2021, 10:48
                    #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 8 Nov 2021, 10:26
                    0
                    • Q Qt embedded developer
                      8 Nov 2021, 10:24

                      @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 8 Nov 2021, 10:26 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 8 Nov 2021, 10:30
                      2
                      • artwawA artwaw
                        8 Nov 2021, 10:26

                        @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 8 Nov 2021, 10:30 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

                        J 1 Reply Last reply 8 Nov 2021, 10:56
                        0
                        • Q Qt embedded developer
                          8 Nov 2021, 10:30

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

                          J Offline
                          J Offline
                          JonB
                          wrote on 8 Nov 2021, 10:56 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 8 Nov 2021, 16:47 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 9 Nov 2021, 09:39
                            4
                            • M mpergand
                              8 Nov 2021, 16:47

                              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 9 Nov 2021, 09:39 last edited by Qt embedded developer 11 Sept 2021, 11:45
                              #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

                              1/13

                              5 Nov 2021, 20:54

                              • Login

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