Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak #25

Open
dfmarulanda opened this issue May 20, 2019 · 18 comments
Open

Memory Leak #25

dfmarulanda opened this issue May 20, 2019 · 18 comments
Assignees
Labels

Comments

@dfmarulanda
Copy link

I'm getting around 500MB of memory leak with this lib.
Screen Shot 2019-05-20 at 5 43 26 PM
Screen Shot 2019-05-20 at 5 43 12 PM

@yeatse
Copy link
Owner

yeatse commented May 21, 2019

Hello @dfmarulanda , I'm sorry to hear this. But I can't reproduce it in my own project (using KingfisherWebP 0.5.0, Kingfisher 5.5.0, libwebp 1.0.2 & Xcode 10.2). After double checking the implementation of WebPImageCreateWithData I also can't find the line which may cause this memory leak.
Would you mind sharing your code or a demo project to reproduce it? 😅

@yeatse yeatse self-assigned this May 21, 2019
@dfmarulanda
Copy link
Author

dfmarulanda commented May 21, 2019

This is my code.
KingFisherSource is an array of Kingfisher Images. Every CELL have a ImageSlideShow with more than 1 picture.

  viewModel.imageURL.map { itemImages in
      let processor = WebPProcessor.default
      let inputs: [KingfisherSource] = itemImages.compactMap({ return KingfisherSource(url: URL(string:$0!)!, options: [.transition(.fade(0.33)), .processor(processor), .cacheSerializer(WebPSerializer.default), .scaleFactor(UIScreen.main.scale)]) })
      return inputs }
      .observeOn(MainScheduler.instance)
      .subscribe(onNext: { [unowned self] inputs in
        self.itemSlideShow.setImageInputs(inputs)
      }).disposed(by: disposeBag)

@yeatse
Copy link
Owner

yeatse commented May 22, 2019

I'm guessing you are using ImageSlideshow, right? I set up a demo project with ImageSlideshow, but still couldn't reproduce this issue.
Your screenshot just shows that KingfisherWebP allocates memory, but not leaks memory, so I assume it should not be a KingfisherWebP issue. Maybe it's designed feature relating to memory cache in Kingfisher itself.
You can add this line in your ImageSlideshow view controller to see whether the "memory leak" still happen:

override func viewDidDisappear(_ animated: Bool) {
    KingfisherManager.shared.cache.clearMemoryCache()
}

@dfmarulanda
Copy link
Author

I changed everything from WebP to JPG and remove the library, and i have no leaks right now.
I'll try to cleanMemoryCache.

@yeatse
Copy link
Owner

yeatse commented May 27, 2019

Hello, have you tried the clearMemoryCache method? Did it fix this memory problem?

@Out1and3r
Copy link

Out1and3r commented Jun 3, 2019

I experience exactly same issue. Massive memory leaks but no issues with JPG and default Kingfisher processors.

@duyhungtnn
Copy link

Hello, have you tried the clearMemoryCache method? Did it fix this memory problem?

I experience exactly same issue , try clearMemoryCache but no hope :( , app quickly crash by the system because use too much memory.

@WangKunKun
Copy link

我也遇到了 内存暴涨的问题··· 请问你是怎么解决的?

Hello, have you tried the clearMemoryCache method? Did it fix this memory problem?

I experience exactly same issue , try clearMemoryCache but no hope :( , app quickly crash by the system because use too much memory.

@yeatse
Copy link
Owner

yeatse commented Sep 24, 2019

Hi @duyhungtnn @WangKunKun @Out1and3r , I'm investigating this problem now, but cannot reproduce it myself currently. In order to make sure it's not the cache feature in Kingfisher itself, would you mind trying these methods in your projects?

// Limit memory cache size to 20 MB.
cache.memoryStorage.config.totalCostLimit = 20 * 1024 * 1024

// Limit memory cache to hold 50 images at most. 
cache.memoryStorage.config.countLimit = 50

The code above is taken from Kingfisher Cheat-Sheet, please let me know whether it works or not. Thank you all 🥺

@ZackZheng2014
Copy link

It happened to me at Animated WebP list when I load more items .

@ZackZheng2014
Copy link

It happened to me at Animated WebP list when I load more items .

it's fine now and memory used return to normal since I changed all webp url to gif.

@duyhungtnn
Copy link

duyhungtnn commented Jul 9, 2020 via email

@tanis2000
Copy link

I'm experiencing the same issue where memory is being allocated and then it's never freed.

I'm attaching a screenshot of instruments for you to have a look at... the version of the library is 1.1.0 (the latest at the time I'm writing this)

Schermata 2021-01-31 alle 13 37 52

@tanis2000
Copy link

I believe the leak is mostly caused by the following allocation:

void *bufCopy = malloc(bufSize);

It is this line: https://github.com/Yeatse/KingfisherWebP/blob/7f5e0f5eb0e799e09d6850a0da85ace97f8d9a74/KingfisherWebP/Classes/CGImage%2BWebP.m#L260

I can't see this memory being deallocated anywhere. @yeatse when can we free this buffer?

@yeatse
Copy link
Owner

yeatse commented Feb 10, 2021

I believe the leak is mostly caused by the following allocation:

void *bufCopy = malloc(bufSize);

It is this line:

https://github.com/Yeatse/KingfisherWebP/blob/7f5e0f5eb0e799e09d6850a0da85ace97f8d9a74/KingfisherWebP/Classes/CGImage%2BWebP.m#L260

I can't see this memory being deallocated anywhere. @yeatse when can we free this buffer?

Thanks for pointing it out. The memory pointed by bufCopy is transferred to CGDataProvider by CGDataProviderCreateWithData, and will be freed in WebPFreeInfoReleaseDataCallback when the CGDataProvider deallocates. CGDataProvider's lifecycle is bounded to the result image, so when the image releases, everything is freed.

You can test it yourself by forcing Kingfisher not to cache any image using .forceRefresh option:

KingfisherManager.shared.defaultOptions += [.forceRefresh]

image

@tanis2000
Copy link

Thanks for the explanation. I didn’t see that callback.
in my case it looks like the real issue is with the cache storing huge images. I don’t know where they came from but I disabled storing the original and I’m only saving the webp images which are all well under 500kb. But the other images were allocating about 200-300Mb each. Put them in a collection view and everything is going to crash and burn

@LOVEY9527
Copy link

LOVEY9527 commented Mar 14, 2024

内存暴增是由于缓存了解析的每一帧图,目的是降低CPU的消耗
image
所以即使设置KingfisherManager缓存上限也是没用的
#25 (comment)
image
简单的做法就是设置frameCache缓存上限,并且缓存处理过的每一帧图

@yeatse
Copy link
Owner

yeatse commented Mar 21, 2024

内存暴增是由于缓存了解析的每一帧图,目的是降低CPU的消耗 image 所以即使设置KingfisherManager缓存上限也是没用的 #25 (comment) image 简单的做法就是设置frameCache缓存上限,并且缓存处理过的每一帧图

NSCache 本身会在收到 memory warning 的时候清理资源所以感觉还好,不加的话在某些场景会非常卡。后面我可以提供个开关配置下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants