際際滷

際際滷Share a Scribd company logo
Qu畉n l箪 b畛 nh畛 khi
l畉p tr狸nh Apple iOS
     Tr畛nh Minh C動畛ng
    http://techmaster.vn

                           http://techmaster.vn
Bi tr狸nh by ny t畉p trung vo v畉n
畛 k畛 thu畉t trong ph叩t tri畛n native
 app cho Apple iPhone/iPod/iPad.

   Dnh ri棚ng cho l畉p tr狸nh vi棚n
                              http://techmaster.vn
Android (Java)  Windows Phone (C#)


 B畛 nh畛 動畛c c畉p         Kh担ng c坦 bi畉n tr畛
                                                         Garbage Collector
ph叩t khi c坦 nhu c畉u     畉n, ho畉c bi畉n 達
                                                         s畉 thu h畛i b畛 nh畛
      s畛 d畛ng           h畉t h畉n trong stack




                 ObjY
    P2
    P1           ObjX




   Stack         Heap   http://www.simple-talk.com/dotnet/.net-
                        framework/understanding-garbage-collection-in-.net/
                                                                  http://techmaster.vn
iOS kh叩c v畛i Android, Windows Phone
 Native iOS app l畉p tr狸nh b畉ng Objective-C.



          C language       Smalltalk



                 Objective C


                                           http://techmaster.vn
iOS kh担ng thu h畛i b畛 nh畛 t畛 畛ng
          hi畛n 畉i nh動 Android hay .NET



                                         Qu畉n l箪 b畛 nh畛 khi l畉p
                                         tr狸nh l c担ng vi畛c nhm
                                           ch叩n nh動ng v畉n c畉n
                                          lm 炭ng ngay t畛 畉u


B鱈t n動畛c r嘆 r畛 t畉i 畉p th畛y i畛n s担ng Gianh               http://techmaster.vn
Pointer  Con tr畛
 Pointer tr畛 畉n 畛i t動畛ng. N坦 gi炭p 畛i t動畛ng
  動畛c gi畛 l畉i trong heap.
 Khi 畛i t動畛ng kh担ng c嘆n b畉t k畛 ai tr畛 畉n, n坦
  c畉n 動畛c lo畉i b畛 畛 gi畉i ph坦ng b畛 nh畛.



            A
                          Object in heap

            B

                                           http://techmaster.vn
 Con tr畛 c坦 th畛 l bi畉n c畛c b畛 tr畛 t畛i 畛i t動畛ng 畛
  heap
 Ho畉c n坦 l thnh ph畉n c畛a 畛i t動畛ng, tr畛 畉n 畛i
  t動畛ng kh叩c



                 *p



                Stack          Heap

                                            http://techmaster.vn
[object release]
NSArray *array = [[NSArray alloc] initWithObjects:
@"Honda", @"Yamaha, nil];
NSLog(@Retain count: %d", [array retainCount]);
[array release];

                                RetainCount = 1
                      Honda
  array              Yamaha




   Stack             Heap
                                                     http://techmaster.vn
N畉u kh担ng release d畉n 畉n v湛ng nh畛 do
array tr動畛c 但y tr畛 t畛i tr畛 n棚n v担 ch畛. App
v畉n ch畉y nh動ng b畛 nh畛 b畛 r嘆 r畛, leaking.




                                      http://techmaster.vn
Product  Analyze 畛 ph但n t鱈ch ki畛m tra




                                  http://techmaster.vn
Product - Profile




                    http://techmaster.vn
LEAK !!!


                                    Th畛 ph畉m g但y ra Leak




N棚n s畛 d畛ng th動畛ng xuy棚n 畛i v畛i c叩c d畛 叩n Non ARC !
                                              http://techmaster.vn
NSArray *array = [[NSArray alloc] initWithObjects:
@"Honda", @"Yamaha, nil];
[array release];//C畉n ph畉i release




UIImage *aImage = [UIImage imageNamed:@A.png"];
[aImage release]; //Kh担ng c畉n ph畉i release. T畉I SAO?




                                                http://techmaster.vn
[UIImage imageNamed ] tr畉 v畛 畛i t動畛ng
autorelease (t畛 nh畉). Do 坦 kh担ng c畉n g畛i
message release 畉n 畛i t動畛ng ny n畛a.




                                     http://techmaster.vn
[object autorelease]
 D湛ng khi t畉o m畛t object trong hm r畛i tr畉
  v畛 qua return.

 [object autorelease] : object s畉 動畛c qu畉n
  l箪 trong AutoRelease pool

 L畉m d畛ng autorelease kh担ng t畛t. T畉i sao?


                                      http://techmaster.vn
K畉t th炭c m畛i chu k畛 x畛 l箪 s畛 ki畛n, CocoaTouch s畉
g畛i message release 畉n t畉t c畉 c叩c autorelease
object.




                                           http://techmaster.vn
RetainCount = 2


   RetainCount = 0


   RetainCount = 3



   RetainCount = 0


Autorelease pool
                     Heap


                        http://techmaster.vn
[object retain] d湛ng khi no?
NSArray *resultArray = [self returnAnArray];

NSArray *anotherArrayPointer = resultArray;




[resultArray release];

[anotherArrayPointer doSomething];

                  Bad memory access !d
                                               http://techmaster.vn
Tr動畛c khi chuy畛n con tr畛 畉n 畛i
t動畛ng kh叩c c畉n release 畛i t動畛ng c滴


              objX

                     [p release]
*p

              objY

              Heap             http://techmaster.vn
@property

Gi炭p 畛nh ngh挑a bi畉n thnh ph畉n
tinh t畉 h董n b畉ng c叩ch b畛 xung
th棚m attributes

                             http://techmaster.vn
Attribute                          ngh挑a

             Kh担ng h畛 tr畛 truy xu畉t 畛ng b畛 nh動ng s畉
nonatomic
             nhanh h董n
retain       Tng ref count c畛a 畛i t動畛ng th棚m 1

             Dnh ri棚ng cho ki畛u cn b畉n (primitive data
assign       type), struct ho畉c 畛i t動畛ng n畉u kh担ng tng
             ref count
readonly     Ch畛 畛c, t畉o duy nh畉t getter

readwrite    C畉 畛c ghi. T畉o c畉 getter v setter

             Copy 畛i t動畛ng g叩n sang v湛ng nh畛 m畛i. Ch畛
copy
             c坦 t叩c d畛ng v畛i ki畛u MutableXXX   http://techmaster.vn
*p1




Dead Lock in memory



          *p2

                      http://techmaster.vn
 Pointer trong Objective-C m畉c 畛nh l
  strong
 Cyclic reference g但y n棚n dead lock. 畛i
  t動畛ng trong dead lock kh担ng th畛 gi畉i
  ph坦ng, g但y th畉t tho叩t b畛 nh畛
 C叩ch x畛 l箪, s畛 d畛ng __weak


@property (nonatomic, assign)   __weak   A *pA;
                                            http://techmaster.vn
ARC saves you from
mem leak or crash !



      ARC

               http://techmaster.vn
T鱈nh nng qu畉n l箪 b畛 nh畛 trong iOS SDK

iOS 1.2               iOS 4.0   iOS 5.0           iOS 5.1       iOS 6.0
                                          5.0.1


3/2008                6/2010    10/2011           3/2012       6/2012



 Tr動畛c iOS 5: Manual Retain / Release (Gi畛 - nh畉
  th畛 c担ng 畛i t動畛ng 畛 heap)

 Sau iOS 5: Automatic Reference Counting (T畛
  畛ng 畉m con tr畛 tr畛 畉n 畛i t動畛ng 畛 heap)
                                                            http://techmaster.vn
ARC
 LTV kh担ng d湛ng
 retain, release, autorelease, dealloc th畛 c担ng
 n畛a.
 Tr狸nh bi棚n d畛ch t畛 畛ng ch竪n
 retain, release, autorelease, dealloc khi c畉n.

 Th棚m m畛t s畛 t鱈nh nng m畛i:
  strong, weak

                                         http://techmaster.vn
ARC
 Kh担ng ph畉i l c董 ch畉 qu畉n l箪 b畛 nh畛 hon
  ton m畛i
 Kh担ng l Garbage Collector nh動 nhi畛u
  ng動畛i hi畛u nh畉m
 B畛 nh畛 v畉n c坦 th畛 th畉t tho叩t, 畛ng d畛ng 畛
  v畛 n畉u d湛ng sai
 Gi畉i ph坦ng b畛 nh畛 kh担ng tinh t畉 nh動 JVM
  hay .NET runtime, nh動ng r畉t ph湛 h畛p thi畉t
  b畛 smartphone                      http://techmaster.vn
strong vs weak
 M畉c 畛nh con tr畛 l strong

__weak NSString *weakP = textField.text;

      @Mobile Day              @Mobile Day
         2011                     2012


NIL

         *weakP                textField.text
                                                http://techmaster.vn
Kinh nghi畛m
 D湛ng strong pointer 畛 gi畛 畛i
  t動畛ng m狸nh t畉o ra.

 Kh担ng d湛ng strong pointer 畛 gi畛
  畛i t動畛ng kh担ng th畛c s畛 c畛a
  m狸nh  kh担ng th畛 gi畉i ph坦ng b畛
  nh畛. D湛ng weak pointer l 畛.
                              http://techmaster.vn
 G叩n strong pointer = nil 畛 hm viewDidUnload 畛
  iOS gi畉i ph坦ng 畛i t動畛ng kh担ng c畉n d湛ng n畛a.
- (void)viewDidUnload
{
   [super viewDidUnload];
   myString = nil;
   myImage = nil;
}

 Kh担ng nh畉t thi畉t ph畉i d湛ng strong 畛 tr畛 t畛i
  ph畉n t畛 giao di畛n trong XIB.

@property (nonatomic, weak) IBOutlet UIButton *myButton;

                                                http://techmaster.vn
???



      http://techmaster.vn
ARC c畉n IOS SDK 5.x nh動ng h畛 tr畛 iOS SDK
                   4.x

 LTV b畉t bu畛c ph畉i d湛ng iOS SDK 5.x 畛 b畉t ARC
  nh動ng c坦 th畛 ch畛n deployment target xu畛ng iOS
  4.x.

 L動u 箪 iOS 4.x kh担ng h畛 tr畛 automatic weak
  reference zeroing.



                                          http://techmaster.vn
Tr棚n menu c畛a XCode, ch畛n
Edit -> Refactor -> Convert to Objective-C ARC




                                        http://techmaster.vn
bridge trong ARC
__bridge_transfer: cast 畛i t動畛ng mi畛n CFObject
sang mi畛n NSObject sau 坦 release CFObject

__bridge_retained: cast 畛i t動畛ng mi畛n
NSObject sang CFObject nh動ng gi畛 畛i t動畛ng
c滴.

__bridge: chuy畛n 畛i qua l畉i 畛i t動畛ng gi畛a mi畛n
CFObject v NSObject, kh担ng quan t但m reference
counter.
                                         http://techmaster.vn
畛ng d畛ng ARC v畉n c坦 th畛 xi m達 nonARC

 M畛t s畛 th動 vi畛n kh担ng d湛ng ARC nh動
  ASIHttpRequest, AFNetworking,




 Lm th畉 no 畛 d湛ng ch炭ng trong d畛
  叩n b畉t ARC ?

                                    http://techmaster.vn
B畉t compiler flags fno-objc-arc l棚n !
                                 http://techmaster.vn
C嘆n nhi畛u v畉n 畛 n畛a..


 Truy v畉n bi畉n trong ph畉m vi block
 X畛 l箪 khi b畛 nh畛 d湛ng c畉n
 Lazy Loading
 Qu畉n l箪 b畛 nh畛 khi l畉p tr狸nh 畛ng b畛
 Chuy畛n 畛i v湛ng nh畛 C++ v畛i Objective_C

                                         http://techmaster.vn
Sao ph畛c t畉p th畉
                 !!!?




畛ng lo l畉ng, c畛 l畉p tr狸nh
i r畛i s畉 quen !
                               http://techmaster.vn
Xin ch但n thnh c畉m 董n c叩c b畉n 達
          ch炭 箪 l畉ng ngh辿




                            http://techmaster.vn

More Related Content

Similar to Vietnam mobile day 2012 i os memory management - techmaster (20)

Beezo Share - Donetnuke Deserialize
Beezo Share - Donetnuke DeserializeBeezo Share - Donetnuke Deserialize
Beezo Share - Donetnuke Deserialize
Beezo
Arrowjs.io
Arrowjs.ioArrowjs.io
Arrowjs.io
TechMaster Vietnam
ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015
ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015
ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015
IT Expert Club
gioi thieu ve java
gioi thieu ve javagioi thieu ve java
gioi thieu ve java
Dat Ngo
Gioi thieu ve_java
Gioi thieu ve_javaGioi thieu ve_java
Gioi thieu ve_java
V滴 Trung 担ng
Node.js cn b畉n
Node.js cn b畉nNode.js cn b畉n
Node.js cn b畉n
TechMaster Vietnam
Gi畛i thi畛u Nuxt.js
Gi畛i thi畛u Nuxt.jsGi畛i thi畛u Nuxt.js
Gi畛i thi畛u Nuxt.js
Robin Huy
0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw
0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw
0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw
phanthanhthuan039914
L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛
L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛
L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛
Tr畉n Thi棚n 畉i
Gi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚n
Gi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚nGi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚n
Gi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚n
Tr畉n Thi棚n 畉i
Gt java080102005
Gt java080102005Gt java080102005
Gt java080102005
Ton Tr畉n M畉nh
Kh坦a h畛c l畉p tr狸nh ios
Kh坦a h畛c l畉p tr狸nh iosKh坦a h畛c l畉p tr狸nh ios
Kh坦a h畛c l畉p tr狸nh ios
o t畉o Seo
Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)
Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)
Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)
Duy L棚 Vn
VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...
VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...
VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...
Thanh Nguyen
Php crawler and parser
Php crawler and parserPhp crawler and parser
Php crawler and parser
Ngoc Bui Dinh
C畉p ph叩t b畛 nh畛 畛ng trong C
C畉p ph叩t b畛 nh畛 畛ng trong CC畉p ph叩t b畛 nh畛 畛ng trong C
C畉p ph叩t b畛 nh畛 畛ng trong C
Iam Me
Th棚m s畛a-x坦a-combobox - c#
Th棚m s畛a-x坦a-combobox - c#Th棚m s畛a-x坦a-combobox - c#
Th棚m s畛a-x坦a-combobox - c#
Vn D滴ng
L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...
L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...
L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...
VKhang Yang
BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019
BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019
BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019
TiLiu5
Beezo Share - Donetnuke Deserialize
Beezo Share - Donetnuke DeserializeBeezo Share - Donetnuke Deserialize
Beezo Share - Donetnuke Deserialize
Beezo
ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015
ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015
ITLC - Hanoi - NodeJS - ArrowJS - 27-11 - 2015
IT Expert Club
gioi thieu ve java
gioi thieu ve javagioi thieu ve java
gioi thieu ve java
Dat Ngo
Gi畛i thi畛u Nuxt.js
Gi畛i thi畛u Nuxt.jsGi畛i thi畛u Nuxt.js
Gi畛i thi畛u Nuxt.js
Robin Huy
0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw
0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw
0-Gioi thieu.pdfqwdqwwwwwwwwwwwwwwwwdqwdqw
phanthanhthuan039914
L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛
L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛
L畉p tr狸nh C# 2008 c董 b畉n_Nh畉t Ngh畛
Tr畉n Thi棚n 畉i
Gi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚n
Gi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚nGi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚n
Gi叩o tr狸nh java 畉i H畛c S動 Ph畉m K畛 Thu畉t H動ng Y棚n
Tr畉n Thi棚n 畉i
Kh坦a h畛c l畉p tr狸nh ios
Kh坦a h畛c l畉p tr狸nh iosKh坦a h畛c l畉p tr狸nh ios
Kh坦a h畛c l畉p tr狸nh ios
o t畉o Seo
Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)
Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)
Ebook l畉p tr狸nh Java h動畛ng 畛i t動畛ng (Ti畉ng vi畛t)
Duy L棚 Vn
VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...
VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...
VMD2017 - S畛 d畛ng React Native cho m担i tr動畛ng Production: kh畉 thi 畉n 但u? kh...
Thanh Nguyen
Php crawler and parser
Php crawler and parserPhp crawler and parser
Php crawler and parser
Ngoc Bui Dinh
C畉p ph叩t b畛 nh畛 畛ng trong C
C畉p ph叩t b畛 nh畛 畛ng trong CC畉p ph叩t b畛 nh畛 畛ng trong C
C畉p ph叩t b畛 nh畛 畛ng trong C
Iam Me
Th棚m s畛a-x坦a-combobox - c#
Th棚m s畛a-x坦a-combobox - c#Th棚m s畛a-x坦a-combobox - c#
Th棚m s畛a-x坦a-combobox - c#
Vn D滴ng
L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...
L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...
L畉p tr狸nh background job b畉ng azurequeue v webjob s畛 d畛ng azure storage emul...
VKhang Yang
BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019
BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019
BI TH NGHI畛M S畛 XY D畛NG HIBERNATE CHO 畛NG D畛NG JAVA_10441312092019
TiLiu5

More from Quang Anh Le (20)

Mobilemonday.vn mobile interaction - mr ng cuu long goldsun focus media
Mobilemonday.vn   mobile interaction - mr ng cuu long goldsun focus mediaMobilemonday.vn   mobile interaction - mr ng cuu long goldsun focus media
Mobilemonday.vn mobile interaction - mr ng cuu long goldsun focus media
Quang Anh Le
Mobilemonday.vn adflex
Mobilemonday.vn   adflexMobilemonday.vn   adflex
Mobilemonday.vn adflex
Quang Anh Le
Mobilemonday.vn momo-monetize thinking_tu do tai chinh_v1.2
Mobilemonday.vn   momo-monetize thinking_tu do tai chinh_v1.2Mobilemonday.vn   momo-monetize thinking_tu do tai chinh_v1.2
Mobilemonday.vn momo-monetize thinking_tu do tai chinh_v1.2
Quang Anh Le
02 mobile monday hanoi april 2013 vinova
02 mobile monday hanoi april 2013   vinova02 mobile monday hanoi april 2013   vinova
02 mobile monday hanoi april 2013 vinova
Quang Anh Le
01 mobile monday hanoi april 2013 mobile game - afoli
01 mobile monday hanoi april 2013   mobile game - afoli01 mobile monday hanoi april 2013   mobile game - afoli
01 mobile monday hanoi april 2013 mobile game - afoli
Quang Anh Le
03 mobile monday hanoi april 2013 monetize thinking g mo m-work
03 mobile monday hanoi april 2013 monetize thinking g mo   m-work03 mobile monday hanoi april 2013 monetize thinking g mo   m-work
03 mobile monday hanoi april 2013 monetize thinking g mo m-work
Quang Anh Le
2. minh chau momo t4
2. minh chau momo t42. minh chau momo t4
2. minh chau momo t4
Quang Anh Le
0. mobilemonday.vn hcm 20130424
0. mobilemonday.vn hcm 201304240. mobilemonday.vn hcm 20130424
0. mobilemonday.vn hcm 20130424
Quang Anh Le
Vietnam mobile day 2012 phan mem cho phai dep - mana
Vietnam mobile day 2012   phan mem cho phai dep - manaVietnam mobile day 2012   phan mem cho phai dep - mana
Vietnam mobile day 2012 phan mem cho phai dep - mana
Quang Anh Le
Vietnam mobile day 2012 multi platform development - game loft
Vietnam mobile day 2012   multi platform development - game loftVietnam mobile day 2012   multi platform development - game loft
Vietnam mobile day 2012 multi platform development - game loft
Quang Anh Le
Vietnam mobile day 2012 building a mobile linux firmware for your router - ...
Vietnam mobile day 2012   building a mobile linux firmware for your router - ...Vietnam mobile day 2012   building a mobile linux firmware for your router - ...
Vietnam mobile day 2012 building a mobile linux firmware for your router - ...
Quang Anh Le
Vietnam mobile day 2012 ban quyen mobile contents - vsm
Vietnam mobile day 2012   ban quyen mobile contents - vsmVietnam mobile day 2012   ban quyen mobile contents - vsm
Vietnam mobile day 2012 ban quyen mobile contents - vsm
Quang Anh Le
Vietnam mobile day 2012 platform mo va ung dung de phat trien mang xa hoi m...
Vietnam mobile day 2012   platform mo va ung dung de phat trien mang xa hoi m...Vietnam mobile day 2012   platform mo va ung dung de phat trien mang xa hoi m...
Vietnam mobile day 2012 platform mo va ung dung de phat trien mang xa hoi m...
Quang Anh Le
Vietnam mobile day 2012 co hoi mobile tai vietnam - cyber agent
Vietnam mobile day 2012   co hoi mobile tai vietnam - cyber agentVietnam mobile day 2012   co hoi mobile tai vietnam - cyber agent
Vietnam mobile day 2012 co hoi mobile tai vietnam - cyber agent
Quang Anh Le
Vietnam mobile day 2012 html5 game using impact js - aiti-aptech
Vietnam mobile day 2012   html5 game using impact js - aiti-aptechVietnam mobile day 2012   html5 game using impact js - aiti-aptech
Vietnam mobile day 2012 html5 game using impact js - aiti-aptech
Quang Anh Le
Vietnam mobile day 2012 tuong lai cho nha phat trien nds - share plus
Vietnam mobile day 2012   tuong lai cho nha phat trien nds - share plusVietnam mobile day 2012   tuong lai cho nha phat trien nds - share plus
Vietnam mobile day 2012 tuong lai cho nha phat trien nds - share plus
Quang Anh Le
Vietnam mobile day 2012 trends in mobile marketing - fifth i media
Vietnam mobile day 2012   trends in mobile marketing - fifth i mediaVietnam mobile day 2012   trends in mobile marketing - fifth i media
Vietnam mobile day 2012 trends in mobile marketing - fifth i media
Quang Anh Le
Vietnam mobile day 2012 thi hieu nguoi dung mobile - app storevn
Vietnam mobile day 2012   thi hieu nguoi dung mobile - app storevnVietnam mobile day 2012   thi hieu nguoi dung mobile - app storevn
Vietnam mobile day 2012 thi hieu nguoi dung mobile - app storevn
Quang Anh Le
Vietnam mobile day 2012 new platform - new opportunity - appota
Vietnam mobile day 2012   new platform - new opportunity - appotaVietnam mobile day 2012   new platform - new opportunity - appota
Vietnam mobile day 2012 new platform - new opportunity - appota
Quang Anh Le
Vietnam mobile day 2012 vietnam mobile vas market review - vdec
Vietnam mobile day 2012   vietnam mobile vas market review - vdecVietnam mobile day 2012   vietnam mobile vas market review - vdec
Vietnam mobile day 2012 vietnam mobile vas market review - vdec
Quang Anh Le
Mobilemonday.vn mobile interaction - mr ng cuu long goldsun focus media
Mobilemonday.vn   mobile interaction - mr ng cuu long goldsun focus mediaMobilemonday.vn   mobile interaction - mr ng cuu long goldsun focus media
Mobilemonday.vn mobile interaction - mr ng cuu long goldsun focus media
Quang Anh Le
Mobilemonday.vn adflex
Mobilemonday.vn   adflexMobilemonday.vn   adflex
Mobilemonday.vn adflex
Quang Anh Le
Mobilemonday.vn momo-monetize thinking_tu do tai chinh_v1.2
Mobilemonday.vn   momo-monetize thinking_tu do tai chinh_v1.2Mobilemonday.vn   momo-monetize thinking_tu do tai chinh_v1.2
Mobilemonday.vn momo-monetize thinking_tu do tai chinh_v1.2
Quang Anh Le
02 mobile monday hanoi april 2013 vinova
02 mobile monday hanoi april 2013   vinova02 mobile monday hanoi april 2013   vinova
02 mobile monday hanoi april 2013 vinova
Quang Anh Le
01 mobile monday hanoi april 2013 mobile game - afoli
01 mobile monday hanoi april 2013   mobile game - afoli01 mobile monday hanoi april 2013   mobile game - afoli
01 mobile monday hanoi april 2013 mobile game - afoli
Quang Anh Le
03 mobile monday hanoi april 2013 monetize thinking g mo m-work
03 mobile monday hanoi april 2013 monetize thinking g mo   m-work03 mobile monday hanoi april 2013 monetize thinking g mo   m-work
03 mobile monday hanoi april 2013 monetize thinking g mo m-work
Quang Anh Le
2. minh chau momo t4
2. minh chau momo t42. minh chau momo t4
2. minh chau momo t4
Quang Anh Le
0. mobilemonday.vn hcm 20130424
0. mobilemonday.vn hcm 201304240. mobilemonday.vn hcm 20130424
0. mobilemonday.vn hcm 20130424
Quang Anh Le
Vietnam mobile day 2012 phan mem cho phai dep - mana
Vietnam mobile day 2012   phan mem cho phai dep - manaVietnam mobile day 2012   phan mem cho phai dep - mana
Vietnam mobile day 2012 phan mem cho phai dep - mana
Quang Anh Le
Vietnam mobile day 2012 multi platform development - game loft
Vietnam mobile day 2012   multi platform development - game loftVietnam mobile day 2012   multi platform development - game loft
Vietnam mobile day 2012 multi platform development - game loft
Quang Anh Le
Vietnam mobile day 2012 building a mobile linux firmware for your router - ...
Vietnam mobile day 2012   building a mobile linux firmware for your router - ...Vietnam mobile day 2012   building a mobile linux firmware for your router - ...
Vietnam mobile day 2012 building a mobile linux firmware for your router - ...
Quang Anh Le
Vietnam mobile day 2012 ban quyen mobile contents - vsm
Vietnam mobile day 2012   ban quyen mobile contents - vsmVietnam mobile day 2012   ban quyen mobile contents - vsm
Vietnam mobile day 2012 ban quyen mobile contents - vsm
Quang Anh Le
Vietnam mobile day 2012 platform mo va ung dung de phat trien mang xa hoi m...
Vietnam mobile day 2012   platform mo va ung dung de phat trien mang xa hoi m...Vietnam mobile day 2012   platform mo va ung dung de phat trien mang xa hoi m...
Vietnam mobile day 2012 platform mo va ung dung de phat trien mang xa hoi m...
Quang Anh Le
Vietnam mobile day 2012 co hoi mobile tai vietnam - cyber agent
Vietnam mobile day 2012   co hoi mobile tai vietnam - cyber agentVietnam mobile day 2012   co hoi mobile tai vietnam - cyber agent
Vietnam mobile day 2012 co hoi mobile tai vietnam - cyber agent
Quang Anh Le
Vietnam mobile day 2012 html5 game using impact js - aiti-aptech
Vietnam mobile day 2012   html5 game using impact js - aiti-aptechVietnam mobile day 2012   html5 game using impact js - aiti-aptech
Vietnam mobile day 2012 html5 game using impact js - aiti-aptech
Quang Anh Le
Vietnam mobile day 2012 tuong lai cho nha phat trien nds - share plus
Vietnam mobile day 2012   tuong lai cho nha phat trien nds - share plusVietnam mobile day 2012   tuong lai cho nha phat trien nds - share plus
Vietnam mobile day 2012 tuong lai cho nha phat trien nds - share plus
Quang Anh Le
Vietnam mobile day 2012 trends in mobile marketing - fifth i media
Vietnam mobile day 2012   trends in mobile marketing - fifth i mediaVietnam mobile day 2012   trends in mobile marketing - fifth i media
Vietnam mobile day 2012 trends in mobile marketing - fifth i media
Quang Anh Le
Vietnam mobile day 2012 thi hieu nguoi dung mobile - app storevn
Vietnam mobile day 2012   thi hieu nguoi dung mobile - app storevnVietnam mobile day 2012   thi hieu nguoi dung mobile - app storevn
Vietnam mobile day 2012 thi hieu nguoi dung mobile - app storevn
Quang Anh Le
Vietnam mobile day 2012 new platform - new opportunity - appota
Vietnam mobile day 2012   new platform - new opportunity - appotaVietnam mobile day 2012   new platform - new opportunity - appota
Vietnam mobile day 2012 new platform - new opportunity - appota
Quang Anh Le
Vietnam mobile day 2012 vietnam mobile vas market review - vdec
Vietnam mobile day 2012   vietnam mobile vas market review - vdecVietnam mobile day 2012   vietnam mobile vas market review - vdec
Vietnam mobile day 2012 vietnam mobile vas market review - vdec
Quang Anh Le

Vietnam mobile day 2012 i os memory management - techmaster

  • 1. Qu畉n l箪 b畛 nh畛 khi l畉p tr狸nh Apple iOS Tr畛nh Minh C動畛ng http://techmaster.vn http://techmaster.vn
  • 2. Bi tr狸nh by ny t畉p trung vo v畉n 畛 k畛 thu畉t trong ph叩t tri畛n native app cho Apple iPhone/iPod/iPad. Dnh ri棚ng cho l畉p tr狸nh vi棚n http://techmaster.vn
  • 3. Android (Java) Windows Phone (C#) B畛 nh畛 動畛c c畉p Kh担ng c坦 bi畉n tr畛 Garbage Collector ph叩t khi c坦 nhu c畉u 畉n, ho畉c bi畉n 達 s畉 thu h畛i b畛 nh畛 s畛 d畛ng h畉t h畉n trong stack ObjY P2 P1 ObjX Stack Heap http://www.simple-talk.com/dotnet/.net- framework/understanding-garbage-collection-in-.net/ http://techmaster.vn
  • 4. iOS kh叩c v畛i Android, Windows Phone Native iOS app l畉p tr狸nh b畉ng Objective-C. C language Smalltalk Objective C http://techmaster.vn
  • 5. iOS kh担ng thu h畛i b畛 nh畛 t畛 畛ng hi畛n 畉i nh動 Android hay .NET Qu畉n l箪 b畛 nh畛 khi l畉p tr狸nh l c担ng vi畛c nhm ch叩n nh動ng v畉n c畉n lm 炭ng ngay t畛 畉u B鱈t n動畛c r嘆 r畛 t畉i 畉p th畛y i畛n s担ng Gianh http://techmaster.vn
  • 6. Pointer Con tr畛 Pointer tr畛 畉n 畛i t動畛ng. N坦 gi炭p 畛i t動畛ng 動畛c gi畛 l畉i trong heap. Khi 畛i t動畛ng kh担ng c嘆n b畉t k畛 ai tr畛 畉n, n坦 c畉n 動畛c lo畉i b畛 畛 gi畉i ph坦ng b畛 nh畛. A Object in heap B http://techmaster.vn
  • 7. Con tr畛 c坦 th畛 l bi畉n c畛c b畛 tr畛 t畛i 畛i t動畛ng 畛 heap Ho畉c n坦 l thnh ph畉n c畛a 畛i t動畛ng, tr畛 畉n 畛i t動畛ng kh叩c *p Stack Heap http://techmaster.vn
  • 8. [object release] NSArray *array = [[NSArray alloc] initWithObjects: @"Honda", @"Yamaha, nil]; NSLog(@Retain count: %d", [array retainCount]); [array release]; RetainCount = 1 Honda array Yamaha Stack Heap http://techmaster.vn
  • 9. N畉u kh担ng release d畉n 畉n v湛ng nh畛 do array tr動畛c 但y tr畛 t畛i tr畛 n棚n v担 ch畛. App v畉n ch畉y nh動ng b畛 nh畛 b畛 r嘆 r畛, leaking. http://techmaster.vn
  • 10. Product Analyze 畛 ph但n t鱈ch ki畛m tra http://techmaster.vn
  • 11. Product - Profile http://techmaster.vn
  • 12. LEAK !!! Th畛 ph畉m g但y ra Leak N棚n s畛 d畛ng th動畛ng xuy棚n 畛i v畛i c叩c d畛 叩n Non ARC ! http://techmaster.vn
  • 13. NSArray *array = [[NSArray alloc] initWithObjects: @"Honda", @"Yamaha, nil]; [array release];//C畉n ph畉i release UIImage *aImage = [UIImage imageNamed:@A.png"]; [aImage release]; //Kh担ng c畉n ph畉i release. T畉I SAO? http://techmaster.vn
  • 14. [UIImage imageNamed ] tr畉 v畛 畛i t動畛ng autorelease (t畛 nh畉). Do 坦 kh担ng c畉n g畛i message release 畉n 畛i t動畛ng ny n畛a. http://techmaster.vn
  • 15. [object autorelease] D湛ng khi t畉o m畛t object trong hm r畛i tr畉 v畛 qua return. [object autorelease] : object s畉 動畛c qu畉n l箪 trong AutoRelease pool L畉m d畛ng autorelease kh担ng t畛t. T畉i sao? http://techmaster.vn
  • 16. K畉t th炭c m畛i chu k畛 x畛 l箪 s畛 ki畛n, CocoaTouch s畉 g畛i message release 畉n t畉t c畉 c叩c autorelease object. http://techmaster.vn
  • 17. RetainCount = 2 RetainCount = 0 RetainCount = 3 RetainCount = 0 Autorelease pool Heap http://techmaster.vn
  • 18. [object retain] d湛ng khi no? NSArray *resultArray = [self returnAnArray]; NSArray *anotherArrayPointer = resultArray; [resultArray release]; [anotherArrayPointer doSomething]; Bad memory access !d http://techmaster.vn
  • 19. Tr動畛c khi chuy畛n con tr畛 畉n 畛i t動畛ng kh叩c c畉n release 畛i t動畛ng c滴 objX [p release] *p objY Heap http://techmaster.vn
  • 20. @property Gi炭p 畛nh ngh挑a bi畉n thnh ph畉n tinh t畉 h董n b畉ng c叩ch b畛 xung th棚m attributes http://techmaster.vn
  • 21. Attribute ngh挑a Kh担ng h畛 tr畛 truy xu畉t 畛ng b畛 nh動ng s畉 nonatomic nhanh h董n retain Tng ref count c畛a 畛i t動畛ng th棚m 1 Dnh ri棚ng cho ki畛u cn b畉n (primitive data assign type), struct ho畉c 畛i t動畛ng n畉u kh担ng tng ref count readonly Ch畛 畛c, t畉o duy nh畉t getter readwrite C畉 畛c ghi. T畉o c畉 getter v setter Copy 畛i t動畛ng g叩n sang v湛ng nh畛 m畛i. Ch畛 copy c坦 t叩c d畛ng v畛i ki畛u MutableXXX http://techmaster.vn
  • 22. *p1 Dead Lock in memory *p2 http://techmaster.vn
  • 23. Pointer trong Objective-C m畉c 畛nh l strong Cyclic reference g但y n棚n dead lock. 畛i t動畛ng trong dead lock kh担ng th畛 gi畉i ph坦ng, g但y th畉t tho叩t b畛 nh畛 C叩ch x畛 l箪, s畛 d畛ng __weak @property (nonatomic, assign) __weak A *pA; http://techmaster.vn
  • 24. ARC saves you from mem leak or crash ! ARC http://techmaster.vn
  • 25. T鱈nh nng qu畉n l箪 b畛 nh畛 trong iOS SDK iOS 1.2 iOS 4.0 iOS 5.0 iOS 5.1 iOS 6.0 5.0.1 3/2008 6/2010 10/2011 3/2012 6/2012 Tr動畛c iOS 5: Manual Retain / Release (Gi畛 - nh畉 th畛 c担ng 畛i t動畛ng 畛 heap) Sau iOS 5: Automatic Reference Counting (T畛 畛ng 畉m con tr畛 tr畛 畉n 畛i t動畛ng 畛 heap) http://techmaster.vn
  • 26. ARC LTV kh担ng d湛ng retain, release, autorelease, dealloc th畛 c担ng n畛a. Tr狸nh bi棚n d畛ch t畛 畛ng ch竪n retain, release, autorelease, dealloc khi c畉n. Th棚m m畛t s畛 t鱈nh nng m畛i: strong, weak http://techmaster.vn
  • 27. ARC Kh担ng ph畉i l c董 ch畉 qu畉n l箪 b畛 nh畛 hon ton m畛i Kh担ng l Garbage Collector nh動 nhi畛u ng動畛i hi畛u nh畉m B畛 nh畛 v畉n c坦 th畛 th畉t tho叩t, 畛ng d畛ng 畛 v畛 n畉u d湛ng sai Gi畉i ph坦ng b畛 nh畛 kh担ng tinh t畉 nh動 JVM hay .NET runtime, nh動ng r畉t ph湛 h畛p thi畉t b畛 smartphone http://techmaster.vn
  • 28. strong vs weak M畉c 畛nh con tr畛 l strong __weak NSString *weakP = textField.text; @Mobile Day @Mobile Day 2011 2012 NIL *weakP textField.text http://techmaster.vn
  • 29. Kinh nghi畛m D湛ng strong pointer 畛 gi畛 畛i t動畛ng m狸nh t畉o ra. Kh担ng d湛ng strong pointer 畛 gi畛 畛i t動畛ng kh担ng th畛c s畛 c畛a m狸nh kh担ng th畛 gi畉i ph坦ng b畛 nh畛. D湛ng weak pointer l 畛. http://techmaster.vn
  • 30. G叩n strong pointer = nil 畛 hm viewDidUnload 畛 iOS gi畉i ph坦ng 畛i t動畛ng kh担ng c畉n d湛ng n畛a. - (void)viewDidUnload { [super viewDidUnload]; myString = nil; myImage = nil; } Kh担ng nh畉t thi畉t ph畉i d湛ng strong 畛 tr畛 t畛i ph畉n t畛 giao di畛n trong XIB. @property (nonatomic, weak) IBOutlet UIButton *myButton; http://techmaster.vn
  • 31. ??? http://techmaster.vn
  • 32. ARC c畉n IOS SDK 5.x nh動ng h畛 tr畛 iOS SDK 4.x LTV b畉t bu畛c ph畉i d湛ng iOS SDK 5.x 畛 b畉t ARC nh動ng c坦 th畛 ch畛n deployment target xu畛ng iOS 4.x. L動u 箪 iOS 4.x kh担ng h畛 tr畛 automatic weak reference zeroing. http://techmaster.vn
  • 33. Tr棚n menu c畛a XCode, ch畛n Edit -> Refactor -> Convert to Objective-C ARC http://techmaster.vn
  • 34. bridge trong ARC __bridge_transfer: cast 畛i t動畛ng mi畛n CFObject sang mi畛n NSObject sau 坦 release CFObject __bridge_retained: cast 畛i t動畛ng mi畛n NSObject sang CFObject nh動ng gi畛 畛i t動畛ng c滴. __bridge: chuy畛n 畛i qua l畉i 畛i t動畛ng gi畛a mi畛n CFObject v NSObject, kh担ng quan t但m reference counter. http://techmaster.vn
  • 35. 畛ng d畛ng ARC v畉n c坦 th畛 xi m達 nonARC M畛t s畛 th動 vi畛n kh担ng d湛ng ARC nh動 ASIHttpRequest, AFNetworking, Lm th畉 no 畛 d湛ng ch炭ng trong d畛 叩n b畉t ARC ? http://techmaster.vn
  • 36. B畉t compiler flags fno-objc-arc l棚n ! http://techmaster.vn
  • 37. C嘆n nhi畛u v畉n 畛 n畛a.. Truy v畉n bi畉n trong ph畉m vi block X畛 l箪 khi b畛 nh畛 d湛ng c畉n Lazy Loading Qu畉n l箪 b畛 nh畛 khi l畉p tr狸nh 畛ng b畛 Chuy畛n 畛i v湛ng nh畛 C++ v畛i Objective_C http://techmaster.vn
  • 38. Sao ph畛c t畉p th畉 !!!? 畛ng lo l畉ng, c畛 l畉p tr狸nh i r畛i s畉 quen ! http://techmaster.vn
  • 39. Xin ch但n thnh c畉m 董n c叩c b畉n 達 ch炭 箪 l畉ng ngh辿 http://techmaster.vn

Editor's Notes

  • #2: http://stackoverflow.com/questions/6991142/writable-atomic-property-result-cannot-pair-a-synthesized-setter-getter-with-a
  • #5: Objective-C is a thin layer on top of C, and moreover is a strict superset of C; it is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class.Objective-C derives its object syntax from Smalltalk. All of the syntax for non-object-oriented operations (including primitive variables, pre-processing, expressions, function declarations, and function calls) are identical to that of C, while the syntax for object-oriented features is an implementation of Smalltalk-style messaging.
  • #16: http://www.asgteach.com/blog/?p=73
  • #17: http://www.asgteach.com/blog/?p=73
  • #33: http://stackoverflow.com/questions/7747783/is-arc-really-supported-in-ios-4-the-ios-4-2-sdk-is-missing-arc-related-symbols