From 8d139ff0c6c11fe46be0dc2e73e155ac49ae0389 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Tue, 17 Sep 2024 13:36:35 -0400 Subject: [PATCH] Call sqlite3_db_release_memory when we discard a connection. https://www.sqlite.org/capi3ref.html#sqlite3_db_release_memory --- ext/sqlite3/database.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/sqlite3/database.c b/ext/sqlite3/database.c index ebd2ee5e..607e880b 100644 --- a/ext/sqlite3/database.c +++ b/ext/sqlite3/database.c @@ -29,6 +29,11 @@ close_or_discard_db(sqlite3RubyPtr ctx) "is being discarded. This is a memory leak. If possible, please close all sqlite " "database connections before forking."); + // release as much heap memory as possible by deallocating non-essential memory + // allocations held by the database library. Memory used to cache database pages to + // improve performance is an example of non-essential memory. + sqlite3_db_release_memory(ctx->db); + #ifdef HAVE_SQLITE3_DB_NAME const char *db_name; int j_db = 0;