Graphics Programs Reference
In-Depth Information
The Heap
All Objective-C objects are stored in a part of memory called
the heap
. When we send an
alloc
message to a class, a chunk of memory is allocated from the heap. This chunk in-
cludes space for the object's instance variables.
For example, consider an instance of
NSDate
, which represents a specific point in time.
An
NSDate
has two instance variables: a
double
that stores the number of seconds since
a fixed reference point in time and the
isa
pointer, which every object inherits from
NSObject
. A
double
is eight bytes, and a pointer is 4 bytes, so each time
alloc
is sent
to the
NSDate
class, 12 bytes is allocated from the heap for a new
NSDate
object.
Consider another example:
BNRItem
. A
BNRItem
has five instance variables: four point-
ers (
isa
,
itemName
,
serialNumber
, and
dateCreated
) and an
int
(
valueInDollars
). The amount of memory needed for an
int
is four bytes, so the
Figure 3.1 Byte count of BNRItem and NSDate instances