Object Pooling in Objective-C: 3 Answers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

7/28/2015

Object pooling in Objective-C - Stack Overflow


sign up

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

log in

tour

help

stack overflow careers

Take the 2-minute tour

Object pooling in Objective-C

Is there a nice way to do this in Objective-C, or do I have to write my own tedious logic?
I'm creating and destroying a little of little state objects per frame in an iPhone game. It would be nice if I could just reuse
objects from a pool.
objective-c

object-pooling

asked May 4 '10 at 22:26


Ranking Stackingblocks
287

10

3 Answers

Neither Cocoa nor Objective-C does anything particularly helpful for object pools. They don't
do anything to stop you either, but you'll basically have to DIY.
answered May 4 '10 at 22:43
Chuck
156k

16

207

310

What about creating a memory zone? Am I mistaken in thinking that this works as a sort of pool?
Ranking Stackingblocks May 4 '10 at 23:27
1 They can be useful in this situation, but they're very distinct from object pools, at least as I understand
them. Memory zones can conceivably make allocation more efficient and help keep related objects
contiguous, but you still allocate and deallocate as normal inside them. Chuck May 5 '10 at 0:04

The Sparrow framework for the iPhone contains a class called "SPPoolObject". The
framework uses it internally for helper objects that are used extremely often, like points,
rectangles or matrices.
If you inherit from SPPoolObject, the 'dealloc' method does not really delete it; instead, the
memory will be reused for the next alloc'ed object.
It's quite a simple class - you can easily use it for your projects. It does not have any
dependency, so you can easily grab it out of the Sparrow framework ;)
answered Nov 3 '10 at 10:55
PrimaryFeather
441

12

I think that the table View has some sort of pooling mechanism for the different
TableCell
something like that :
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellId];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CheckMarkCellIdentifier] autorelease];
}

http://stackoverflow.com/questions/2769380/object-pooling-in-objective-c

1/2

7/28/2015

Object pooling in Objective-C - Stack Overflow

Not sure if there is something more generic to use.


answered Jan 27 '12 at 12:21
Itay Levin
320

20

http://stackoverflow.com/questions/2769380/object-pooling-in-objective-c

2/2

You might also like