ios - 查询sqlite数据库时内存分配不断增加

标签 ios sqlite memory-management

+ (BOOL) isExistingTicket:(NSString *)TicketID{
int numrows=0;

sqlite3 *database;

NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:dbName];
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
    NSString *sqlString = [NSString stringWithFormat:@"select count(*) from tickets WHERE  ticket_id = '%@' LIMIT 1",TicketID];
    sqlite3_stmt *selectStatement;


    int returnValue = sqlite3_prepare_v2(database, [sqlString UTF8String], -1, &selectStatement, NULL);


    if (returnValue == SQLITE_OK)
    {
        if(sqlite3_step(selectStatement) == SQLITE_ROW)
        {
            numrows= sqlite3_column_int(selectStatement, 0);
        }
    }


    sqlite3_finalize(selectStatement);
}
else
    sqlite3_close(database);

if (numrows > 0) {
    return YES;
}else{
    return NO;
}

大约 200 个比较的数据集正在我的应用程序中使用此查询。多次调用此函数时,应用程序内存使用量不断增加。它消耗超过 25Mb,之后不会减少。为什么会这样?您能提出任何优化建议吗?

最佳答案

此代码仅在 sqlite3_open 调用失败时关闭数据库。

sqlite3_close 调用移动到 if 的第一个分支。

关于ios - 查询sqlite数据库时内存分配不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24756331/

相关文章:

c++ - Wt C++ 如何为分配的对象调用 delete

ios - 使用 AVSpeechSynthesizer 读取 map 上的位置描述

ios - 使用 Xcode 11 构建旧版本的 React-Native 应用程序

ios - 使用图像对 UIView 的位置和大小进行动画处理的正确方法(此方法在 Xcode 6 中中断)?

java - 结果集已关闭。为什么?

php - 如何找到泄漏内存的 PHP 脚本?

iphone - "initialiser element is not a compile-time constant"创建 GCD 队列时

r - 在 R 中使用 SQLite 的多个同时线程

java - 在android中的SQLite数据库中保存用户名和密码的程序

linux - 将变量存储在任何地方(交换空间、磁盘)但不在物理内存中