Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDWebImage - Memory leak in UITableView?

I'm sorry in advance as maybe this is a dumb and noob question...

I'm using SDWebImage to display pictures in a UITableView in my cellForRowAtIndexPath method, using the classic

[cell.pointPicture setImageWithURL:[NSURL URLWithString:thePoint.imageURL] placeholderImage:[UIImage imageNamed:POINT_DEFAULT_IMAGE]];

(the displayed pictures are light and well compressed jpgs, just some ko, and yes I'm using dequeueReusableCellWithIdentifier of course).

When I inspect my app with "Instrument - Allocations", and just scroll down my UITableView (with 40 cells containing picture, a bit like Instagram), I got a huge amount of memory used ! (see screenshot)

instrument allocation screenshot

But it seems to be "VM", and especially "VM: CG raster data" from the coreGraphics library.

So the questions are :

  • Is it normal?
  • Is that a serious problem?
  • Is there a way to avoid this?

I'm sorry but after few search on the web I can't find any relevant information concerning the "VM: CG raster data"... Any idea? Thanks in advance !

like image 899
Fabien ParseError Avatar asked Oct 27 '13 15:10

Fabien ParseError


1 Answers

I experienced the same issue and found the root cause, at least in my implementation.

Root Cause

The root cause was that my table cell stored a strong pointer to the image which is stored in SDWebImage cache. This strong pointer, caused the memory release function of SDWebImage removeAllObjects not to release any memory when receiving a memory warning from iOS.

Solution 1 - Keep weak pointers from within your ViewController and allow only SDWebImage to keep a strong pointer to all UIImage objects.

Solution 2 - Implement - (void)prepareForReuse and set the image pointers to nil

To test this solution run your application and simulate a memory warning - You will be able to see the data removed

like image 66
avihayas Avatar answered Sep 19 '22 09:09

avihayas