airbnb / lottie-ios

An iOS library to natively render After Effects vector animations
http://airbnb.io/lottie/
Apache License 2.0
25.76k stars 3.75k forks source link

Issue: LottieAnimationView Not Clearing from Memory #2143

Closed tudor-alexa99 closed 1 year ago

tudor-alexa99 commented 1 year ago

Description: I've encountered a memory management issue with LottieAnimationView in my iOS app. Despite removing the view from its superview and setting it to nil, the view doesn't seem to be releasing its memory footprint as expected. This results in unnecessary memory consumption over time, potentially leading to performance degradation and even app crashes.

Steps to Reproduce:

  1. Create a LottieAnimationView instance and add it as a subview to a parent view.
  2. Later, remove the LottieAnimationView from its superview and set it to nil.
  3. Observe memory usage using Xcode's Instruments or other memory profiling tools.

Additionally, the same behaviour is present if you create a new instance of LottieAnimationView inside of your storyboard or xib file and reference it using @IBOutlet

Expected Behavior: When a LottieAnimationView is removed from its superview and set to nil, it should release its memory and not contribute to memory leaks or excessive memory consumption.

Actual Behavior: The LottieAnimationView instance remains in memory, causing memory usage to accumulate over time.

https://github.com/airbnb/lottie-ios/assets/47315066/49458c48-69a5-4540-a4e7-0867e5384e57

Environment:

Additional Information: I've tried various methods to clear the LottieAnimationView instance from memory, including removing it from its superview, setting it to nil, using the clearCache() method and explicitly calling any relevant cleanup methods. Despite these efforts, the memory usage remains high. This issue is impacting the app's performance and stability, and I'm seeking guidance on how to effectively clear LottieAnimationView instances from memory.

I appreciate any assistance or insights into resolving this memory management issue.

calda commented 1 year ago

Could you please share a sample Xcode project that demonstrates this issue? Thanks!

tudor-alexa99 commented 1 year ago

Sure thing!

// usage option 1: instantiate the view in the xib file and create a reference to it 
class MyViewController: UIViewController {
    // please notice that setting the Animation Name was done in the Attributes Inspector 
    @IBOutlet weak var myAnimationView: LottieAnimationView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Set the background color of the animation view
        self.myAnimationView.backgroundColor = .clear
        // Start playing the animation
        self.startMyAnimation()
    }

    // Start the animation with specific loop mode and speed
    private func startMyAnimation() {
        self.myAnimationView.loopMode = .loop
        self.myrAnimationView.animationSpeed = 3.0
        self.myAnimationView.play()
    }

    // Stop the animation
    private func stopMyAnimation() {
        self.myAnimationView.stop()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // Stop the animation
        myAnimationView.stop()
        // Remove the animation view from its superview
        myAnimationView.removeFromSuperview()
        // Clear the animation cache
        LottieAnimationCache.shared?.clearCache()
        // Set the animation view reference to nil
        myAnimationView = nil
    }
}

This is the most common usage of Lottie Animation Views inside our project, and it results in the memory consumption mentioned above.

An implementation similar to the one mention in this article has also been tried, where the LottieAnimationView has been wrapped inside a Parent View and then removed from the superview, with both the animation view and parent view being further set to nil, but to no better result in terms of memory consumption.

tudor-alexa99 commented 1 year ago

Could you please let me know whether the provided sample was enough to reproduce the issue, or is additional code required?

calda commented 1 year ago

Could you please share a full Xcode project that builds and runs successfully, with repro steps that demonstrate the issue? As-is i'm not sure how to build/run the sample code you shared and repro the issue.

github-actions[bot] commented 1 year ago

This issue is stale because it is marked "can't reproduce" and has had no activity in the past week. Please comment with additional information, or this issue will be closed due to inactivity in one week.