IDN Media Timotius Leonardo Lianoto Jun 7, 2023

Advanced Lottie Animation Memory Management in iOS

Article Summary

Timotius Leonardo Lianoto from IDN Media discovered their livestream gift animations were silently eating memory with every play. Even following Lottie's official docs didn't prevent the leak.

While building a livestream gifting feature for the IDN App, the iOS team hit a critical memory management issue. Lottie animations weren't releasing memory after playback, despite proper cleanup code. The team profiled the app and found a surprising solution that contradicts typical iOS memory management patterns.

Key Takeaways

Critical Insight

Adding a simple UIView container between your view controller and LottieAnimationView prevents memory leaks that standard iOS cleanup methods can't fix.

The solution works, but the root cause reveals something unexpected about how Lottie manages its internal references.

About This Article

Problem

IDN Media's livestream gift feature had a memory leak. When animations played multiple times on the same ViewController, memory kept growing even after calling removeFromSuperview() and setting variables to nil.

Solution

Timotius Leonardo Lianoto's team fixed it by wrapping LottieAnimationView instances in a container UIView class. This isolated the animation's reference cycle so memory could be properly freed when the container was removed.

Impact

Memory usage dropped right after animation playback ended when using the container pattern. This prevented the excessive memory consumption that was causing app crashes during long livestream sessions.