onevcat / Kingfisher

A lightweight, pure-Swift library for downloading and caching images from the web.
MIT License
23.43k stars 2.66k forks source link

同时为多个ImageView set多个相同url的gif图, 走disk缓存读取 内存会爆炸 #966

Open lixiang1994 opened 6 years ago

lixiang1994 commented 6 years ago

复现场景: 创建一个UICollectionView, 一页可显示10个cell, 每个cell中有一个UIImageView, cellForRow代理中为cell中的imageView setImage, (相同的url) gif图片大小大约在3MB左右, 相等于为不同UIImageView同时setImage 10次, 通过回调得知 cacheType为 disk, 此时内存可以飙升300MB. 如果cacheType为 memory 则不会出现这种情况.

猜测: 在disk读取数据时 memory中并没有缓存, disk读取完成写入memory后, memory中才会有缓存记录, 同时在disk没有读取完成前 多次调用setImage(), 造成多次执行disk读取写入内存的操作, 最终导致内存暴涨.

希望可以尽快解决

lixiang1994 commented 6 years ago

暂时解决方案: 避免使用disk缓存, 使用.fromMemoryCacheOrRefresh

linqingmo commented 6 years ago

try this

KingfisherManager.shared.cache.maxMemoryCost = 30 * 1024
xspyhack commented 5 years ago

You can use Kingfisher.AnimatedImageView instead of UIImageView to improve memory usage.

lixiang1994 commented 5 years ago

@xspyhack Thanks.

stackJolin commented 5 years ago

animatedFrames load all images in memory and play,how AnimatedImageView impove memory usage? @xspyhack AnimatedImageView improve memory usage? @onevcat

xspyhack commented 5 years ago

@stackJolin AnimatedImageView only keeps a limited number of frames in memory, it uses CADisplayLink to update the buffer asynchronously. For more details, you can read the source code.