Why is memory not freed after a lottie-ios animation view controller is dismissed?

1.2k views Asked by At

I have a view controller with AnimationView (the class of lottie-ios pod). The animation takes too much memory (about 800 MB). But the problem is that,after the view controller is dismissed, the memory is still full.

The animation view is created as following:

var mainAnimation = AnimationView(name: "my_animation_name")
1

There are 1 answers

0
Артур Пономаренко On

The problem reason was that I used the old version of lottie-ios pod – 3.1.8. In the old version, the BundleImageProvider had the static cache property

static var cache = [String: UIImage]()

So even when all instances of BundleImageProvider are deinitialized, this property is still in memory because it's static. There is no clearCache method. And the property is not public. So I can't access it directly. In version 3.1.9, they removed this property, so the problem is solved.

But if you still need to use the old version there is another solution

The init of the AnimationView has imageProvider: AnimationImageProvider argument. The AnimationImageProvider is a protocol. If the passed property is nil the BundleImageProvider is used by default. You may create your own implementation of AnimationImageProvider and use it. You may copy the implementation of the BundleImageProvider and then do one of two actions:

  1. make the cache property non-static
  2. leave the cache property static, but add the clearCache method. I prefer the first one because you won't need to manually handle cache clear.