1

I have a photo browsing app using UICollectionView and I used Kingfisher to download image from url like this:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    ...
                    cell.ivPhoto.kf.setImage

I debugged using xCode Profile tool and saw that when I scroll through the collection view, memory keep growing because of this object: ImageIO_jpeg_Data, and it linked to Kingfisher. Sometimes when scrolling through a collection view with large images (about 500KB each image), it's very laggy, memory could go up to 500+MB and the app crashed, I saw in lldb it was memory issue. I tried to set this:

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    ImageCache.default.clearMemoryCache()
}

but it didn't solve the crash issue.

Hoang Trung
  • 263
  • 4
  • 15
  • 1
    I had similiar problems with Kingfisher. I am using https://github.com/rs/SDWebImage for my projects now. This works perfectly. – cb89 Jul 13 '17 at 09:02
  • are you handling the reuse of your `UICollectionViewCell`'s? I would suspect something to be wrong there, not in KingFisher. Also, KingFisher is writing images to the disk and reading from them there, so it should be not increasing the memory usage of the app. – dirtydanee Jul 13 '17 at 09:10
  • Yes I re-use the cell, and I already checked for memory leak in my app. The only things I found is memory growth, and it's caused by Kingfisher. I read on web and they said that Kingfisher is using memory cache, that's the reason @dirtydanee – Hoang Trung Jul 13 '17 at 09:14
  • can you show how did you override the `prepareForReuse` function in your `UICollectionViewCell` subclass? – dirtydanee Jul 13 '17 at 09:30
  • I don't use prepareForReuse funtion, I just use this: dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) in my cellForItem function – Hoang Trung Jul 13 '17 at 11:37
  • @HoangTrung well, that is why you are leaking. You need to clean up the memory for the unused `UIImage` instances. Read this: https://developer.apple.com/documentation/uikit/uicollectionreusableview/1620141-prepareforreuse – dirtydanee Jul 13 '17 at 13:16
  • Set a maximum memory cost, otherwise Kingfisher will use as much ram as there is available. Try with a max of 200 for example. https://stackoverflow.com/a/44354411/2227743 – Eric Aya Jul 13 '17 at 14:10
  • @dirtydanee ok let me check that, thanks – Hoang Trung Jul 14 '17 at 03:13
  • @EricAya I did try that, but still crash, and setting that caused laggy in scrolling thru the collection view – Hoang Trung Jul 14 '17 at 03:14
  • @cb89 you're right. SDWebImage does solve my issue. Thanks. This is accepted answer – Hoang Trung Jul 14 '17 at 06:44

1 Answers1

0

Pretty old question but it can help someone else. Kingfisher has unlimited memory cache limit as default. Just set it some value you want.

KingfisherManager.shared.cache.maxMemoryCost = 1000000

Hope this helps.