8

I need to free all the memory occupied by images fetched using Kingfisher. I have a UITableView that store a lot of images and has the Load More feature also.

I tried these measures.

In viewDidLoad() I am setting the cache size.

let cache = KingfisherManager.sharedManager.cache
cache.maxMemoryCost = 50 * 1024 * 1024
// Set max disk cache to 50 mb. Default is no limit.
cache.maxDiskCacheSize = 50 * 1024 * 1024
// Set max disk cache to duration to 3 days, Default is 1 week.
cache.maxCachePeriodInSecond = 60 * 60 * 24 * 3

In viewWillDisappear() I am clearing this.

cache.clearMemoryCache()
// Clear disk cache. 
cache.clearDiskCache()
// Clean expired or size exceeded disk cache.
cache.cleanExpiredDiskCache()

Still memory does not gets free as expected. Kindly correct me if I m missing something.

Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54

6 Answers6

18

On receiving memory warning I cleared the cache and it worked fine for me now :

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    cache.clearMemoryCache()
    cache.clearDiskCache()
    cache.cleanExpiredDiskCache()
}

Update for Swift 4 :

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    KingfisherManager.shared.cache.clearMemoryCache()
    KingfisherManager.shared.cache.clearDiskCache()
    KingfisherManager.shared.cache.cleanExpiredDiskCache()
}
Ankit Kumar Gupta
  • 3,994
  • 4
  • 31
  • 54
8

KingFisher in Swift 4

KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
KingfisherManager.shared.cache.cleanExpiredDiskCache()
Chhaileng
  • 2,428
  • 1
  • 27
  • 24
4

According to the documentation:

Kingfisher will purge the memory cache when received a memory warning, as well as clean the expired and size exceeded cache when needed. Normally there is no need to clean cache yourself. These methods exist in case of you want your users have more control on the cache.

DoesData
  • 6,594
  • 3
  • 39
  • 62
3

Swift 5, you could replace 3 line with just one.

KingfisherManager.shared.cache.clearCache()
MMDev11070
  • 131
  • 5
0

Remove images from cache manually

Kingfisher manages its cache automatically. But you still can manually remove a certain image from cache:

KingfisherManager.shared.default.cache.removeImage(forKey: cacheKey)

Or, with more control:

KingfisherManager.shared.default.cache.removeImage(
    forKey: cacheKey,
    processorIdentifier: processor.identifier,
    fromMemory: false,
    fromDisk: true)
{
    print("Removed!")
}
0

Just load with options: [.forceRefresh]

If set, Kingfisher will ignore the cache and try to start a download task for the image source.

self.kf.setImage(with: url, placeholder: placeHolderImage, options: [.forceRefresh])