在ARC下的内存优化<一>

 

1.前言  

     本来以为在改成ARC以后,不再需要考虑内存问题了,可是在实践中还是发现有一些内存问题需要注意,今天我不谈block的循环引用的问题,主要说说一些对象、数组不内存得不到释放的情况.  

2.数组内存得不到释放的情况  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//组织字典数据
- (NSMutableDictionary *)setupDicData{
   
     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
     for ( int i = 0; i <= 30; i++) {
       
         [dict setObject:[self setupArrayData] forKey:[NSString stringWithFormat: @"%d%@" ,i, @"class" ]];
     }
     return dict;
}
 
//组织数组数据
- (NSMutableArray *)setupArrayData{
   
     NSMutableArray *marry = [NSMutableArray array];
   
     for ( int i = 0; i<=30; i++) {
       
         NSString *s = [NSString stringWithFormat: @"%@" , @"data-test" ];
       
         [marry addObject:s];
       
     }
     return marry;
}

 运行+——

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- ( void )viewDidLoad {
   
     [super viewDidLoad];
   
   
     while ( true ) {
       
         //30.0定时执行
       
         [NSThread sleepForTimeInterval:30.0];
       
         NSDictionary *dict = [self setupDicData];
       
         NSLog( @"%@" ,dict);
         //每次数据内存都得不到释放
 
     }
}

 

  //按上代码传递数组执行,每次数组、对象内存都得不到释放。如图:内存会无线的往上增加,直至崩溃。    

2.是什么原因导致这种内存得不到释放的?  

  主要是你在iOS里使用    while (true) {} 无线循环时, iOS ARC默认认为你这个方法永远没有执行完,所以不会去主动释放你方法里的对象,这一点和JAVA不一样, 所以很多JAVA开发者转iOS后习惯性的使用while(true){} 导致项目里存在这种内存隐患,导致内存无限增加。

 

3.如何解决这种数组传递内存得不到释放的情况?  

解决方法一:

3.1.最简单最直接在ARC的环境下使用        @autoreleasepool {}  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//@autoreleasepool {}的作用是在每次循环一次,都会把内存主动释放掉
 
 
- ( void )viewDidLoad {
    
     [super viewDidLoad];
    
    
     while ( true ) {
       
         @autoreleasepool {
             //30.0定时执行
            
             [NSThread sleepForTimeInterval:30.0];
            
             NSDictionary *dict = [self setupDicData];
            
             NSLog( @"%@" ,dict);
             //每次数据内存都得不到释放
         }
     }
}

 

 内存图,我们发现很稳定,每次都会主动将内存释放    解决方法二:

3.2.使用NSTimer来做数组传递的无限循环,ARC会自动帮你释放内存  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- ( void )usingDatadosomething{
   
     //30.0定时执行
   
     [NSThread sleepForTimeInterval:0.10];
   
     NSDictionary *dict = [self setupDicData];
   
     NSLog( @"%@" ,dict);
     //每次数据内存都得不到释放
 
}
 
 
 
- ( void )viewDidLoad {
   
     [super viewDidLoad];
   
     [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(usingDatadosomething) userInfo:self repeats:YES];
   
     [[NSRunLoop currentRunLoop] run];
   
}

 内存图如下

 

解决方法三:

3.3.使用block封装数组传递,最后做block的释放,ARC会自动帮你释放内存  

 

转载于:https://my.oschina.net/u/2607703/blog/678586

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值