iOS NSMutableArray removeObjectAtIndex

时间:2015-01-26 14:45:31

标签: ios objective-c nsmutablearray

这是我在ViewController.m文件中的代码,

- (void)viewDidAppear:(BOOL)animated
 {
   [super viewDidAppear:animated];

   NSLog(@"%f",[self usedMemory]);
   NSMutableArray *array= [[NSMutableArray alloc]init];
   for (int i = 0; i < 100; i++) {
      NSMutableData *data = [NSMutableData dataWithLength:10000];
       [array addObject:data];
   }
   NSLog(@"%f",[self usedMemory]);

   for (int i = 0; i < 100; i++) {
       [array removeObjectAtIndex:0];
   }
   NSLog(@"%f",[self usedMemory]);
}

以下是usedMemory方法:

- (double)usedMemory
{
    task_basic_info_data_t taskInfo;

    mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;

    kern_return_t kernReturn = task_info(mach_task_self(),

                                     TASK_BASIC_INFO,

                                     (task_info_t)&taskInfo,

                                     &infoCount);
    if (kernReturn != KERN_SUCCESS

       )
    {
        return NSNotFound;
    }
    return taskInfo.resident_size / 1024.0 / 1024.0;
}

结果如下:

2015-01-26 22:39:00.058 audio_memory_test[9050:874963] 25.011719
2015-01-26 22:39:00.060 audio_memory_test[9050:874963] 26.312500
2015-01-26 22:39:00.060 audio_memory_test[9050:874963] 26.312500

为什么在删除数组中的对象时没有释放内存? removeObjectAtIndex方法做了什么?我该如何释放这段记忆?

3 个答案:

答案 0 :(得分:3)

在最后一个循环后调用[self usedMemory]时,您的对象仍保留在内存中。他们所属的自动释放池还没有耗尽;这通常发生在您离开源代码范围并且系统再次获得控制权时。

答案 1 :(得分:2)

所有因为[NSMutableData dataWithLength:]返回一个自动释放的对象,所以你得到了完全预期的行为。

要解决此问题:使用[[NSMutableData alloc] initWithLength:]或使用自动释放池。

答案 2 :(得分:0)

正如其他人所说,问题是你正在创建自动释放的对象。以下是您可以对代码进行的更改,以便实际释放对象:

- (void)viewDidAppear:(BOOL)animated
 {
   [super viewDidAppear:animated];

   NSLog(@"%f",[self usedMemory]);

   //all autoreleased objects created inside the braces 
   //of the @autorleasepool directive will be released
   //when we leave the braces
   @autoreleasepool
   {
     NSMutableArray *array= [[NSMutableArray alloc]init];
     for (int i = 0; i < 100; i++) {
        NSMutableData *data = [NSMutableData dataWithLength:10000];
         [array addObject:data];
     }
     NSLog(@"%f",[self usedMemory]);

     for (int i = 0; i < 100; i++) {
         [array removeObjectAtIndex:0];
     }
   }
   NSLog(@"%f",[self usedMemory]);
}