sqlite3_*()的正常例程有没有内存泄漏? [英] Is there any memory leak in the normal routine of sqlite3_*()?

查看:20
本文介绍了sqlite3_*()的正常例程有没有内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sqlite3_prepare_v2() + sqlite3_step() + sqlite3_finalize() 的正常例程可能包含泄漏.

A normal routine of sqlite3_prepare_v2() + sqlite3_step() + sqlite3_finalize() could contain leak.

听起来很荒谬.但是测试代码好像说了.或者我错误地使用了 sqlite3_*().

It sound ridiculous. But the test code seems to say it. Or I used the sqlite3_*() wrongly.

感谢您的任何回复.

#include <stdio.h> 
#include <unistd.h>     // for usleep() 
#include <sqlite3.h> 


int multi_write (int j); 

sqlite3 *db = NULL; 

int main (void) 
{ 
    int ret = -1; 

    ret = sqlite3_open("test.db", &db); 
    ret = sqlite3_exec(db,"CREATE TABLE data_his (id INTEGER PRIMARY KEY, d1 CHAR(16))", NULL,NULL,NULL); 
    usleep (100000); 


    int j=0; 
    while (1) 
    { 
        multi_write (j++); 
        usleep (2000000); 
        printf ("   ----------- %d\n", j); 
    } 


    ret = sqlite3_close (db); 
    return 0; 
} 


int multi_write (int j) 
{ 
    int ret = -1; 

    char *sql_f = "INSERT OR REPLACE INTO data_his VALUES (%d, %Q)"; 
    char *sql = NULL; 

    sqlite3_stmt *p_stmt = NULL; 


    ret = sqlite3_prepare_v2 (db, "BEGIN TRANSACTION", -1, &p_stmt, NULL); 
    ret = sqlite3_step ( p_stmt ); 
    ret = sqlite3_finalize ( p_stmt ); 

    int i=0; 
    for (i=0; i<100; i++) 
    { 
        sql = sqlite3_mprintf ( sql_f, j*100000 + i, "00000000000068FD"); 

        ret = sqlite3_prepare_v2 (db, sql, -1, &p_stmt, NULL ); 
        sqlite3_free ( sql ); 
        //printf ("sqlite3_prepare_v2(): %d, %s\n", ret, sqlite3_errmsg (db)); 

        ret = sqlite3_step ( p_stmt ); 
        //printf ("sqlite3_step():       %d, %s\n", ret, sqlite3_errmsg (db)); 

        ret = sqlite3_finalize ( p_stmt ); 
        //printf ("sqlite3_finalize():   %d, %s\n\n", ret, sqlite3_errmsg (db)); 
    } 

    ret = sqlite3_prepare_v2 (db, "COMMIT TRANSACTION", -1, &p_stmt, NULL ); 
    ret = sqlite3_step ( p_stmt ); 
    ret = sqlite3_finalize ( p_stmt ); 


    return 0; 
}

然后我观察流程由 top 运行.

And I watch the the process's run by top.

首先,内存统计为:

PID     PPID   USER     STAT   VSZ    %MEM  %CPU  COMMAND 
17731   15488  root     S      1104   5%    7%    ./sqlite3multiwrite 

当main()的while(1){}中的printf()打印150时,内存统计为:

When the printf() in while(1){} of main() prints the 150, the memory statistics is:

PID     PPID   USER     STAT   VSZ    %MEM  %CPU  COMMAND 
17731   15488  root     S      1552   5%    7%    ./sqlite3multiwrite 

听起来在 150 个 for-cycle 之后,sqlite3multiwrite 使用的内存从 1104KB 增加到 1552KB.

It sounds that after 150 for-cycles, the memory used by sqlite3multiwrite increase from 1104KB to 1552KB.

什么意思?内存泄漏或其他事情?

What does it mean? memory leak or other thing?

推荐答案

使用 Valgrind.从 1.1 MB 到 1.5 MB 的增长并不是那么大,尤其是超过 150 次迭代.例如,SQLite 可以做一些缓存(它预先保留一些内存).

Use Valgrind. Growth from 1.1 MB to 1.5 MB is not that big, especially over 150 iterations. SQLite can, for example, do some caching (it reserves some memory in advance).

尝试更多迭代 - 也许有一个阈值,您的程序无法增长.但 Valgrind 是查找内存泄漏最准确的工具.

Try more itarations - maybe there is a threshold value over which your program cannot grow. But Valgrind is the most accurate tool for finding memory leaks.

这篇关于sqlite3_*()的正常例程有没有内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆