0

我正在使用iOS ARC应用程序(更多代码可根据要求提供),并且早期我正在制作并丢弃大量图像。我认为我的某个地方仍然有对图像的引用,即使我调用removeFromSuperview并试图删除所有对不再使用的图像的引用。我试过Leaks,Leaks报告说内存使用量随着时间的推移大致线性增加,大约从17M开始。如何处理内存消耗增加但泄漏未检测到泄漏的iOS泄漏?

我将所有对图像的引用都替换为实例变量,因此它们将占用一个小的,有限的和固定的内存量,并且转换用于钟针的图像,而不是摆脱这些图像。不幸的是,这导致内存使用量随着时间的推移缓慢增加,从5M开始而不是17M,但是另一个相同的问题,只是转化为更好的起点。

我的代码的修剪版本如下。你能告诉我什么是泄漏(或“伪泄漏”,因为泄漏没有表明泄漏)关于这一点,以及我如何可以保持接近代码启动时使用的内存边界?

谢谢,

- (void) renderScreen 
{ 
int height = floor([[UIScreen mainScreen] bounds].size.height + .4); 
int width = floor([[UIScreen mainScreen] bounds].size.width + .4); 

if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984) 
{ 
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait.png"]; 
    } 
    else 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default-Landscape.png"]; 
    } 
} 
else if (height == 1536 || height == 768 || height == 748 || height == 728) 
{ 
    _backgroundImage = [UIImage imageNamed:@"Background-Default-Landscape.png"]; 
} 
else if (height == 1136 || height == 1116 || height == 1096) 
{ 
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default-568.png"]; 
    } 
    else 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default-Rotated-568.png"]; 
    } 
} 
else if (height == 960 || height == 940 || height == 920 || height == 480 || height == 460 || height == 440) 
{ 
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default.png"]; 
    } 
    else 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default-Rotated.png"]; 
    } 
} 
else if ((height == 640 || height == 620 || height == 600) && (width == 1136 || width == 1116 || width == 1096)) 
{ 
    _backgroundImage = [UIImage imageNamed:@"Background-Rotated-568.png"]; 
} 
else if ((height == 640 || height == 620 || height == 600 || height == 320 || height == 300 || height == 280) && (width == 960 || width == 940 || width == 920 || width == 480 || width == 470 || width == 410)) 
{ 
    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait.png"]; 
    } 
    else 
    { 
     _backgroundImage = [UIImage imageNamed:@"Background-Default-Rotated.png"]; 
    } 
} 
else 
{ 
    _backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait.png"]; 

} 
if (!UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) 
{ 
    int juggle = height; 
    height = width; 
    width = juggle; 
} 
NSUInteger centerX = width * .5; 
NSUInteger centerY = height * .5; 

_containerRect = CGRectZero; 
_containerRect.size = [[UIScreen mainScreen] bounds].size; 

self.view.backgroundColor = [UIColor colorWithPatternImage:_backgroundImage]; 
_backgroundView = [[UIImageView alloc] initWithImage:_backgroundImage]; 
[self.view addSubview:_backgroundView]; 
if (_changed) 
{ 
    _containerView = [[UIView alloc] initWithFrame:_containerRect]; 
} 

double timeStampSeconds = [[NSDate date] timeIntervalSince1970]; 
double hours = fmod(timeStampSeconds/86400, 24); 
double minutes = fmod(timeStampSeconds/3600, 60); 
double seconds = fmod(timeStampSeconds,  60); 
NSLog(@"Milliseconds: %lf, Hours: %.0f, minutes: %.0f, seconds: %.0f", timeStampSeconds * 1000.0, hours, minutes, seconds); 
[_containerView removeFromSuperview]; 
_containerView = [[UIView alloc] initWithFrame:_containerRect]; 
_hourHandImage = [UIImage imageNamed:@"hour-hand.png"]; 
_hourHandView = [[UIImageView alloc] initWithImage:_hourHandImage]; 
_hourHandImage = [UIImage imageNamed:@"hour-hand.png"]; 
_hourHandView = [[UIImageView alloc] initWithImage:_hourHandImage]; 
[self.view addSubview:_hourHandView]; 

_hourHandView.layer.anchorPoint = CGPointMake(0.5f, 0.5f); 
_hourTransform = CGAffineTransformMakeTranslation(centerX, centerY); 
_hourTransform = CGAffineTransformTranslate(_hourTransform, -17, -127); 
_hourTransform = CGAffineTransformRotate(_hourTransform, hours/12.0 * M_PI * 2.0); 
_minuteTransform = CGAffineTransformMakeTranslation(centerX, centerY); 
_minuteTransform = CGAffineTransformTranslate(_minuteTransform, -10, -182); 
_minuteTransform = CGAffineTransformRotate(_minuteTransform, minutes/60.0 * M_PI * 2.0); 
_hourHandView.transform = _hourTransform; 
_minuteHandImage = [UIImage imageNamed:@"minute-hand.png"]; 
_minuteHandView = [[UIImageView alloc] initWithImage:_minuteHandImage]; 
_minuteHandView.transform = _minuteTransform; 
[self.view addSubview:_minuteHandView]; 
_minuteTransform = CGAffineTransformRotate(_minuteTransform, minutes/60.0 * M_PI * 2.0); 
_secondHandImage = [UIImage imageNamed:@"second-hand.png"]; 
_secondTransform = CGAffineTransformMakeTranslation(centerX, centerY); 
_secondTransform = CGAffineTransformTranslate(_secondTransform, -10, -189); 
_secondTransform = CGAffineTransformRotate(_secondTransform, seconds/60.0 * M_PI * 2.0); 
_secondHandView = [[UIImageView alloc] initWithImage:_secondHandImage]; 
_secondHandView.transform = _secondTransform; 
[self.view addSubview:_secondHandView]; 
} 

- 编辑 -

我已经重构了一个方法为两种方法,一个用于初始显示,以及一个用于增量更新。看起来增量更新方法只被调用一次,因为时钟被冻结并且签名日志语句只被调用一次。

我现在有,对于更新部分:

- (void)render 
{ 
    [self renderScreenInitial]; 
    [NSTimer scheduledTimerWithTimeInterval:0.002 
            target:self 
            selector:@selector(renderingTimer:) 
            userInfo:nil 
            repeats:YES]; 
} 

- (void) renderScreenIncremental 
{ 
    int height = floor([[UIScreen mainScreen] bounds].size.height + .4); 
    int width = floor([[UIScreen mainScreen] bounds].size.width + .4); 
    if (!UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) 
    { 
     int juggle = height; 
     height = width; 
     width = juggle; 
    } 
    double decibelAngle = M_PI/4 + (_decibel/60) * M_PI/2; 
    double decibelAngleDifference = decibelAngle - _previousDecibelAngle; 
    _previousDecibelAngle = decibelAngle; 
    NSLog(@"%lf %lf", _decibel, decibelAngle); 
    _decibelNeedleView = [[UIImageView alloc] initWithImage:_decibelNeedle]; 
    // CGAffineTransform decibelTransform = CGAffineTransformMakeTranslation(centerX, centerY); 
    CGAffineTransform decibelTransform = CGAffineTransformMakeRotation(decibelAngleDifference); 
    decibelTransform = CGAffineTransformTranslate(decibelTransform, sin(decibelAngle - .06) * -298, -cos(decibelAngle - .06) * 298); 
    _decibelNeedleView.transform = decibelTransform; 
    [self.view addSubview:_decibelNeedleView]; 
    double timestampSeconds = [[NSDate date] timeIntervalSince1970]; 
    double timestampSecondsDifference = timestampSeconds - _previousTimestampSeconds; 
    _previousTimestampSeconds = timestampSeconds; 
    double hoursDifference = fmod(timestampSecondsDifference/86400, 24); 
    double minutesDifference = fmod(timestampSecondsDifference/3600, 60); 
    double secondsDifference = fmod(timestampSecondsDifference,  60); 
    NSLog(@"Milliseconds: %lf, Hours: %.0f, minutes: %.0f, seconds: %.0f", timestampSecondsDifference * 1000.0, hoursDifference, minutesDifference, secondsDifference); 
    _hourHandView.transform = CGAffineTransformMakeRotation(hoursDifference); 
    _minuteHandView.transform = CGAffineTransformMakeRotation(minutesDifference); 
    _secondHandView.transform = CGAffineTransformMakeRotation(secondsDifference); 
} 

-(void)renderingTimer:(NSTimer *)timer { 
    [self renderScreenIncremental]; 
} 

我欣赏的帮助。你知道为什么它应该更新显示一次,然后不继续保持更新?

感谢,在内存使用您正在使用是预期的行为API的

+0

你能否确认'renderingTimer:'是否被调用?我假设你从主队列中调用了'render'?顺便说一句,在每个“0.002”秒内调用它可能没有意义。我会把这个定时器减少到一个合理的水平(比如每秒30-60次)。 – Rob

回答

0

的逐渐增加。 [UIImage imageNamed:]will cache the image。如果您想要测试刷新缓存时是否释放这些缓存,则会导致发生内存警告。您可以在模拟器中通过Hardware菜单并选择Simulate Memory Warning来完成此操作。

你也可以用下面的代码做这在设备编程:

[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification 
                object:[UIApplication sharedApplication]]; 

YOU SHOULD NOT船舶生产APP有了这个代码

+0

1.是的,使用'imageNamed'缓存将导致内存增长,因为您的应用使用越来越多不同的图像。但是,如果应用程序反复访问相同的图像,内存使用情况不会像这样蔓延。所以这是一个很好的观察,它困扰了很多应用程序,它们对于很少使用的图像使用'imageNamed'草率,但我认为这不是问题。 – Rob

2

首先,你应该使用分配的工具在仪器中确定什么是分配和不被释放。我建议你想WWDC 2012的视频iOS App Performance: Memory,这表明了这一点,除其他事项。

使用仪器的分配工具,你不会猜什么东西被分配,而不是被释放,而是可以使用heapshots /代,以确定被分配,而不是被一些时间范围内发布的实际对象的采摘。您甚至可以钻入并查看有问题的对象最初分配的位置。

随着的是,除了在你的代码一眼,看来你删除并重新添加容器,但没有什么是进入容器中。并且您正在添加小时/分钟/秒手,但不会移除它们。也许你打算把这些添加到容器中?

而且,您的代码不说明什么呢_changed,但如果这YES,那么你有效地创建一个_containerView,从它的父删除它(即使它从未被添加到上海华),然后丢弃它并创建另一个_containerView。很奇怪。

我也从这个代码推断,你将多次拨打renderScreen。如果这是真的,那么我可能会建议从视图的“更改”(调整transform值)中分离视图内容的“创建”(添加背景,各种图像等)。如果可能的话,你应该添加你的视图一次,然后在更新时尽量减少(改变变换)。

+0

谢谢;我确实想观看iOS应用程序性能:内存。不幸的是,Safari在停止播放之前只播放了前三分钟的预告片,而我还没有将它加载到iTunes中。 ) - :_containerView是早期效果的残留物。但是,我所看到的可操作性主要是将原始设置从增量重复调整中分离出来。对此... – JonathanHayward

+0

如果您有付费的iOS开发者帐户,所有WWDC视频都可用[可用](https://developer.apple.com/wwdc/videos/),无需额外费用。 iOS开发者帐户的费用为99美元。 – zaph