Memory Management in iOS

Introduction

Memory management in iOS was initially non ARC(Automatic reference counting) where we have to retain and release the objects. Now it supports ARC where we don't need to add retain and release. Actually the Xcode takes care of the job automatically in compile time.

Problems faced

The two major problems faced as per apple documentation are,

  • Freeing or overwriting data that is still in use. This causes memory corruption, and typically results in your application crashing, or worse, corrupted user data.
  • Not freeing data that is no longer in use causes memory leaks. A memory leak is where allocated memory is not freed, even though it is never used again. Leaks cause your application to use ever-increasing amounts of memory, which in turn may result in poor system performance or (in iOS) your application being terminated.

Memory Management rules

  • We own the objects we create and we have to subsequently release them when they are no longer needed.
  • Retain can be used t gain ownership of an object that we did not create. We have release these objects too when it's not needed.
  • Don't release the objects that we don't own.

Handling memory in ARC

You don't need to use release and retain in ARC. So, all the view controller's objects will be released when the view controller is removed. Similarly any objects sub objects will be released when they are released. Remember if other classes have strong reference to an object of the class then the whole class won't be released. So it is recommended to use weak properties for delegates.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值