UPDATE: I fixed the memory leak. I am still very curious to hear what the best practices are for an app that is designed to pull down a potentially endless stream of image data to balance performance (user sees image without download delay) and memory usage. I am also curious if anyone can speak to 1) is a PFFile just a path used to get the data in a stream or is each PFFile element taking up considerable space? 2) To release the space after an image has been viewed will setting the image=nil release the space?
EDITED ORIGINAL POST: Is there a best practice for image heavy apps (an example would be facebook or instagram) where users can continually cycle through images. I noticed that memory usage constantly increases using my current approach.
Currently I have a parse-server class for "Images" where the actual image is a PFFile. I select * from this and read each row into an array of customer data struct containing the "Images" fields of which the PFFile is one. I then maintain a "current Position" variable and each time the user clicks next I add one convert the PFFile at current position into a UIImage, set that in the array element, and update the image view (that way when they click back I don't have to reprocess the PFFile. When testing my app I quickly saw the memory usage spike to 2gb since going through every image meant that every UIImage was retained in an array. I definitely think that I can NOT hold on to every UIImage since users may look at all the images and I will run out of memory.
I suspect that instead of retaining every UIImage after I convert the data I need to release the previous ones. Generally I would appreciate any advice or direction for things that I can educate myself on to responsibly manage data when I am allowing users to scroll through an indeterminate number of images.
If your memory isn't decreasing even after the view controller disappears then you have a memory problem somewhere. Maybe something else is referencing that array with images and causing it to not be released?
For your general problem, you need some way to save the images to a cache (on disk). Keeping an ever-growing array of UIImage objects will mean your app will be terminated. If you try testing on a device you'll find that 2GB of memory being used will cause the system to terminate your app. For caching use a library. SDWebImage is very popular.
Finally, pay attention to the system telling you about memory pressure with UIViewController
's didReceiveMemoryWarning
and the UIApplicationDidReceiveMemoryWarningNotification
notification. When you get one of those notifications, release some of the images. How you should do this is complex and depends on your own code.