【问题标题】:Why UILabel increasing memory usage?为什么 UILabel 会增加内存使用量?
【发布时间】:2014-07-11 14:55:53
【问题描述】:

所以,我有一些边界 (addBoundaryWithIdentifier:),有一个代码每秒生成块 (UIView 方块) performSelector: ,方块掉落,如果与边界发生碰撞,我删除项目。

现在,如果我将 UILabel 添加到视图中,内存使用量会慢慢增加到 15Mb,然后停止爬升。

不使用 UILabel 的内存: 与 UILabel 一起使用的内存: 几分钟后使用 UILabel 的内存:

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

这是我正在使用的代码。

@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

【问题讨论】:

  • 仪器是否显示泄漏?如果不是,那么就没有理由断定任何事情都需要修复。 15mb 完全未使用的 RAM 与 15mb 用作缓存一样有益,但如果出现内存不足警告会立即释放,因此 UIKit 通常更喜欢后者。
  • 您的代码没有(基于上述)使用任何 UILabel。
  • @HotLicks 看顶部;它被注释掉了。它实际上是一个 UILabel。
  • @RobNapier - 就像我说的,上面的代码没有使用任何 UILabel。

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


【解决方案1】:

我已经重现了您所描述的症状,但我认为 Xcode 对您撒谎,您应该打开雷达 (bugreport.apple.com)。

如果您在 Instruments 中运行此程序,内存使用情况将完全符合您的预期。我已经尝试在发布模式下使用 Xcode 配置文件(就像 Instruments 一样),并且我已经在所有 iPad 模拟器上运行。该症状在 Xcode 中非常可重现,而 Instruments 显示了预期的结果并且非常稳定。我什至将一个在 Xcode 中显示增长的正在运行的进程转移到 Instruments 中,但 Instruments 仍然没有显示增长。我会说 Xcode 在这里被破坏了。

【讨论】:

    猜你喜欢
    • 2013-02-14
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    相关资源
    最近更新 更多