iPhone + UIView.drawRect期间的内存消耗量很大.减少这个的任何策略?

dug*_*gla 2 iphone uiview drawrect

我的数据可视化应用程序在重绘期间会产生大量内存消耗峰值(setNeedsDisplay会触发drawRect).我目前正在重新绘制包含数据图的整个视图.此视图比设备显示大得多.

有没有办法告诉CoreGraphics分配足够的内存来绘制每个元素(每个元素都是一个比设备显示小得多的小矩形块)并在完成后释放内存,而不是我当前的天真方法?

提前致谢.

-Doug

更新12月8日美国东部时间上午8:28

以下是具有解释性词汇的相关代码.我正在运行使用ObjectAlloc,Memory Monitor和Leaks仪器运行的仪器.我唯一的内存泄漏是由于NSOperationQueue没有释放mems.这是次要的,不相关.

从结构上来说,该应用程序包含一个tableView,其中列出了人类基因组中需要检查的有趣位置.当选择表行时,我将数据收集操作排入队列,该操作返回名为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);

}
Run Code Online (Sandbox Code Playgroud)

注意:在调用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];

}
Run Code Online (Sandbox Code Playgroud)

在这行代码之后:

self.alignmentView.packedAlignmentRows = ...
Run Code Online (Sandbox Code Playgroud)

内存占用量为13.8 MB

在这行代码之后:

[self.alignmentView setNeedsDisplay];
Run Code Online (Sandbox Code Playgroud)

内存占用量达到21.5 MB,在那里停留几秒钟,然后返回到预先存在的13.8 MB的水平

我正在寻找的解决方案将允许我基本上创建一个水平渲染缓冲区窗口,该窗口是单行对齐对象的高度.我会将其内存渲染分配到其中,然后将其丢弃.我会一遍又一遍地为每行对齐数据执行此操作.

从理论上讲,我可以用这种方法渲染无限量的数据,这当然是最优秀的;-).

-Doug

dug*_*gla 6

这是 - 我的记忆问题不是那么明显的答案.我会给自己这个,因为我是在苹果开发论坛Rincewind上学到的 - 一位非常有帮助的Apple工程师BTW.

事实证明,通过将大视图切成N个较小的片段并依次渲染到每个片段中,我将产生大约为大视图大小1/N的存储器尖峰.

因此,对于每个较小的视图:alloc/init,提供我的一部分数据,setNeedsDisplay.冲洗/重复所有N个小视图.

简单,嗯?

在学习之前,我错误地认为setNeedsDisplay:myRect为大视图做了这个.显然不是.

感谢所有建议团伙.

干杯,道格@dugla