Zones are page-aligned areas of memory that hold the objects and data allocated by an application. Each zone contains a private memory heap, with its own free list and pool of memory pages. The system assigns each application a “default” zone initially and applications can create additional zones later. The use of additional zones has both advantages and disadvantages and should be considered with great care. In most circumstances, using the default zone is faster and more efficient than creating a separate zone.
Because zones maintain their own pool of memory, creating new zones increases the memory footprint of your application. However, this increased memory footprint can yield performance advantages in other areas. For example, allocating a group of related objects in the same zone co-locates those objects in the same area of memory. If a page fault occurs when trying to access one of the objects, loading the page brings in all of the related objects, which could significantly reduce the number of future page faults.
Zones are represented in Cocoa by the opaque data type NSZone
.
Creating and Managing Zones
Allocating Memory in Zones
Creating a zone for your application is not required since the system creates a default zone for you automatically. In fact, creating zones is generally the exception to the rule and should only be used in situations where the need for better performance or memory efficiency outweighs the overhead of maintaining a zone. Most of your memory allocations will otherwise occur in your application’s default zone.
To create a new zone, you use the NSCreateZone
function. This function uses the system zone allocation routines to set aside an area of memory for your zone. When you call this function, you specify the initial size of the zone and the amount by which to grow the zone. If you attempt to allocate memory beyond the size of your zone, the system automatically expands the zone by the amount you indicate in the second parameter. Thus, if you allocate memory for a contiguous block of structures, you can grow the memory by the exact size of the structure. The following example allocates an 8K block of memory and specifies that the block should grow by 4K when more space is needed.
NSZone* tempZone = NSCreateZone(8192, 4096, YES); |
The Foundation Kit defines several functions for setting and getting the name of a zone and for finding existing zones. You can use the NSZoneSetName
function to assign a name to a zone and the NSZoneName
function to retrieve that name later. The NSZoneFromPointer
function takes a pointer to a block of memory and returns the memory zone in which the block was allocated.
To destroy a zone completely, you must use the system level function malloc_destroy_zone
. However, destroying a zone that still contains referenced objects can cause severe problems in your application. Before you destroy a zone, you should make absolutely sure that it does not contain any referenced objects or memory blocks. You should never destroy the default zone assigned to your application.
To allocate Cocoa objects in a zone, use the allocWithZone:
class method of NSObject
. This method allocates memory for your object in the zone you specify and returns the object pointer to you. The alloc
method also allocates memory in a zone, the default zone in that case, and is equivalent to calling allocWithZone:
with a parameter of nil
. To release an object, you use the same release
and autorelease
methods you would normally use.
Note: Although the allocWithZone:
method lets you specify a zone in which to allocate the object, the provided zone is only a suggestion. The object’s implementation may ignore the zone if it chooses to.
In addition to allocating Cocoa objects in zones, you can allocate your own custom data structures using functions defined in Foundation Kit. You might use these functions to allocate memory for a C-type data structure or to optimize the storage implementation of one of your classes. The Foundation Kit function names and behaviors are based on routines from the standard C library but include support for allocating memory in zones.
To allocate a block of memory, use the NSZoneMalloc
function. This function allocates a memory block of a fixed size and returns a basic pointer for you to use.NSZoneMalloc
doesn’t initialize the allocated memory to zero; if you want zeroed memory, use the NSZoneCalloc
function instead. You can resize a pointer block using the NSZoneRealloc
function and deallocate a block using NSZoneFree
.
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)