iOS - FMDB 使用和内存
我一直在跟踪 iOS 应用程序中的内存泄漏,并且不断使用泄漏工具返回到以下代码:
NSMutableArray *resultSet = [[NSMutableArray alloc] initWithCapacity:3];
NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];
FMResultSet *rs = [db executeQuery:query,equipmentID];
while ([rs next])
{
[resultSet addObject: [rs resultDict]];
}
[rs close];
[innerPool release];
return [resultSet autorelease];
这是 FMDB 的正确用法(就内存管理而言)吗?以下是泄漏工具的屏幕截图:
泄露的详细截图:
I have been tracking down memory leaks in my iOS app and I keep coming back to the following code using the leaks instrument:
NSMutableArray *resultSet = [[NSMutableArray alloc] initWithCapacity:3];
NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];
FMResultSet *rs = [db executeQuery:query,equipmentID];
while ([rs next])
{
[resultSet addObject: [rs resultDict]];
}
[rs close];
[innerPool release];
return [resultSet autorelease];
Is this the correct (in terms of memory management) usage of FMDB? Here is a screenshot of the leaks instrument:
Detailed Screenshot of the leak:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是正确的内存管理。
[rs close];
行在技术上是不必要的,因为当FMResultSet
被释放(作为池耗尽的一部分)时,它会发生(如果还没有发生) 。但明确地把它放在那里就可以了。您是否可能过度保留返回数组?
Yes, this is correct memory management. The
[rs close];
line is technically unnecessary, because it will happen (if it hasn't already) when theFMResultSet
is deallocated (as part of the pool draining). But putting it in there explicitly is fine.Is it possible you're over-retaining the return array?
SQLite 分配并保留一堆内存,只有在数据库关闭时才会释放这些内存。您还可以通过发出“pragma cache_size = nnn”命令来调整它将分配的内存量。
请参阅此相关问题和答案:
sqlite+fmdbvacuum命令后内存泄漏(?)
SQLite allocates and keeps a bunch of memory, which is only freed when the database is closed. You can also adjust how much memory it will allocate by issuing a 'pragma cache_size = nnn' command.
See this related question and answer:
memory leak (?) after sqlite+fmdb vacuum command