This workshop will be retired on May 31, 2020.
Introduction to Memory Management7:40 with Gabe Nadel
Memory Management has changed drastically since iOS first hit the market. Learn about the history of iOS Memory Management and how it fits into the broader subject of memory management in general.
Hello again. 0:04 Depending on where you are on your journey as a programmer and what iOS projects 0:05 you've been exposed to, your perception of what memory management is and 0:09 it's implication in building apps may vary wildly. 0:13 Different languages handle it differently and 0:17 even within iOS there have been major shifts in the past five years. 0:19 Don't let this worry you however, as things in iOS have become simpler and 0:23 simpler and less error prone over the years. 0:26 That said, because developers need to be able to work with legacy code and 0:30 interface with other languages. 0:34 Let's begin with a really high level overview of memory management and 0:35 then we'll focus on the history of iOS. 0:39 Then in subsequent videos, we'll explore the major topics to give you a good lay of 0:42 the land, point out the common pitfalls, and most importantly, signal when and 0:46 where you might need to dive deeper on your own projects. 0:51 So let's talk for a minute about why memory management is important. 0:55 Well in the most extreme case your code can be written such that you create severe 0:59 memory leaks. 1:03 That is memory which you allocate but 1:04 don't de-allocate when you should and that could quickly crash your app. 1:06 In a more insidious case, you might be creating smaller leaks or 1:10 just not being frugal with your memory. 1:13 In which case your performance might decrease as a user's session goes on, or 1:15 problems might just show up intermittently. 1:20 As our friends in Cupertino would tell you, performance is a feature, and 1:22 like many vital features and services, it only really gets noticed in its absence. 1:27 I don't know anyone who goes around talking about how much they love 1:32 electricity, but 1:34 everyone's world gets flipped upside down when there's a blackout. 1:35 Similarly, when I scroll through my iPhone and 1:39 quickly tap between apps, I'm not really conscious of the speed and responsiveness. 1:42 But when something hiccups, it sticks out like a sore thumb and 1:46 it can ruin my experience. 1:49 Long story short, just because your app isn't crashing, 1:51 doesn't mean you don't have a serious memory management problem. 1:54 Moreover, your job is to proactively make sure the performance is humming. 1:58 Nobody wants the QA department or, far worse yet, 2:02 the client, to be the one to tell you that your app is performing poorly. 2:05 So before we get into the details of iOS memory management, 2:10 let's take a second to talk about memory management in general. 2:13 We just discussed why it's important, but 2:16 what's actually happening in our device's memory? 2:18 Computer memory, which includes mobile phones and all sorts of 2:22 computers that don't just sit on desks, of course, is divided up into chunks. 2:25 As we create things like ints, strings, arrays, structs, and 2:29 objects, we allocate memory to them. 2:32 An int or a float would take just a small chunk of memory, but 2:35 an object, especially a complex one, might take up a larger chunk, that's all fine. 2:38 But when our code executes, 2:43 very often we create lots of things that we only need temporarily. 2:44 If we don't free up that memory when we're done using it, we can quickly run out. 2:48 So how do we solve this problem? 2:53 Broadly speaking, we employ memory management, as you probably guessed. 2:55 There are a few different ways developers have come up with over the years. 2:59 At the highest level, you can imagine the world of memory management 3:03 divided into manual memory management and garbage collection. 3:06 Manual memory management is a system whereby the developer gives manual 3:10 instructions as to when memory should be allocated, retained and released. 3:14 When iOS was first launched, it made use of a type of manual memory management 3:19 called manual-retain-release, or MMR. 3:23 And we'll talk more about that in a moment. 3:27 Broadly speaking garbage collection is a system which automates many if not 3:29 all of the memory tasks for you. 3:33 And in doing so, goes around cleaning up after your code as it executes, 3:35 releasing unneeded memory for new purposes. 3:39 Now, within in the world of garbage collection we have a few major flavors. 3:42 One is called trace garbage collection, which is so 3:46 common it's often just called garbage collection. 3:50 Java and .NET are two well known examples that make use of garbage collectors. 3:52 If you'd like more information about garbage collection and 3:57 to see a great visualization about different garbage collection algorithms, 4:00 check the link below. 4:04 As iOS developers, we'll be primarily concerned with a type of memory management 4:06 called reference counting. 4:11 Now reference counting does technically fall under the larger umbrella 4:13 of garbage collection, but it's not a tracing garbage collector. 4:17 So in casual conversation, people will often say 4:21 iOS doesn't have garbage collection, it uses reference counting. 4:24 Great, so what's reference counting? 4:28 Well, when we create an object like this, or an Objective-C like this, 4:31 we're actually allocating a chunk of memory with our alloc call. 4:37 That's what it means, right? 4:40 When we do that, we've just given our object a reference count of one. 4:42 There's just one reference to that chunk of memory. 4:46 Now somewhere later in our code, we might assign a different object like this. 4:49 [SOUND] When we do that, we're pointing a different object, 4:53 myOtherCar to that same memory address. 4:57 So, our reference count increases to two. 5:00 There are two references to that same chunk of memory. 5:03 We might later set one of those values to nil, or no value, like this. 5:06 And that would bring our reference count back down to one. 5:12 You can see how these counts could go up and down all the time, and 5:15 it's great that ARC handles this for us. 5:18 ARC, by the way, is automatic reference counting, and 5:21 we'll talk much more about it as we go. 5:24 You may be wondering, why even keep track of the count of these references? 5:26 Well, once there are no more references, that means it's no longer 5:31 relevant to the program, so we can de-allocate the chunk of memory. 5:34 That means that some other part of the program will be able to make use of that 5:38 precious memory. 5:41 So over the years, 5:43 we as iOS developers have had three ways to address reference counting. 5:44 From the first iPhone release in 2007, until the launch of iOS5 in 2011, 5:49 we manually called the retain-release and auto-release throughout our 5:53 objective C code to explicitly instruct when we needed memory held. 5:58 AKA retained and when it needed to be released. 6:02 As you've probably figured out, in order to know if it's okay to release, 6:06 you need to be sure that it's no longer needed, as in, has a retain count of zero. 6:10 Keeping track of all of that was a huge pain in the neck and 6:15 very error-prone, especially for new developers. 6:18 In 2011, iOS five brought us automatic reference counting, simply know as ARC. 6:20 Just like the name suggests, the manual calls to retain and 6:27 release were now to be handled automatically. 6:30 You still had to consider memory management, but at least 6:32 the reference counts handled themselves, and that was a mammoth step forward. 6:35 With the launch of Swift, we still have ARC, 6:39 though the way we specify our memory directives is a little bit different. 6:41 But we'll get into that later. 6:45 No doubt some of you already focused your work mostly in the latest regime 6:47 of Swift ARC. 6:51 And are preparing to release that mental memory you're now briefly holding for 6:53 the left side of this graphic. 6:57 Well there are three good reasons you shouldn't tune out just yet. 6:59 Firstly, sooner or later someone may hand you Legacy code, and 7:03 you'll need to learn how to either code using that style or 7:07 employ a tool to bring its memory management up to date. 7:10 Secondly, understanding the historical landscape drives home what's happening 7:14 under the hood, since much more used to be visible and actionable to developers. 7:18 Lastly, and perhaps most critically for many of you is, memory management is one 7:23 of the most common topics covered in technical interviews. 7:27 If you can intelligently discuss how iOS memory management has changed over 7:30 the years, you´ll send a signal that´s it a concept you´re familiar with, and 7:34 you´ve gone the extra mile to flesh out your knowledge. 7:37
You need to sign up for Treehouse in order to download course files.Sign up