views:

202

answers:

2

Is it wise to create views in Cocoa that have dimensions around 15000 pixels? (of course only small part of this view will be visible at a time in a NSScrollView)

Interface Builder has limit of 10000 pixels in size. Is this an artificial limitation or is there a good reason behind it?

Should I just create huge view and let NSScrollView/Quartz worry about rendering it efficiently (my view is drawn programmatically within area requested in drawRect) or do I risk excessive memory usage and other problems? (e.g. could OS X try to cache whole view's bitmap in video memory at any time?)

A: 

Well, if Cocoa does try to cache the entire view in memory, that would be a problem:

10000 * 10000 = 100,000,000
* 4 = 400,000,000

That's 400 MB in raw RGBA pixels for one view. If we want to be really pessimistic, assume that NSView is double-buffering that for you, in which case your memory usage doubles to 800 MB.

In the worst case, your user is running your app on an old Mac mini with 1 GB of RAM—of which you have just used 80%. The system will certainly start paging way before this point, making their system unbearably slow.

On the other hand, it is the easiest way to implement it that I can think of, so I say try it and see what Activity Monitor says about your memory usage. If it's too high, try changing various options of the scroll view and clip view; if that doesn't work, I can think of nothing else but to make your own scrollers and fake it.

Peter Hosey
Memory usage is reasonable on my machine (test app uses 3MB of private memory + 15MB shared), however I'd like to know whether that's just simple case on my machine or is that a guaranteed behavior (are there any graphic cards or system settings that could cause Cocoa to cache entire bitmap?)
porneL
You can never rely on implementation details that aren't explicitly specified in the documentation. That said, it sounds like you should be able to go ahead with this approach. You can always switch to something else if a performance problem does appear, perhaps in user testing.
Peter Hosey
+1  A: 

Views don't have backing stores, unless they are layer-backed. The window is what has the backing store, so the amount of memory used to display the view is limited to the size of the window.

So, the answer is yes. Go ahead and make your views as big as you want.

(Of course, you'll want to limit the drawing you do in the view to the rect passed in drawRect: or you'll be wasting a lot of time doing invisible drawing.)

kperryua
Views can and sometimes do create NSImage snapshots for performance reasons. Indeed, NSClipView has exactly such an option in its API (although it almost certainly does not capture the entire document view).
Peter Hosey