Solution to sqlite3 memory keeps growing

First of all, the reason for the memory growth of sqlite3 is generally that the pointer application has not been recycled, or the cache needs to be cleared due to system differences after recycling.
We need to clarify which pointers in sqlite3 need to be recycled, as follows:
1.

 char *zErrMsg = nullptr;
 if (sqlite3_exec(db,sql,NULL,NULL,&zErrMsg) != SQLITE_OK)
 {
    
    
      qDebug()<<sqlite3_errmsg(db);
      sqlite3_free(zErrMsg);
      return false;
 }

The error message pointer zErrMsg in sqlite3_exec needs to be recycled, and the recycling method calls sqlite3_free.

2、

    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(db,sql,-1,&stmt,NULL) != SQLITE_OK)
    {
    
    
        qDebug()<<sqlite3_errmsg(db);
        sqlite3_finalize(stmt);
        return false;
    }

    while (sqlite3_step(stmt) == SQLITE_ROW) {
    
    
	    //...
    }
    sqlite3_finalize(stmt);

After executing sqlite3_prepare_v2 and sqlite3_step, the sqlite3_stmt pointer needs to be recycled, and the recycling method calls sqlite3_finalize.
3.

	char *zErrMsg = nullptr;
	char **dbResult = nullptr; 
    if (sqlite3_get_table(db, sql, &dbResult, &nRow, &nColumn, &zErrMsg) != SQLITE_OK)
    {
    
    
        qDebug()<<sqlite3_errmsg(db);
        sqlite3_free_table(dbResult);
        sqlite3_free(zErrMsg);
        return false;
    }
    sqlite3_free_table(dbResult);
    sqlite3_free(zErrMsg);

After executing sqlite3_get_table, both zErrMsg and dbResult pointers need to be recycled. The recycling methods are sqlite3_free_table and sqlite3_free.

Here is a function sqlite3_memory_used, through which the memory size applied for by sqlite3 can be monitored in real time. If the memory keeps increasing and is not recycled, then the pointer must not be freed somewhere.

But sometimes you observe that the size of sqlite3_memory_used has not changed, but through the command top or free you find that the program memory is increasing and is not recycled. At this time, it may be that the system has not recycled the cache of the database. In order to quickly add, delete, modify, and query, the database will Data is loaded into cache to speed up. You can clear the cache by calling the following command:

sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

As for why it is echo 3, the explanation is as follows:
0: It is the system default value. By default, it means that the memory is not released and is automatically managed by the operating system.
1: Release the page cache
2: Release dentries and inodes
3: Release all caches

Guess you like

Origin blog.csdn.net/weixin_43246170/article/details/130988139