我的数据可视化应用程序在重绘期间会出现大量内存消耗峰值(触发 drawRect 的 setNeedsDisplay)。我目前正在重绘包含数据图的整个视图。此视图比设备显示大得多。
有没有办法告诉 CoreGraphics 分配足够的内存来绘制每个元素(每个元素都是一个比设备显示小得多的小矩形块)并在完成后释放内存,而不是我目前的幼稚方法?
提前致谢。
-道格
更新 12 月 8 日美国东部标准时间上午 8:28
这是带有解释性文字的相关代码。我正在运行具有 ObjectAlloc、内存监视器和泄漏仪器的仪器。我唯一的内存泄漏与 NSOperationQueue 没有释放内存有关。这是次要的,不相关的。
在架构上,该应用程序由一个 tableView 组成,其中包含人类基因组中要检查的有趣位置列表。When a table row is selected I enqueue a data gathering operation that returns data called alignmentData. 然后将该数据绘制为水平矩形板。
最初,当 tableView 启动时,我的内存占用为 5 MB。
- (void)viewWillAppear:(BOOL)animated {
// Initial dimensions for the alignment view are set here. These
// dimensions were roughed out in IB.
frame = self.alignmentView.frame;
frame.origin.x = 0.0;
frame.origin.y = 0.0;
frame.size.width = self.scrollView.contentSize.width;
frame.size.height = 2.0 * (self.containerView.frame.size.height);
}
注意:viewWillAppear: 调用后内存占用没有变化。即使alignmentView 的大小远远超出显示器的尺寸。
这是从数据收集操作中调用的方法。
- (void)didFinishRetrievingAlignmentData:(NSDictionary *)results {
// Data retrieved from the data server via the data gathering operation
NSMutableData *alignmentData = [[results objectForKey:@"alignmentData"] retain];
NSMutableArray *alignments = [[NSMutableArray alloc] init];
while (offset < [alignmentData length]) {
// ...
// Ingest alignmentData in alignments array
// ...
} // while (offset < [alignmentData length])
[alignmentData release];
// Take the array of alignment objects and position them in screen space
// so that they pack densely creating horizontal rows of alignment objects
// in the process.
self.alignmentView.packedAlignmentRows =
[Alignment packAlignments:alignments basepairStart:self.startBasepairValue basepairEnd:self.endBasepairValue];
[alignments release];
[self.alignmentView setNeedsDisplay];
}
在这行代码之后:
self.alignmentView.packedAlignmentRows = ...
内存占用为 13.8 MB
在这行代码之后:
[self.alignmentView setNeedsDisplay];
内存占用飙升至 21.5 MB,停留几秒钟,然后返回到预先存在的 13.8 MB 水平
我正在寻找的解决方案将允许我从本质上创建一个水平渲染缓冲区窗口,该窗口是单行对齐对象的高度。我会将它的内存渲染分配给它,然后丢弃它。我会一遍又一遍地为每一行对齐数据执行此操作。
从理论上讲,我可以使用这种方法渲染无限量的数据,这当然是最出色的 ;-)。
-道格