Avoiding memory fragmentation was Re: Cross platform Cocoa/Obj-C
Avoiding memory fragmentation was Re: Cross platform Cocoa/Obj-C
- Subject: Avoiding memory fragmentation was Re: Cross platform Cocoa/Obj-C
- From: Brian Hook <email@hidden>
- Date: Sat, 22 Dec 2001 11:18:32 -0800
At 10:09 AM 12/22/2001 -0500, Simson Garfinkel wrote:
implement your own memory allocation scheme. However, it is my experience
that many applications which appear to require dynamic memory allocation
with compaction can be redesigned so that they don't. When they are
redesigned, they frequently run faster.
Okay, I'll bite: how would I do this in Obj-C? In C/C++, I typically have
fixed size heaps for certain objects. For example, in a game I might have
a global table of entities:
Entity g_aEntities[ MAX_ENTITIES ];
With a routine that grabs one whenever I need it:
Entity *
Entity::s_createEntity( void )
{
return sm_findFreeEntity(); //searches g_aEntities for a free entity
}
This is technically less efficient, but makes debugging much easier and
gives way more predictable (and capped) memory consumption.
I'm unclear how I would do this in Obj-C (I'm still a newbie), since all
the code I see uses the alloc(withZone)/init paradigm. If I could do use a
pattern in Obj-C where dynamic memory allocation was greatly minimized
without losing the nice aspects of Obj-C, I'd be a happy camper.
The specific app I'm working on is a server/simulation app that is going to
have very long run times (weeks) and likely will do hundreds of thousands
of allocations of many different sizes and in varying orders.
Brian