SDWebImage - memory leak in UITableView?

I'm sorry in advance because maybe this is a stupid question and noob question ...

I use SDWebImage to display images in a UITableView in my cellForRowAtIndexPath method using classic

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

(the displayed images are light and well-compressed jpg, just some co, and of course I use dequeueReusableCellWithIdentifier).

When I test my application using Instrument - Allocations "and just scroll down my UITableView (with 40 cells containing an image a bit like Instagram), I got a huge amount of memory! (See screenshot)

instrument allocation screenshot

But it looks like " VM " and especially " VM: CG raster data " from the coreGraphics library.

So the questions are:

  • This is normal?
  • Is this a serious problem?
  • Is there any way to avoid this?

Sorry, but after several searches on the Internet I can’t find any relevant information about β€œ VM: CG raster data ” ... Any idea? Thanks in advance!

+7
memory-management ios memory-leaks uitableview sdwebimage
source share
4 answers

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

Root cause

The main reason was because my table cell kept a strong pointer to an image that is stored in the SDWebImage cache. This strong pointer caused the SDWebImage removeAllObjects free memory function to not free memory when receiving a memory warning from iOS.

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

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

To test this solution, run the application and simulate a memory warning. You will be able to see the deleted data.

+3
source share

As someone working on SDWebImage explained to me:

SDWebImage Cache images use NSCache . This is discarded memory. See Apple's documentation , so this is completely normal behavior, and memory is freed if necessary.

0
source share

I had the same problem. My problem, apparently, came from something other than the SDWebImage cache, but since it was contributing to memory accumulation, the first thing I, however, concluded was that the cache could be the cause of my problem. But this is not so. You may have the same problem. Keep in mind that I am using ARC.

  • Run the profiler with the Leaks template and check the allocation of your own classes in the Allocation Summary .
  • Immerse yourself in them and check how they are distributed if there are leaks. Keep in mind that the leak does not appear in the Leaks tool because you are using ARC. So the tools might think that everything is going fine, but somewhere there might be a leak. Having plunged into the distribution of your classes, you can understand what is going wrong.
  • Keep in mind that retention / release count information is provided only when using the Leaks template, and not when using the Allocations template.

My problem was that I referenced the instance and self variables directly from within the blocks, without reassigning them to __weak variables. When self is used inside a block, it will be automatically saved by ARC and sometimes will never be released. Weak recommendation prevents this.

For example, this is not true:

 [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardDidShowNotification object:nil queue:nil usingBlock:^(NSNotification *note) { [self.view setContentOffset:CGPointMake(0.0, kKeyboardOffset) animated:YES]; }]; 

You must call self using the __weak link, for example:

 __weak YourViewControllerClass *weakSelf = self; [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardDidShowNotification object:nil queue:nil usingBlock:^(NSNotification *note) { [weakSelf.view setContentOffset:CGPointMake(0.0, kKeyboardOffset) animated:YES]; }]; 

Since my application uses many blocks, I had a ton of leaks, the Leaks tool could not detect. When I fixed them, the memory issue disappeared.

Hope this helps.

0
source share

Despite the fact that NSCache free up memory on alerts with low memory, more critical (and reduced in size) objects will be discarded in other parts of the program.

It would be better to set the maxMemoryCost library (which sets NSCache totalCostLimit ) to a limit to prevent SDWebImage from starting from memory notifications.

0
source share

All Articles