【问题标题】:Where is this leaking? / Why am I having memory issues?这是哪里漏的? / 为什么我有记忆问题?
【发布时间】:2011-07-24 14:53:46
【问题描述】:

我在创建 iPad 应用时遇到了一些问题。有一个大约 80 帧的简单图像序列/动画。

代码如下(这是一个UIView子类的子类):

- (id)init {
    UIImage *theImage = [UIImage imageNamed:@"chart0075.jpg"];

    // get the frame
    CGRect lgeFrame = CGRectMake(20, 130, theImage.size.width, theImage.size.height);

    // set the new frame
    CGFloat newHeight = theImage.size.height/1.65;
    CGFloat newWidth = theImage.size.width/1.65;

    CGRect smlFrame = CGRectMake(480, 200, newWidth, newHeight);

    self = [super initWithLargeFrame:lgeFrame smallFrame:smlFrame];
    if(self){
        // lets add the image as an image view
        theImageView = [[UIImageView alloc] initWithImage:theImage];
        [theImageView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [theImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        [self addSubview:theImageView];

        // now we need to make an array of images for the image sequence
        NSMutableArray *imageSeq = [NSMutableArray array];
        for(int i = 1; i < 76; i++){
            NSString *jpgnumber;
            if(i<10){
                jpgnumber = [NSString stringWithFormat:@"000%i",i];
            }
            else {
                jpgnumber = [NSString stringWithFormat:@"00%i",i];
            }
            NSString *imageFile = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"chart%@",jpgnumber] ofType:@"jpg"];
            [imageSeq addObject:[UIImage imageWithContentsOfFile:imageFile]];
        }
        [theImageView setAnimationImages:imageSeq];
        [theImageView setAnimationDuration:1.5];
        [theImageView setAnimationRepeatCount:1];
    }

    return self;
}

然后在反向捏合手势上,图像应该具有动画效果。我第一次做这个反向捏合手势需要几秒钟来启动动画。有时我会收到 1 级内存警告,应用会崩溃。

这里有什么问题? 80 jpg 是否太多,无法一次记住?它们总共不到 2mb,所以它们肯定不应该填满 ipad 的内存吧?

我用分配工具查看了它,这表明我在动画时有大约 40kb 的内存,但在随后的动画中这又回到了 0。 (虽然分配工具确实让我很困惑)。

有人知道是什么原因造成的吗?如果需要,我可以发布更多代码或任何内容?

非常感谢:)

【问题讨论】:

  • 我不知道它是否有帮助,但我会将 NSString *jpgnumber; 和 NSString *imageFile; 移到 for 循环 See here 之外。另外,我会使用NSMutableArray *imageSeq = [[NSMutableArray alloc] init];。只要有可能,我就会避免使用自动释放的对象。
  • 您好。我已经使我的可变数组和图像非自动释放,并且我已经将所有指针移到了 for 循环之外,但我仍然遇到同样的问题...... :( 感谢您的帮助。

标签: objective-c cocoa-touch ipad memory-management memory-leaks


【解决方案1】:

您的内存使用量取决于未压缩图像的大小。宽度乘以高度时间 4 将告诉您每个图像将占用的字节数,乘以图像数得到总数。

我的猜测是,你正处于记忆力过剩的边缘。

确保使用 VMTracker 仪器在仪器中运行。您应该查看 Dirty Resident 内存。

WWDC '10 和 WWDC '09 都有关于仪器和内存使用分析的精彩内容。

【讨论】:

  • 嗯,我得到了 89.15 的脏大小和 90.64mb 的驻留。我猜这很高,因为该工具的时间线已经填满了。我如何找出造成这种情况的原因?
  • 感谢您的帮助,我发现了问题... :( 当我浏览我的导航界面时,有两个导航控制器上有类似的动画,这就是导致内存增加的原因使用情况。如果我转到最后一个导航控制器并使用 vm 跟踪器,那么内存会达到 90mb,但是如果我回到带有动画的视图控制器之前,内存使用量会下降到 20mb。我将如何获得围绕这个?有什么想法吗?
  • 当然,不要一次加载所有图像,你不能一次在屏幕上显示 90MB 的图像,所以你应该流式传输它们,平铺它们,或者任何其他方法可以帮助你只有在任何时候都需要记忆。
【解决方案2】:

你没有释放 ImageView

[theImageView release]

【讨论】:

  • 它很可能是一个实例变量。
  • 它是......但它正在 dealloc 中发布......我需要对它的引用来启动动画。不幸的是,这不是问题。
猜你喜欢
  • 2011-02-03
  • 2019-12-26
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
相关资源
最近更新 更多