我有一个带有select语句的函数,该语句从一个表中获取一些信息,并使用该信息的一部分来使用另一个select语句从另一个表中获取更多信息。然后,该函数将两个表中的信息添加到一个对象数组中。
看起来很简单,但每次加载使用该功能的ViewController时,内存使用率都会上升,而不会再次降低。如果我注释掉对此函数的调用,那么内存使用情况就会好了。
-(void)displayExhibitor
{
arrayOfExhibitors = [[NSMutableArray alloc] init];
if (sqlite3_open([[self filePath] UTF8String], &congressDB) == SQLITE_OK){
sqlite3_stmt *sqlStatementExhibitor;
NSString *sqlQuery = [NSString stringWithFormat:@"SELECT * FROM Exhibitor ORDER BY Name"];
const char *sqlQueryChars = [sqlQuery UTF8String];
if (sqlite3_prepare(congressDB, sqlQueryChars, -1, &sqlStatementExhibitor, NULL)==SQLITE_OK) {
while (sqlite3_step(sqlStatementExhibitor)==SQLITE_ROW) {
NSString *name = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(sqlStatementExhibitor, 1)];
NSString *category = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(sqlStatementExhibitor, 4)];
NSString *description = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(sqlStatementExhibitor, 5)];
NSString *locationID = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(sqlStatementExhibitor, 3)];
NSString *location;
// Get exhibitor location information from location table
sqlite3_stmt *sqlStatementLocation;
NSString *sqlQueryLocation = [NSString stringWithFormat:@"SELECT Name FROM Location WHERE _ID = '%@'", locationID];
const char *sqlQueryCharsLocation = [sqlQueryLocation UTF8String];
if (sqlite3_prepare(congressDB, sqlQueryCharsLocation, -1, &sqlStatementLocation, NULL)==SQLITE_OK) {
while (sqlite3_step(sqlStatementLocation)==SQLITE_ROW) {
location = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(sqlStatementLocation, 0)];
}
} else {
NSLog(@"There was a problem with get location - %s",sqlite3_errmsg(congressDB));
}
sqlite3_finalize(sqlStatementLocation);
Exhibitor *exhibitor = [[Exhibitor alloc] init];
[exhibitor setName:name];
[exhibitor setCategory:category];
[exhibitor setDescription:description];
[exhibitor setLocation:location];
[arrayOfExhibitors addObject:exhibitor];
}
} else {
NSLog(@"There was a problem with prepare- %s",sqlite3_errmsg(congressDB));
}
sqlite3_finalize(sqlStatementExhibitor);
} else {
NSLog(@"There was a problem with DB open - %s",sqlite3_errmsg(congressDB));
}
[[self tableView]reloadData];
}
是因为我在另一个内部有一个SQLite3_prepare吗?
修改 我刚刚尝试将代码剥离到最低限度:
-(void)testFunction
{
sqlite3_stmt *sqlStatementExhibitor;
NSString *sqlQuery = [NSString stringWithFormat:@"SELECT * FROM Exhibitor ORDER BY Name"];
const char *sqlQueryChars = [sqlQuery UTF8String];
if (sqlite3_open([[self filePath] UTF8String], &congressDB) == SQLITE_OK){
if (sqlite3_prepare(congressDB, sqlQueryChars, -1, &sqlStatementExhibitor, NULL)==SQLITE_OK) {
while (sqlite3_step(sqlStatementExhibitor)==SQLITE_ROW) {
NSLog(@"Row found");
}
} else {
NSLog(@"There was a problem with prepare- %s",sqlite3_errmsg(congressDB));
}
sqlite3_finalize(sqlStatementExhibitor);
}
}
每次运行时仍然会增加大约250KB的内存使用量,发生了什么?有没有办法可以手动从内存中释放SQL连接?