Re: NSImages are killing my WindowServer
Re: NSImages are killing my WindowServer
- Subject: Re: NSImages are killing my WindowServer
- From: Ricky Sharp <email@hidden>
- Date: Thu, 23 Mar 2006 11:15:30 -0600
On Thursday, March 23, 2006, at 10:31AM, James Bucanek <email@hidden> wrote:
>I'm hoping that someone with some knowledge of how NSImage works can help me. (Since I rarely -- read "never" -- mess with NSImage, that should be just about everyone...)
>
>I'm working on an application that writes Finder icons to a file -- thousands of them. I wrote to this list some time ago and asked for advice on how to convert an IconRef into an NSImage. (I use the NSImage to encode the icon using TIFF for storage). From these suggestions, I wrote the following little utility:
>
>NSImage* makeNSImageFromIconRef(IconRef iconRef)
>{
> CGRect folderIconSize = { { 0,0 }, { 16,16 } };
>
> NSImage* image = [[[NSImage alloc] initWithSize:NSMakeSize(16,16)] autorelease];
> [image lockFocus];
> PlotIconRefInContext((CGContextRef)[[NSGraphicsContext currentContext] graphicsPort],
> &folderIconSize,
> kAlignNone,
> kTransformNone,
> NULL/*inLabelColor*/,
> kPlotIconRefNormalFlags,
> iconRef);
> [image unlockFocus];
>
> return (image);
>}
>
>The problem is that after my application runs for a bit, the WindowServer daemon begins to consume the machine. The entire user interface chokes up -- windows, dock, menu, everything is sluggish and stalls for seconds at a time. The virtual memory footprint of WindowServer spikes up to 1.5GB or more, and its CPU usage exceeds 100% (this is on a dual processor system). Also the number of ports reported in top spike up to over a thousand (at which time top simply reports it as '>>>').
>
>As soon as the application stops, WindowServer goes catatonic for about 30 seconds to a minute; chewing up all available CPU time, while its memory usage slowly creeps back down to normal. Eventually it recovers and all it well again.
>
>If I comment the drawing portion of this code out, the application runs normally and WindowServer doesn't get bogged down.
>
>So can anyone make any suggestions on how to address this performance problem? Would it help to release the NSImage immediately instead of letting hundreds of them accumulate in the autorelease pool? Is there some caching or something that I can turn off to keep WindowServer from chewing up so many resources? Should I be reusing a singleton NSImage instead?
As John mentioned in another reply, definitely look at releasing the objects earlier and see if that helps.
Something I always code up when working with many images is an "ImageFactory". While the contents of NSImages are cached, I find it helpful to provide a helping hand. My factory often implements it's own cache of a fixed size where I implement a 'most-recently-used' policy. This allows me to easily play around with the cache size to experiment with memory consumption vs. speed.
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden