Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In today's iOS landscape, you most likely won't have to deal with manually retaining or releasing, but it's still a topic you should be familiar with. Not only will it flesh out your understanding of iOS history, but you may one day find yourself sifting through pre-ARC code.
-
0:00
[MUSIC]
-
0:04
In the next set of videos, we're going to look at a topic we really haven't bothered
-
0:09
to talk about so far at all, but it's quite important programming, and
-
0:12
that is memory management.
-
0:14
When you write code and create an instance of an object every single instance takes
-
0:20
up space in memory by that I mean actual physical space on your device's RAM.
-
0:26
Memory is a precious commodity in a mobile device and
-
0:29
if you end up writing code that consumes too much memory the system,
-
0:33
that is iOS, will automatically kill your apps process.
-
0:37
For the most part, for simple apps like we've written and
-
0:40
we'll be writing in the near future, the memory footprint is barely anything and
-
0:45
we haven't even considered the cost of our code.
-
0:48
However, it is good to understand how the system manages memory,
-
0:53
both from a historical perspective and a look at how it is handled today.
-
0:58
Even with the system managing much of the memory for you today,
-
1:01
there are still some considerations you need to make and
-
1:04
information you have to provide depending on the code you write.
-
1:08
But don't worry, we'll get to that in just a bit.
-
1:10
Prior to iOS 5 in 2011, all memory management was done
-
1:15
manually using a Manual-Retain-Release or MRR model.
-
1:20
Under the MRR model, you the programmer was responsible for
-
1:24
memory management and did so by keeping track of the objects you used.
-
1:28
This was handled by carefully managing object ownership relationships and
-
1:33
making sure an object existed just as long as it needed to.
-
1:38
But not any longer.
-
1:39
In terms of the nitty gritty, a reference counting system internally kept
-
1:44
track of the number of owners an object had.
-
1:47
For example, when you created an instance of an object
-
1:50
you allocated space in memory and increased its reference count.
-
1:54
When you were done with the object you decrease the reference count.
-
1:58
If an object had a reference count greater than zero,
-
2:01
the system kept it alive, otherwise it de-allocated the object.
-
2:06
It was both a cumbersome approach but
-
2:08
one that allowed you to finally control how much memory your application used.
-
2:14
This allowed App Store well on the early iPhone models that had low memory.
-
2:18
You could carefully ensure that your memory footprint was quite small.
-
2:22
If you claimed ownership of an object more times than you released it,
-
2:26
you'd end up with a memory leak that can crash your app.
-
2:29
On the other hand,
-
2:30
if you release the object too many times you end up with what was called a dangling
-
2:35
pointer, where accessing the object caused a crash because they didn't exist.
-
2:39
If all this sounds like a pain, many people thought so as well, which is why,
-
2:43
in iOS 5, Apple introduced a new model for
-
2:46
memory management called automatic reference counting or ARC.
-
2:50
Let's head over to the next video to talk about it.
You need to sign up for Treehouse in order to download course files.
Sign up