Improved memory management for images