Home > Articles > Programming > General Programming/Other Languages

The Big Nerd Ranch Guide to iOS Programming: Managing Memory with ARC

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book
In this chapter, you will learn how memory is managed in iOS and the concepts that underlie automatic reference counting, or ARC.

Let's start with some basics of application memory.

The Stack

When a method (or function) is executed, a chunk of memory is allocated from a part of memory called the stack. This chunk of memory is called a frame, and the frame stores the values for variables declared inside the method. A variable declared inside a method is called a local variable.

When an application launches and runs main(), the frame for main() is put on the stack. When main() calls another method (or function), the frame for that method is put on top of the stack. Of course, that method could call another method, and so on, until you have a towering stack of frames. As each method or function finishes, its frame is “popped off” the top of the stack and destroyed. If the method is called again, a new frame will be allocated and put on the stack.

For example, in the RandomItems application, the main function runs BNRItem's randomItem method, which in turn runs alloc. The stack would look like Figure 3.1. Notice that main()'s frame stays alive while the other methods are executingbecause it has not yet finished executing.

Figure 3.1

Figure 3.1 Stack growing and shrinking

The randomItem method runs inside a loop in main(). With every iteration of the loop, the stack grows and shrinks as frames are put on and popped off the stack.

  • + Share This
  • 🔖 Save To Your Account


comments powered by Disqus