<分区>
我正在开发一个 iOS ARC 应用程序(可应要求提供更多代码),之前我正在制作和丢弃大量图像。我认为我在某个地方仍然有对图像的引用,即使我调用了 removeFromSuperview 并试图删除所有对不再使用的图像的引用。我尝试了 Leaks,Leaks 报告内存使用量随着时间的推移大致呈线性增长,从 17M 左右开始。
我仔细检查并将所有对图像的引用替换为实例变量,因此它们将占用少量、有限且固定的内存,并转换而不是删除用于时钟指针的图像。不幸的是,这导致内存使用量随着时间的推移缓慢增加,从 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];
}
感谢您的帮助。你明白为什么它应该更新一次显示然后不继续保持更新吗?
谢谢,
最佳答案
首先,您应该使用 Instruments 中的 Allocations 工具来确定正在分配和未释放的内容。我建议你想要 WWDC 2012 视频 iOS App Performance: Memory ,除其他外,这证明了这一点。
使用 Instrument 的 Allocations 工具,您不会猜测什么正在分配和未被释放,而是您可以使用堆快照/生成来识别在您选择的某个时间范围内正在分配和未释放的实际对象.您甚至可以深入查看违规对象的最初分配位置。
除此之外,看一眼您的代码,您似乎正在删除并重新添加容器,但没有任何内容进入容器。您正在添加时针/分针/秒针,但从不删除它们。也许您打算将它们添加到容器中?
此外,您的代码没有描述 _changed
的作用,但如果是 YES
,那么您实际上是在创建一个 _containerView
,从它的 super View 中删除它(即使它从未添加到 super View ),然后丢弃它并创建另一个 _containerView
。很奇怪。
我还从这段代码中推断出您将多次调用 renderScreen
。如果这是真的,那么我可能建议将 View 内容的“创建”(添加背景、各种图像等)与 View 的“更改”(transform
值的调整)分开).如果可能,您应该添加一次 View ,然后在更新时执行最少的操作(更改转换)。
关于ios - 当内存消耗攀升但 Leaks 未检测到泄漏时,如何处理 iOS 中的泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19348298/