Release Memory taken by Image View

How would you release the memory taken by the previous image in an image view? In ASS I used something like:
set oldImage toimage of image view “myImageView” of window of theObject
delete oldImage

I have a pop up button and an image view on a window and three images on my desktop. When I run this code my app grows 4 mb every time I select an item from the pop up button.

script testImageProjectAppDelegate

property parent : class "NSObject"
property NSImage : class "NSImage" of current application

property popUpButton : missing value
property ImageView : missing value

on popUpButtonClicked_(sender)
	set selectedImage to (popUpButton's titleOfSelectedItem(sender) as string)
	set pathToImage to POSIX path of ((path to desktop as string) & selectedImage)
	set showImage to NSImage's alloc()'s initWithContentsOfFile_(pathToImage)
	ImageView's setImage_(showImage)
end popUpButtonClicked_
	
on applicationWillFinishLaunching_(aNotification)
	-- Insert code here to initialize your application before any files are opened 
end applicationWillFinishLaunching_

on applicationShouldTerminate_(sender)
	-- Insert code here to do any housekeeping before your application quits 
	return true
end applicationShouldTerminate_

end script

Well you are using an alloc/init method of setting the item, and in Obj-C the rule is “you must release any item yourself that you make by alloc, copy, or new”.

Apple’s memory mgt rules:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH

I placed ImageView’s release() at the end of my on popUpButtonClicked_ handler but looking at activity monitor my app’s memory keeps going up every time I show a new image…

Assuming you have garbage collection on – in which case release is a no-op – what you’re seeing is probably the normal caching NSImage does. It’s using memory because, well, that’s what memory is for. You’ll probably find it starts releasing memory over time.

If you know the images won’t ever need to be reloaded and you’re short of memory, you can probably use setCacheMode: to turn it off. But it’s probably needless extra work.

Look in the Cocoa Drawing Guide under Images → Images and Caching.