views:

352

answers:

1

Does the state pattern in an iPhone application -- In my case, a relatively light-weight utility application -- use too much memory?

The state pattern, as I understand it, uses several classes; these classes represent different states. All of the different state objects are instantiated and stored in different pointer variables until the state is needed, at which point it is set to a curState object.

I figure that I could lazy load each state object to save some memory and loading time; I could then release the objects if my app receives a memory warning.

But what I wanted to know is does this pattern utilize too much memory for general usage in an iPhone OS application? Should iPhone developers stay away from this pattern? Is there a different pattern that is better suited to the iPhone OS?

+1  A: 

Not worth worrying about. Unless your states are immensely complicated, or you have thousands and thousands of them, then any art or media files that are in your application will absolutely swamp them in size.

I mean in general - don't sweat the memory usage of your model objects. Objective-C objects are pretty lightweight, in terms of memory usage. If you've got a fairly typical model class, with say a half-dozen fields, each of which is a pointer to some other object, the total memory usage for each instance of that class is something like 32 bytes or so. If you put 1,000 of them into an array or some other data structure, that'd use about as much memory as a 128x128 bitmap.

You're much more likely to run into memory issues due to hanging on to graphics or sounds that you don't need to, than you ever will from the "working" parts of your application.

Mark Bessey
In regards to the art and media files --> that would only be if the art and media files were inside the state objects, correct?
Frank V
Added some more detail to my answer.
Mark Bessey
Thank you for answering my question.
Frank V