如何应对iOS中泄漏时,内存消耗攀升,但泄漏未检测到泄漏? [关闭](How to deal w

2019-10-30 10:17发布

关闭 。 这个问题需要更加专注。 它目前不接受的答案。 了解详情 。


我工作在iOS应用ARC(更应要求提供的代码),以及以前的我是生产和丢弃大量图像。 我想,我还是地方不得不图像的参考,即使我叫removeFromSuperview,并试图消除对使用不再视为图像的所有引用。 我试过泄漏,泄漏和报道随着时间的推移内存使用量大致呈线性增加,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];
}

我欣赏的帮助。 你看为什么要更新一次显示,然后再继续保持更新?

谢谢,

Answer 1:

首先,你应该在仪器使用分配的工具,以确定什么正在分配的,而不是被释放。 我建议你要WWDC 2012的视频iOS应用性能:内存 ,这表明这一点,除其他事项。

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

与放在一边,你的代码一眼,看来你删除并重新添加容器,但没有什么是进入容器中。 而你添加时/分/秒的双手,但从来没有删除它们。 也许你的意思是那些添加到容器?

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

我也是从这个代码,你将调用推断renderScreen多次。 如果这是真的,那么我可能会建议从该视图(调整的“变”分离的观点的东西(添加背景,各种图像等)的“创造” transform值)。 如果可能的话,你应该添加你的观点一次,然后执行最低限度(改变变换)后更新。



Answer 2:

使用您正在使用这些API在内存中的逐渐增加是预期的行为。 [UIImage imageNamed:] 将缓存图像 。 如果你想测试是否当缓存刷新这些得到释放,导致内存警告发生。 您可以通过在模拟器做Hardware菜单并选择Simulate Memory Warning

你也可以做到这一点的设备上编程用下面的代码:

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

你应该不运送生产APP使用此代码



文章来源: How to deal with leaks in iOS when memory consumption climbs but Leaks does not detect a leak? [closed]