Ios 记忆释放目标c

Ios 记忆释放目标c,ios,memory,release,Ios,Memory,Release,在我看来,当我们创建对象并将其设置为nil时,这个对象将被释放。但我尝试向NSMutableDictionary插入大量数据,然后为每个对象设置NIL。记忆仍然存在,并没有减少。请查看我的代码: - (void)viewDidLoad { [super viewDidLoad]; NSMutableDictionary*frameAnotationLeft=[[NSMutableDictionary alloc] init];; // Do any additional

在我看来,当我们创建对象并将其设置为nil时,这个对象将被释放。但我尝试向NSMutableDictionary插入大量数据,然后为每个对象设置NIL。记忆仍然存在,并没有减少。请查看我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
     NSMutableDictionary*frameAnotationLeft=[[NSMutableDictionary alloc] init];;
    // Do any additional setup after loading the view, typically from a nib.

    NSMutableDictionary *  testDict = [[NSMutableDictionary alloc] init];
    frameAnotationLeft = [[NSMutableDictionary alloc] init];
    for ( int j=0; j<1000; j++) {
        for (int i= 0; i<5000; i++) {

            NSString* __weak test = @"dule.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.For the most part, Automatic Reference Counting lets you completely forget about memory management. The idea is to focus on high-level functionality instead of the underlying memory management. The only thing you need to worry about are retain cycles, which was covered in the Properties module.";
            NSMutableArray * arrayTest = [[NSMutableArray alloc] init];
            [arrayTest addObject:test];
            test = nil;
            [testDict setObject:arrayTest forKey:[NSString stringWithFormat:@"%d",i]];

            arrayTest = nil;
        }

        [frameAnotationLeft setObject:testDict forKey:[NSString stringWithFormat:@"%d",j]];

    }

    testDict = nil;

    // Begin to release memory
    for (NSString* key in frameAnotationLeft) {

        NSMutableDictionary *testDict2 = (NSMutableDictionary*)[frameAnotationLeft objectForKey:key];

        for (NSString* key2 in testDict2) {
        NSMutableArray * arrayTest = (NSMutableArray *)[testDict2 objectForKey:key2];
        NSString* test = [arrayTest objectAtIndex:0];
        [arrayTest removeAllObjects];
        test = nil;
        arrayTest = nil;
    }
    [testDict2 removeAllObjects];
    testDict2 = nil;
    }
    [frameAnotationLeft removeAllObjects];
    frameAnotationLeft = nil;

}

运行时的内存为218MB。而且没有减少。有人能帮我解决问题吗?感谢so muuch

如果您使用的是ARC,那么将局部变量设置为nil是不必要的,也不会有任何效果。局部变量完全由内存管理。将变量设置为nil并不会像您想象的那样导致内存本身被清除;它减少了对象的保留计数,但仅此而已。如果对象的保留计数已降至零,则可能会在将来某个时间回收内存,但您无法控制何时会发生这种情况。因此,nil设置不起任何作用,因为反正ARC也在为您做同样的事情


如果您担心在紧循环过程中二级自动释放对象的累积,只需将循环内部包装在@autoreleasepool块中即可。

此外,不要在模拟器中测试内存管理;它的工作方式与真正的设备不同。如果你想知道到底发生了什么,只在设备上测试。谢谢,我在真实设备上测试了iphone和ipad。。我对此很困惑。我也尝试过@autoreleasepool,但没有效果,但正如我所说,只有当内存永远无法回收时才会出现问题。我想你不知道。如果您想知道不需要的对象是否存在,请使用Instruments分配工具。如果您想知道是否存在泄漏,请使用仪器泄漏工具。若并没有,那个么别担心,继续前进。嗯,我用了仪器探查器很多次,泄漏工具并没有显示任何东西。我知道这不是泄漏,但我只想在需要时释放内存读我的答案。内存何时被清除并不取决于你。即使没有ARC,释放也不会清除内存。它只是减少了保留计数。如果没有泄漏,不要担心,开心点,继续前进。让虚拟内存管理系统完成它的功能。