SQLite Forum

How to free max heap size on execution of sqlite query using sqlite3_exec?
To answer the original question (since nobody else did) - what you are probably looking for is this:   
[sqlite3_db_release_memory()](https://sqlite.org/c3ref/db_release_memory.html) or [sqlite3_release_memory](https://sqlite.org/c3ref/release_memory.html) - related to whether your version is compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT](https://sqlite.org/compile.html#enable_memory_management).

That said, the warnings of the other posters are to be heeded - if your Heap grows and grows forever, especially when simply repeating the same query, you have a leak.  This is most probably due to what Larry described, which should be fixed before attempting any other memory optimizations.