Skip navigation.
 
mlI'm losing my memory (GC & NSImageView)
FROM : Stuart Rogers
DATE : Sun Nov 04 16:10:53 2007

I'm working on a simple image compositor application, and I have a
memory usage/leak problem that I really can't pin down (I have garbage
collection enabled).  The compositor has an NSImageView panel for
the composited/filtered/scaled-down images, and an NSSlider for
scrubbing back and forth through the frames.  The source images are
numbered sequences of TGA files (typically 1280x720 pixels).
Whenever the slider updates, it triggers a redraw.

The problem is that within a very short period of moderate scrubbing
(causing maybe 1000 redraws over a period of about a minute or two)
I can fill up my G5's 5GB of RAM and cause the creation of multiple
swap files.

Here's the redraw code. At most it occurs once per event cycle:

  // Fetch the composition; bounds is an NSRect
  CIImage *imgResult = [layerManager renderFrame:frameNumber 
toSize:bounds];
  NSImage *image;
  NSCIImageRep *imgRep;

  // Crop the image to the required size
  CIFilter *crop  = [CIFilter filterWithName:@"CICrop"];
  CIVector *frameSize = [CIVector vectorWithX:0.0 Y:0.0
                        Z:bounds.size.width W:bounds.size.height];
  [crop setValue:imgResult forKey:@"inputImage"];
  [crop setValue:frameSize forKey:@"inputRectangle"];
  CIImage *finalImage = [crop valueForKey:@"outputImage"];

  // Create an NSImage from the result.
  imgRep = [NSCIImageRep imageRepWithCIImage:finalImage];
  image = [[NSImage alloc] initWithSize: NSMakeSize(bounds.size.width, 
bounds.size.height)];
  [image addRepresentation:imgRep];

  // Display the result; previewView is an NSImageView
  [previewView setImage:(NSImage*)image];

Once this is done, the only thing holding on to the image is 
previewView,
the NSImageView that displays the result.  When previewView gets a
new image, I would expect the garbage collector to eventually dispose
of the previous image, but there's little evidence this is occurring. 
On a
couple of occasions I've seen some memory reclaimed, but not much.
I know GC generally doesn't result in immediate clean up, but this is
getting messy.

ObjectAlloc (within Instruments) reckons I'm using far less memory (of
the order of tens of megabytes).  Leaks shows a 'negligible' 500kB.
I'm confused.

I would be grateful to anyone who can shed some light on to what may
be the cause of my problem, and eternally so to anyone who can help
me overcome it.

OT: OMG! ApKiDo won't launch on my machine!

Stuart Rogers  /  Dual 2.5GHz G5 + 5GB RAM + OS X 10.5

Related mailsAuthorDate
mlI'm losing my memory (GC & NSImageView) Stuart Rogers Nov 4, 16:10
mlRe: I'm losing my memory (GC & NSImageView) Erik Buck Nov 4, 16:51
mlRe: I'm losing my memory (GC & NSImageView) Bill Bumgarner Nov 4, 17:21
mlRe: I'm losing my memory (GC & NSImageView) Stuart Rogers Nov 4, 23:56
mlRe: I'm losing my memory (GC & NSImageView) Uli Kusterer Nov 5, 01:18
mlRe: I'm losing my memory (GC & NSImageView) Rob Keniger Nov 5, 05:15
mlRe: I'm losing my memory (GC & NSImageView) Bill Bumgarner Nov 5, 05:47
mlRe: I'm losing my memory (GC & NSImageView) Stuart Rogers Nov 5, 22:16