How to reduce use of heap section in c++?
-
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?
-
@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.
-
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.
-
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.
-
@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.
-
@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++.
-
@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.
-
@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.
-
@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.
-
@artwaw Hi i am not using qt i am using eclipse as my product support this
C-language Interface Specification for SQLite -
@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. -
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.
-
@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 .