【发布时间】: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