There is another part of memory called the heap that is separate from the stack. The reason for the names “heap” and “stack” has to do with how you visualize them. The stack can be visualized as an orderly stack of frames. The heap, on the other hand, is where all Objective-C objects live. It is a giant heaping mess of objects. You use pointers to keep track of where those objects are stored in the heap.
When you send the alloc message to a class, a chunk of memory is allocated from the heap. This chunk is your object, and it includes space for the object's instance variables. An instance of BNRItem has five instance variables: four pointers (isa, _itemName, _serialNumber, and _dateCreated) and an int (_valueInDollars). Thus, the chunk of memory that is allocated includes space for one int and four pointers. These pointers store addresses of other objects in the heap.
An iOS application creates objects at launch and will typically continue to create objects for as long as the application is running. If heap memory were infinite, the application could create all the objects that it wanted to and have them exist for the entire run of the application.
But an application gets only so much heap memory, and memory on an iOS device is especially limited. Thus, this resource must be managed: It is important to destroy objects that are no longer needed to free up heap memory so that it can be reused to create new objects. On the other hand, it is critical not to destroy objects that are still needed.
ARC and memory management
The good news is that you do not need to keep track of which objects should live and die. Your application's memory management is handled for you by ARC, which stands for Automatic Reference Counting. All of the applications in this book will use ARC. Before ARC was available, applications used manual reference counting. There is more information about manual reference counting at the end of the chapter.
ARC can be relied on to manage your application's memory automatically for the most part. However, it is important to understand the concepts behind it to know how to step in when you need to. Let's start with the idea of object ownership.