How to reduce use of heap section in c++?
-
wrote on 5 Nov 2021, 20:54 last edited by Qt embedded developer 11 Sept 2021, 11:52
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?
-
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?
wrote on 6 Nov 2021, 07:37 last edited by@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.
-
wrote on 5 Nov 2021, 21:39 last edited by fcarney 11 May 2021, 21:43
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.
-
wrote on 6 Nov 2021, 00:56 last edited by
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.
-
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?
wrote on 6 Nov 2021, 07:37 last edited by@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.
-
@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.
wrote on 8 Nov 2021, 10:12 last edited by Qt embedded developer 11 Aug 2021, 10:16@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++.
-
@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++.
wrote on 8 Nov 2021, 10:16 last edited by JonB 11 Aug 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.
-
@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.
wrote on 8 Nov 2021, 10:24 last edited by Qt embedded developer 11 Aug 2021, 10:48@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.
-
@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.
wrote on 8 Nov 2021, 10:26 last edited by@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.
-
@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.
wrote on 8 Nov 2021, 10:30 last edited by@artwaw Hi i am not using qt i am using eclipse as my product support this
C-language Interface Specification for SQLite -
@artwaw Hi i am not using qt i am using eclipse as my product support this
C-language Interface Specification for SQLitewrote on 8 Nov 2021, 10:56 last edited by@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. -
wrote on 8 Nov 2021, 16:47 last edited by
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.
-
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.
wrote on 9 Nov 2021, 09:39 last edited by Qt embedded developer 11 Sept 2021, 11:45@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/13