简体   繁体   English

内存释放目标c

[英]Memory release objective c

Point of my view, when we create object and then set it = nil, this object will be release. 我的观点是,当我们创建对象然后将其设置为nil时,将释放该对象。 but I tried to insert a lot of data to NSMutableDictionary, and then i set NIL for each object. 但是我尝试向NSMutableDictionary插入很多数据,然后为每个对象设置NIL。 Memory still be there and did not decrease. 内存仍然存在,并且没有减少。 Please see my code: 请查看我的代码:

- (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;

}

Memory when i run it is 218 mb. 我运行时的内存为218 MB。 and it did not decrease. 并没有减少。 Someone can help me and give me solution? 有人可以帮助我并给我解决方案吗? Thank so muuch 非常感谢

If you are using ARC, setting local variables to nil is unnecessary and will have no effect. 如果使用的是ARC,则不必将局部变量设置为nil ,并且不会起作用。 Local variables are completely memory-managed for you. 局部变量完全由您管理。 And setting a variable to nil does not , as you imagine, cause the memory itself to be cleared; 就像您想象的那样 ,将变量设置为nil不会导致内存本身被清除。 it reduces the object's retain count, but that's all. 它减少了对象的保留计数,仅此而已。 If the object's retain count has fallen to zero, then the memory may be reclaimed at some future time, but you have no control over when that will happen. 如果对象的保留计数已降至零,则可能会在将来的某个时间回收内存,但是您无法控制何时发生。 Thus, your nil setting does nothing whatever, because the same thing is being done for you by ARC anyway. 因此,您的nil设置不会执行任何操作,因为ARC总会为您完成相同的操作。

If you are concerned about accumulation of secondary autoreleased objects during a tight loop (as here), simply wrap the interior of the loop in an @autoreleasepool block. 如果您担心紧密循环中次级自动释放对象的积累(如此处所示),只需将循环内部包装在@autoreleasepool块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM