【问题标题】:iphone sqlite3 object allocation memory up but no leaksiphone sqlite3 对象分配内存增加但没有泄漏
【发布时间】:2010-11-02 15:57:54
【问题描述】:

我一直在试图弄清楚wh。每次我调用这个函数时,我的对象分配都会保持正确,仪器报告没有泄漏,但我得到了很多来自

的对象
sqlite3_exec --> sqlite3Prepare --> sqlite3Parser --> yy_reduce --> malloc  & also a whole bunch from 

& from

sqlite3Step --> sqlite3VdbeExec --> sqlite3BtreeInsert --> malloc

我尝试按照此处发布的建议解决它:http://www.iphonedevsdk.com/forum/iphone-sdk-development/7092-sqlite3-database-gobbling-up-memory.html 但无法修复它

感谢您的帮助,我的代码如下

+(void)getDesignationsInLibrary:(NSString *)library
{
NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];    
NSString *dbName = @"s8.sqlite";



NSArray *documentPaths = \
NSSearchPathForDirectoriesInDomains \
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = \
[documentPaths objectAtIndex:0];
NSString *databasePath = \
[documentsDir stringByAppendingPathComponent:dbName];

[[DT sharedDT].designationsInLibrary removeAllObjects];

NSString *sqlString;

for(int i=0;i<[[DT sharedDT].typesInLibrary count];i++)
{   

 if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK)
 {
     if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) {
         NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db));
     }
     NSMutableString *lib=[NSMutableString stringWithString:library];

     [lib appendString:@"-"];
     [lib appendString:[[DT sharedDT].typesInLibrary objectAtIndex:i]];

     if([DT sharedDT].sortedBy==@"AISC Default")
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\";",lib];
     }
     else
     {
         sqlString = [NSString stringWithFormat:@"select DESIGNATION from \"%@\" order by cast(%@ as numeric) %@;",lib, [DT sharedDT].sortedBy, [DT sharedDT].sortAscDesc]; 
     }

     const char *sql = [sqlString cStringUsingEncoding:NSASCIIStringEncoding];
     sqlite3_stmt *selectstmt;

     if(sqlite3_prepare_v2(db,sql,-1,&selectstmt, NULL)==SQLITE_OK)
     {
            while(sqlite3_step(selectstmt)==SQLITE_ROW)
            {
                [[DT sharedDT].designationsInLibrary addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,0)]];
            }
            sqlite3_finalize(selectstmt);
         selectstmt=nil;
     }
 }

}   
sqlite3_close(db);
[localPool release];
}

【问题讨论】:

    标签: iphone memory object sqlite allocation


    【解决方案1】:

    看来,您在每个循环周期都打开 db,但在函数退出之前只关闭一次

    所以尝试改变:

    } sqlite3_close(db); [localPool 释放]; }

    sqlite3_close(db); } [localPool 释放]; }

    甚至更好的改变:

    for(int i=0;i [[DT sharedDT].typesInLibrary count];i++) { if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK) { if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) { NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db)); }

    到:

    if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK) { if (sqlite3_exec(db, "PRAGMA CACHE_SIZE=50;", NULL, NULL, NULL) != SQLITE_OK) { NSAssert1(0, @"Error: failed to set cache size with message '%s'.", sqlite3_errmsg(db)); } for(int i=0;i [[DT sharedDT].typesInLibrary count];i++) { ...

    因为你总是打开同一个数据库

    【讨论】:

    • 同意。每次循环都打开数据库是一个非常糟糕的主意。
    【解决方案2】:

    尝试使用以下命令调用 sqlite3_exec:

    pragma cache_size=1 
    

    Sqlite 似乎会占用内存进行缓存。

    【讨论】:

    • 我同意,在我的应用程序中也看到了这种行为。 sqlite 占用大量内存进行缓存。关闭数据库并在 didReceiveMemoryWarning 中再次打开它似乎有效
    猜你喜欢
    • 2019-11-11
    • 2011-03-20
    • 2021-12-25
    • 2011-10-20
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    相关资源
    最近更新 更多