Objective-C Memory Management - raywenderlich.com

  Переглядів 14,724

Kodeco

Kodeco

День тому

Even though we have ARC, understanding memory management is critical to writing effectives. This video covers the process.
----
About www.raywenderlich.com:
raywenderlich.com is a website focused on developing high quality programming tutorials. Our goal is to take the coolest and most challenging topics and make them easy for everyone to learn - so we can all make amazing apps.
We are also focused on developing a strong community. Our goal is to help each other reach our dreams through friendship and cooperation. As you can see below, a bunch of us have joined forces to make this happen: authors, editors, subject matter experts, app reviewers, and most importantly our amazing readers!
----
About Memory Management (from Apple)
developer.apple.com/library/c...
Application memory management is the process of allocating memory during your program’s runtime, using it, and freeing it when you are done with it. A well-written program uses as little memory as possible. In Objective-C, it can also be seen as a way of distributing ownership of limited memory resources among many pieces of data and code. When you have finished working through this guide, you will have the knowledge you need to manage your application’s memory by explicitly managing the life cycle of objects and freeing them when they are no longer needed.
Although memory management is typically considered at the level of an individual object, your goal is actually to manage object graphs. You want to make sure that you have no more objects in memory than you actually need.
Objective-C provides two methods of application memory management.
1. In the method described in this guide, referred to as “manual retain-release” or MRR, you explicitly manage memory by keeping track of objects you own. This is implemented using a model, known as reference counting, that the Foundation class NSObject provides in conjunction with the runtime environment.
2. In Automatic Reference Counting, or ARC, the system uses the same reference counting system as MRR, but it inserts the appropriate memory management method calls for you at compile-time. You are strongly encouraged to use ARC for new projects. If you use ARC, there is typically no need to understand the underlying implementation described in this document, although it may in some situations be helpful. For more about ARC, see Transitioning to ARC Release Notes.
Good Practices Prevent Memory-Related Problems
There are two main kinds of problem that result from incorrect memory management:
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 your application being terminated.
Thinking about memory management from the perspective of reference counting, however, is frequently counterproductive, because you tend to consider memory management in terms of the implementation details rather than in terms of your actual goals. Instead, you should think of memory management from the perspective of object ownership and object graphs.
Cocoa uses a straightforward naming convention to indicate when you own an object returned by a method.
Although the basic policy is straightforward, there are some practical steps you can take to make managing memory easier, and to help to ensure your program remains reliable and robust while at the same time minimizing its resource requirements.
Autorelease pool blocks provide a mechanism whereby you can send an object a “deferred” release message. This is useful in situations where you want to relinquish ownership of an object, but want to avoid the possibility of it being deallocated immediately (such as when you return an object from a method). There are occasions when you might use your own autorelease pool blocks.
About Objective C (from Wikipedia)
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It was the main programming language used by Apple for the OS X and iOS operating systems, and their respective application programming interfaces (APIs) Cocoa and Cocoa Touch prior to the introduction of Swift.
The programming language Objective-C was originally developed in the early 1980s. It was selected as the main language used by NeXT for its NeXTSTEP operating system, from which OS X and iOS are derived.[3] a

КОМЕНТАРІ: 10
@StevePlaysBanjo
@StevePlaysBanjo 6 років тому
This video has been a life-saver having inherited a legacy Objective-C project with no real Objective-C experience. All the contemporary training material seems to assume ARC is being used. Thank you!
@DadBodSwagGod
@DadBodSwagGod 6 років тому
Fantastic refresher! Thanks for this
@calvinchankf
@calvinchankf 7 років тому
Thanks so much for the video. And I would like to know memory management of Swift as well!!!
@arti5musicc
@arti5musicc 3 роки тому
swift always has arc
@Cdswjp
@Cdswjp Рік тому
Thank you for this
@Cdswjp
@Cdswjp Рік тому
I clicked this video by accident and decided to leave it on. It was a really nice refresher
@SMC247
@SMC247 6 років тому
Hmm, yet every legacy Objective C project I work with is full of memory leaks with CF data types etc (at least according to Xcode and Instruments)?
@adil-hussain-84
@adil-hussain-84 5 років тому
How come the NSString creation in the "quote" method calls autorelease but not the NSString creation in "description" method? See the 13-minute mark here: ukposts.info/have/v-deo/q12dq3-CiGd4mo0.html
@KodecoDev
@KodecoDev 5 років тому
The call in the description method is automatically autoreleased. When you create a new object with alloc, that object is not set to be autoreleased, so you have to call it. If you create a new object from a factory method, then that object will automatically be autoreleased. So creating a string with initWithFormat means you have to call autorelease. stringWithFormat is a factory method thus it is automatically set to autorelease. Mind you, if the method read, newStringWithFormat, you'd need to autorelease it since the new keyword indicates an unmanaged object. You can learn about the rules over here: developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
@adil-hussain-84
@adil-hussain-84 5 років тому
Thank you for the response. I'll check out the link which you posted.
Objective-C Memory Management with ARC - raywenderlich.com
7:21
Beginning Objective-C Classes and Methods - raywenderlich.com
15:26
Что будет с кроссовком?
00:35
Аришнев
Переглядів 2,4 млн
ISSEI funny story😂😂😂Strange World | Magic Lips💋
00:36
ISSEI / いっせい
Переглядів 43 млн
Артем Пивоваров х Klavdia Petrivna - Барабан
03:16
Artem Pivovarov
Переглядів 5 млн
Garbage Collection (Mark & Sweep) - Computerphile
16:22
Computerphile
Переглядів 230 тис.
Memory 2 - Finding and Fixing Memory Leaks (iOS, Xcode 9, Swift 4)
34:31
Mark Moeykens
Переглядів 49 тис.
Objective-C Blocks - raywenderlich.com
22:04
Kodeco
Переглядів 16 тис.
Objective C Tutorial
1:18:02
Derek Banas
Переглядів 325 тис.
Objective-C Protocol and Delegates- raywenderlich.com
10:15
Kodeco
Переглядів 24 тис.
Getting Started - Beginning Core Data - raywenderlich.com
10:47