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

标签 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,我的应用程序只有一个按钮可以执行此操作)

最佳答案

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

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

这个概念是为了避免从系统中分配内存块,只在以后需要重新分配时才释放内存块。应用程序的系统分配内存(大块)与对象(大块的一部分)的应用程序分配内存之间存在差异。你关心第二个,系统关心第一个。

查看此 SO Answer Here由@Putz1103 提供完整的详细信息。

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

关于ios - 在数据库中进行一系列插入后内存不会减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971235/

相关文章:

ios - 有选择地在 Mac OS X 上为 iOS 模拟器使用 Charles Proxy

android - 在Phonegap SQLite中根据之前的查询结果进行查询

php 用户 ID 分配不起作用

ios - Alamofire 无法更新类属性数据

ios - UICustomTableViewCell - 在中间垂直设置标签

objective-c - 在 Xcode/Cocoa 中更改应用程序名称

ios - 如何将这个 objective-c 转换为 swift

SQLite:是否通过回滚事务来撤消杂注语句?

ios - WKWebView 获取 Javascript 错误

objective-c - iPhone/iPad : Unable to copy folder from NSBundle to NSDocumentDirectory