iphone - 应用程序中分配了很多内存,如何解决?

标签 iphone ios memory-management memory-leaks

为了测试内存管理和分配,我编写了一个简单的单 View 应用程序,并在 viewDidAppear 中创建了一个包含许多对象的长循环,我这样写:

- (void) viewDidAppear:(BOOL)animated
{
    NSDate * time = [NSDate date];
    [super viewDidAppear:animated];

    for (int i = 0; i < 20003; i++)
    {
        NSString * testString = [[NSString alloc] initWithString:@"This is a test string"];
        NSMutableArray * itemsArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < 1000; j++)
        {
            [itemsArray addObject:testString];
        }
        if ((i % 1000) == 0)
        {
            NSLog(@"called %d", i);
        }
}
NSDate * time2 = [NSDate date];
NSTimeInterval interval = [time2 timeIntervalSinceDate:time];
[label2 setText:[NSString stringWithFormat:@"time interval: %f", interval]];

在分析内存泄漏时,正如预期的那样,存在内存泄漏和超过 260 MB 的分配,屏幕截图: image 1

但是当关注 this 时文档,我将代码更改为:

- (void) viewDidAppear:(BOOL)animated
{
    NSDate * time = [NSDate date];
    [super viewDidAppear:animated];

    for (int i = 0; i < 20003; i++)
    {
        @autoreleasepool
        {
            NSString * testString = [NSString stringWithFormat:@"%@", @"This is a test string"];
            NSMutableArray * itemsArray = [[NSMutableArray alloc] init];
            for (int j = 0; j < 1000; j++)
            {
                [itemsArray addObject:testString];
            }
            [itemsArray release];
            itemsArray = nil;

            if ((i % 1000) == 0)
            {
                NSLog(@"called %d", i);
            }
        }
    }
    NSDate * time2 = [NSDate date];
    NSTimeInterval interval = [time2 timeIntervalSinceDate:time];
    [label2 setText:[NSString stringWithFormat:@"time interval: %f", interval]];

}

分配量仍然没有差异,但没有内存泄漏,执行时间明显增加,从不到 2 秒增加到 80 秒左右: Image 2

而且还是用release之后,没有任何变化。代码:

- (void) viewDidAppear:(BOOL)animated
{
    NSDate * time = [NSDate date];
    [super viewDidAppear:animated];

    for (int i = 0; i < 20003; i++)
    {
        NSString * testString = [[NSString alloc] initWithString:@"This is a test string"];
        NSMutableArray * itemsArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < 1000; j++)
        {
            [itemsArray addObject:testString];
        }
        [testString release];
        testString = nil;
        [itemsArray release];
        itemsArray = nil;

        if ((i % 1000) == 0)
        {
            NSLog(@"called %d", i);
        }
    }
    NSDate * time2 = [NSDate date];
    NSTimeInterval interval = [time2 timeIntervalSinceDate:time];
    [label2 setText:[NSString stringWithFormat:@"time interval: %f", interval]];
}

及截图: enter image description here

显式清空 MutableArray(使用 [itemsArray release])显示没有变化。

我的问题是:

  1. 为什么在三种情况下分配的总内存没有变化(尽管在第二种和第三种情况下没有内存泄漏),有没有办法减少大量分配的内存?是哪里出了问题,如何减少内存消耗?

  2. 其次,我是否使用了正确的工具并读取了正确的数字(使用 Profiler Instruments 中的 Memory Leaks,并引用了 All Allocations,并在 allocation lifetime 中选择了 created and still living)并且我这样做了吗正确的方式?我的意思是这是当前消耗的内存量,还是自应用程序启动以来消耗的总内存量?

  3. 为什么在第二种情况下循环的执行时间显着增加,而在第一种情况和第三种情况下却没有?

使用 xCode 4.5,没有 ARC,没有 Storyboard,所有测试都在模拟器上完成。

最佳答案

1.三种情况下的总分配量将相同,因为在每种情况下您分配的内存量相同。

2. 您正在使用正确的工具,并且阅读了一些正确的数字,我认为您只是没有阅读足够的数字。 Overall Bytes 正如您所怀疑的那样,本质上不是您当前使用的字节数。您需要检查 Live Bytes 部分。在本节中,您将看到第一个案例(浪费了将近一半的总分配)与第二个案例(最终处于相当 slim 的状态)之间的区别。

3.“显着”的时间增加来自使用之间的差异:

[[NSString alloc] initWithString:@"This is a test string"];

@autoreleasepool  -and-
[NSString stringWithFormat:@"%@", @"This is a test string"];

是的,我说的是差异。而不仅仅是 autorelease。但是……

众所周知,第一个自动释放在循环释放内存方面效率低下。它从一开始并不是真正的目的,它只是真正被发明出来,以便方法可以返回对象由对象的调用者拥有,换句话说:它旨在让事情活得太久。虽然 @autorelease 已被证明比旧的 NSAutoReleasePool 更有效,但在这种情况下应避免使用它。

第二个是 NSString 完成的一个相当不为人知的优化,它涉及将字符串文字作为新 NSString 的源字符串的处理。作为described in this answer ,使用 initWithString:@"SomeLiteral" 导致指向文字的指针,而不是形成新的 NSString。此优化不适用于 stringWithFormat:。所以换句话说,你已经歪曲了你的测试。

注意事项:

在保存开始时间后调用 [super viewDidAppear:animated]; 会不必要地扭曲测试。

当我运行这些测试时,它们在分析器中运行,在这些时候:

  • 示例 1:0.999s
  • 示例 2:1.526s
  • 示例 3:1.082s

我不确定您是如何从不到 2 秒变成 80 秒的。

关于iphone - 应用程序中分配了很多内存,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13551868/

相关文章:

ios - iPad应用启动超时

go - 我什么时候应该把对象放回 sync.Pool

iphone - 抑制警告 : Meta method xx in category from xx conflicts with same method from another category

iphone - Objective-C/iPhone 内存管理静态变量

ios - 更新了用户位置 : MKUserLocation for google maps ios swyft

ios - UIRefreshControl 不执行操作

c - 为什么 malloc 中的内存不归零?

iphone - iPhone 内存管理的最佳解释在哪里?

ios - NSURLProtocol 视频请求失败

iphone - UIViewController Containment - 新 child 不知道旋转