views:

233

answers:

3

Cocoa provides for page-aligned memory areas that it calls Memory Zones, and provides a few memory management functions that take a zone as an argument.

Let's assume you need to allocate a block of memory (not for an object, but for arbitrary data). If you call malloc(size), the buffer will always be allocated in the default zone. However, somebody may have used allocWithZone: to allocate your object in another zone besides the default. In that case, it would seem better to use NSZoneMalloc([self zone], size), which keeps your buffer and owning object in the same area of memory.

Do you follow this practice? Have you ever made use of memory zones?

Update: I think there is a tendency on Stack Overflow to respond to questions about low-level topics with a lecture about premature optimization. I understand that zones probably mattered more in 1993 on NeXT hardware than they do today, and a Google search makes it pretty clear that virtually nobody is concerned with them. I am asking anyway, to see if somebody could describe a project where they made use of memory zones.

A: 

If you find yourself doing this, you're probably operating at a lower level than you really ought to be. The subsystem pretty much ignores them; any calls to +alloc or such will get you objects in the default zone. malloc and NSAllocateCollectable are all you need to know.

Jim Puls
So you are basically saying I shouldn't have asked my question?
benzado
+alloc only returns an object in the default zone because it passes the default zone to +allocWithZone: which is the _real_ allocator.
Graham Lee
+1  A: 

You're absolutely right in your entire question, but in practice, nobody really uses zones. As the page you link to puts it:

In most circumstances, using the default zone is faster and more efficient than creating a separate zone.

The benefit of making your own zone is:

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.

If a page fault occurs, that means that the system was recently paging things out and is therefore slow anyway, and that either your app is not responsible or the solution is in the part of your app that allocated too much memory at once in the first place.

So, basically, the question is “can you prove that you really do need to create your own zone to fix a performance problem or make your app wicked fast”, and the answer is “no”.

Peter Hosey
I know you meant well, but your last paragraph is a little bit rude. That is not my question at all; you're assuming I'm only interested in finding magic optimizing pixie dust and that I didn't read or comprehend the document I linked to.
benzado
No, I'm assuming no such thing. My point is that the only purpose of zones is to make things faster, and they don't ever make things fast enough, for any purpose, to justify using them.
Peter Hosey
I don't think Peter's trying to say that was YOUR question, just that it's THE question to ask when considering whether to use custom zones. It's a rhetorical question to ask oneself during the design process.
Quinn Taylor
+2  A: 

I've written software for NeXTStep, GNUstep on Linux and Cocoa on Mac OS X, and have never needed to use custom memory zones. The condition which would suggest it as a good improvement to the software has either never arisen, or never been detected as significant.

Graham Lee