0

I have a scrollview that I add maybe 400 thumbnails to. I profile it in instruments and watch the memory livebytes go up to about 70MB. Then I removefromsuperview all of them and the live bytes stays the same.

Need to recover this memory. What's the trick?

Thanks.

user953175
  • 205
  • 3
  • 9

3 Answers3

0

I think this is a similar question: Problem dealloc'ing memory used by UIImageViews with fairly large image in an UIScrollView

I've had similar issues before. One solution appears to be to set the UIImageView image to nil before final release, per an answer in this question: confusing memory allocation error on iPhone

Community
  • 1
  • 1
TomSwift
  • 39,369
  • 12
  • 121
  • 149
  • confusing memory allocation error on iPhone: this was the solution. setting the image in a UIImageView to nil before releasing the UIImageView blew away all the used memory in the scrollview. – user953175 Sep 11 '12 at 23:42
0

Make sure you are also removing the UIImage instances from the datasource. Also, consider putting the UIImage inside an NSCache, purging them when memory goes low, and use a UITableView instead of a UIScrollView (to reuse the interface elements).

J2theC
  • 4,412
  • 1
  • 12
  • 14
0

There are two things to manage - views and images. You only need enough scroll view subviews to fill the visible portion of the scroll view. The nice pattern here is to have a reuse pool. When you need a subview for the scroll view, check for one in the reuse pool. If there's none there, allocate one. When scrolling happens, put views that are no longer visible into the reuse pool and add views (first checking the reuse pool) in newly visible spaces.

Images can be placed in a cache indexed by their url and by time. A mutable dictionary is well suited for the url index. A mutable array containing the urls can be a nice FIFO queue for the time index. The url can be either a file url for images packaged in the app or a remote url if the images are downloaded. To add to the cache, add a url-image pair to the dictionary, and add the url to the front (index 0) of the time array.

Each time you add to the cache, check to see if it's size exceeds your goal. If it does remove the oldest image. To do that, get the lastObject from the array, remove that url key-value pair from the dictionary and removeLastObject from the array.

This image cache can/should be larger than the number of visible views in the scroll view. You can tune this size to match the desired memory goal, taking into account the time it takes to get the image (if the images are remote, you'll probably want a bigger cache).

When adding an image subview to the scroll view, assign it a default image. Check the cache for a cached image using a url lookup. If there's one there, replace the default. If not, start an asynchronous get for that image. When that image arrives add it to the cache and check the scroll view to see if the subview containing the image is still visible (it may have been scrolled away). If it is, set the image.

danh
  • 62,181
  • 10
  • 95
  • 136