为什么UILabel会增加内存使用量?

时间:2014-05-22 14:00:30

标签: ios objective-c memory memory-management ios7

所以,我有一些边界( addBoundaryWithIdentifier: ),有一个代码,每秒用 UIView 正方形) > performSelector: ,块落下,如果发生与边界的碰撞,我会删除项目。

现在,如果我添加UILabel进行查看,内存使用量会慢慢增加到15Mb,然后停止攀升。

没有UILabel使用的内存: image 使用UILabel的内存: image 几分钟后使用UILabel的内存: image

这里发生了什么?如何解决?我应该解决这个问题吗?

这是我正在使用的代码。

@implementation ESViewController {
    UIPushBehavior *pushBlock;
    UIDynamicAnimator *mainAnimator;
    UICollisionBehavior *blockCollision;
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(110, 400, 100, 50)];
//    [label setText:@"Hello"];
//    [label setTextAlignment: NSTextAlignmentCenter];
//    [self.view addSubview: label];

    mainAnimator = [[UIDynamicAnimator alloc] initWithReferenceView: self.view];

    blockCollision = [[UICollisionBehavior alloc] initWithItems: @[]];
    [blockCollision addBoundaryWithIdentifier:@"bottom"
                                    fromPoint:CGPointMake(0, 480)
                                      toPoint:CGPointMake(320, 480)];
    [blockCollision addBoundaryWithIdentifier:@"left"
                                    fromPoint:CGPointMake(0, 480)
                                      toPoint:CGPointMake(0, 0)];
    [blockCollision addBoundaryWithIdentifier:@"right"
                                    fromPoint:CGPointMake(320, 480)
                                      toPoint:CGPointMake(320, 0)];
    [blockCollision addBoundaryWithIdentifier:@"top"
                                    fromPoint:CGPointMake(0, -40)
                                      toPoint:CGPointMake(320, -40)];
    [blockCollision setCollisionDelegate: (id<UICollisionBehaviorDelegate>)self];
    [mainAnimator addBehavior: blockCollision];

    pushBlock = [[UIPushBehavior alloc] initWithItems: @[] mode: UIPushBehaviorModeContinuous];
    [pushBlock setAngle: M_PI/2 magnitude:0.05];
    [mainAnimator addBehavior: pushBlock];

    [self movingBlocks];
}

- (void) movingBlocks {
    UIView *block = [[UIView alloc] initWithFrame:CGRectMake(arc4random() % 320, -20, 20, 20)];
    [block setBackgroundColor: [UIColor brownColor]];
    [self.view addSubview: block];
    [blockCollision addItem: block];
    [pushBlock addItem: block];
    [self performSelector:@selector(movingBlocks) withObject:nil afterDelay:0.05];
}

- (void)collisionBehavior:(UICollisionBehavior *)behavior
      beganContactForItem:(id<UIDynamicItem>)item
   withBoundaryIdentifier:(id<NSCopying>)identifier
                  atPoint:(CGPoint)p {
    [pushBlock removeItem:item];
    [blockCollision removeItem:item];
    [(UIView*)item removeFromSuperview];
}

@end

1 个答案:

答案 0 :(得分:2)

我已经复制了你所描述的症状,但我相信Xcode对你说谎,你应该打开雷达(bugreport.apple.com)。

如果您在Instruments中运行此功能,则内存使用情况与您的预期完全一致。我试过在发布模式下使用Xcode配置文件(比如Instruments),我已经在所有iPad模拟器上运行了。该症状在Xcode中非常可重复,而Instruments显示预期结果并且非常稳定。我甚至将一个显示Xcode增长的运行流程转移到了Instruments,而且仪器仍然没有增长。我会说Xcode在这里被破坏了。