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

标签 ios objective-c memory memory-management ios7

所以,我有一些边界(addBoundaryWithIdentifier:),有生成 block 的代码(UIView方 block ),每秒使用 performSelector:,方 block 会掉落,如果与边界发生碰撞,我会移除项目。

现在,如果我添加 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

最佳答案

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

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

关于ios - 为什么 UILabel 会增加内存使用量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809048/

相关文章:

ios - 你能检测到文本字段是否突出显示了文本吗

ios - 如何将 UITableView 子类化以在其他 viewController 中使用

objective-c - NSString 和 NSTextfield 的东西

c++ - 如果在 C++ 中自动存储的对象没有被调用 exit 销毁,那么离开程序后这个对象会发生什么?

memory - 是一个 Fortran 子例程,带有指定大小的虚拟参数线程安全数组

iphone - UiImagePickerController takePicture 问题

c# - 单点触控 : UIDocumentInteractionController preview right bar button not working in iPhone

iPhone 应用程序加载器故障排除

iphone - [exifData tagValue :] 返回 Nil 指针

php - 如何减少 Debian 9 中的 PHP FPM 和 MySQL 内存使用