在数据库中进行一系列插入后,内存不会减少

时间:2014-05-31 14:33:36

标签: ios objective-c sqlite

我有简单的代码将值插入我的数据库:

-(void)inserirBd:(NSString *)valores{

    sqlite3_stmt *statement;
    // Get the documents directory
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = dirPaths[0];

    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"fazerbem.sqlite"]];
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {

        NSString *insertSQL = valores;

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(myDatabase, insert_stmt,
                           -1, &statement, NULL);


        if (sqlite3_step(statement) == SQLITE_DONE) {
        } else {
            NSLog(@"Error -> %s", sqlite3_errmsg(myDatabase));
        }

        sqlite3_finalize(statement);
        sqlite3_close(myDatabase);
    }
}

我以这种方式调用此函数:

cInsert = [NSString stringWithFormat:@"INSERT INTO fazerbem_products (value) Values ('%@')", p1_1.text,];

    InsertBD *insert = [[InsertBD alloc] init];
    [insert inserirBd:cInsert];

为了执行一些内存测试,我把上面的命令放在一个循环中,在调试内存中重复插入85,000次,然后在执行循环后将值插入数据库,内存为1.1 MB内存增加到大约500 MB。

但在那之后,为什么不再将内存降低到1.1 MB?因为循环已经处理过,在这种情况下应该释放内存? (我正在使用ARC,我的应用只有一个按钮来执行此操作)

1 个答案:

答案 0 :(得分:1)

Xcode调试内存报告不是您想要的,也不是您认为的。

Debug Navigator显示与“Activity Monitor”工具相同的内容。它没有显示您的应用程序的当前实时内存,它显示操作系统分配给您的应用程序的当前内存。操作系统将根据需要回收未使用但已分配的内存。

这个概念是避免从系统分配内存块,解除分配只需要稍后重新分配。系统分配内存(大块)到应用程序和应用程序分配内存到对象(大块的部分)之间存在差异。你关心第二个,系统关心第一个。

有关详细信息,请参阅@ Putz1103的SO Answer Here

使用Instruments并查看Live Bytes以查看实际的内存使用情况。