Finalet / Elegant-Emoji-Picker

Aesthetic, simple, and powerful emoji picker for Swift built with UIKit.
MIT License
59 stars 15 forks source link

Memory Leak on EmojiPicker #17

Open fikricanc opened 1 year ago

fikricanc commented 1 year ago

Hi,

First of all, thank you for creating this!

However, I have noticed a memory leak when using the library. EmojiPicker never gets released from memory.

You can reproduce this issue on Demo app.

Steps to reproduce:

  1. Present the emoji picker.
  2. Monitor the memory usage of the app.
  3. Dismiss the emoji picker.
  4. Monitor the memory usage of the app again.
image

Thanks in advance!

Finalet commented 1 year ago

Hi!

Thank you for providing this. Its an annoying booboo that I'll dive deeper after my other projects. Hope its not game breaking for your app.

fikricanc commented 1 year ago

Thanks for responding! I'll try take a look at the source code and see if I can help identify the issue too. It's worth noting that I've also noticed similar memory leak problems in other emoji picker libraries.

I'll keep you updated on my findings.

Thanks again for your help!

Finalet commented 1 year ago

Thanks, any help is appreciated!

loongman commented 1 year ago

I'd like to contribute. However, this is not doable for now, due to lack of permission.

To fix it, apply 'weak' to delegate property to avoid from reference cycle.

Could you please apply this small change to the main branch? Thanks. @Finalet

On ElegantEmojiPicker.swift

public var delegate: ElegantEmojiPickerDelegate? -> public weak var delegate: ElegantEmojiPickerDelegate?

And on ElegantEmojiPickerDelegate.swift

public protocol ElegantEmojiPickerDelegate -> public protocol ElegantEmojiPickerDelegate: AnyObject

Finalet commented 1 year ago

Thanks for the input, I will look into it!

In terms of contributing:

However, this is not doable for now, due to lack of permission.

Are you unable to fork and create a pull request to the base repository? @loongman

loongman commented 1 year ago

As suggested, I have created a PR for this issue. @Finalet

loongman commented 1 year ago

The original issue should be fixed now.

However, found another quite interesting issue.

As you can see on the screen recording, after switch among different categories on the toolbar, memory increased rapidly, it stay there even after closed the picker.

After some quick investigations, it seems the OS generated huge cache(1000+ Malloc Blocks) for label.text = emoji.emoji on EmojiCell.swift. If simply replace that code line as label.text = "A", then, that behavior(1000+ Malloc Blocks) is no longer observable.

Any thoughts/suggestions on that? @Finalet

https://github.com/Finalet/Elegant-Emoji-Picker/assets/2167359/09adcb3c-f547-4b3c-b3fc-1c681f707c09

Screenshot 2023-06-03 at 21 46 30 Screenshot 2023-06-03 at 21 47 24
zizicici commented 1 year ago

Have you tried switching the app to the background? I found that it seems to reduce memory usage to a reasonable level.

loongman commented 1 year ago

Good finding. @zizicici

Memory usage can also be reduced if apply 'Debug -> Simulate Memory Warning' to Simulator.