Lottie-android: OutOfMemoryError

Created on 7 Dec 2017  路  4Comments  路  Source: airbnb/lottie-android

I use version of lottie-android library is 2.3.1锛宮y compisition is all images锛宼otal 50锛宎ll image size is 375x667px锛宮y Android device's resolution is 1080x1920px锛孖 got this log:

java.lang.OutOfMemoryError: Failed to allocate a 8294412 byte allocation with 988352 free bytes and 965KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:859)
at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
at android.graphics.Bitmap.createBitmap(Bitmap.java:751)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:627)
at com.hyzb.moudles.gift.ImageCompressHelper.adjustResolution(ImageCompressHelper.java:361)
at com.hyzb.moudles.gift.ImageCompressHelper.getBitmap(ImageCompressHelper.java:348)
at com.hyzb.moudles.gift.ImageCompressHelper.toBitmap(ImageCompressHelper.java:241)
at com.hyzb.moudles.gift.GiftManager.lambda$initLottieAnimationView$15$GiftManager(GiftManager.java:308)
at com.hyzb.moudles.gift.GiftManager$$Lambda$13.fetchBitmap(Unknown Source)
at com.airbnb.lottie.manager.ImageAssetManager.bitmapForId(ImageAssetManager.java:65)
at com.airbnb.lottie.LottieDrawable.getImageAsset(LottieDrawable.java:661)
at com.airbnb.lottie.model.layer.ImageLayer.getBitmap(ImageLayer.java:59)
at com.airbnb.lottie.model.layer.ImageLayer.drawLayer(ImageLayer.java:28)
at com.airbnb.lottie.model.layer.BaseLayer.draw(BaseLayer.java:188)
at com.airbnb.lottie.model.layer.CompositionLayer.drawLayer(CompositionLayer.java:90)
at com.airbnb.lottie.model.layer.BaseLayer.draw(BaseLayer.java:188)
at com.airbnb.lottie.LottieDrawable.draw(LottieDrawable.java:341)
at android.widget.ImageView.onDraw(ImageView.java:1244)
at android.view.View.draw(View.java:16302)
at android.view.View.buildDrawingCacheImpl(View.java:15590)
at android.view.View.buildDrawingCache(View.java:15445)
at android.view.View.draw(View.java:16065)

I think this problem cause by in a short period of time allocate many memory, but there is no enough memory to allocate, in such a case we should recycle the finished ImageLayer's bitmap:

CompositionLayer.java:

BaseLayer finishedLayer = null;
for (int i = layers.size() - 1; i >= 0 ; i--) {
  boolean nonEmptyClip = true;
  if (!newClipRect.isEmpty()) {
    nonEmptyClip = canvas.clipRect(newClipRect);
  }
  if (nonEmptyClip) {
    if (finishedLayer != null && finishedLayer instanceof ImageLayer) {
      Bitmap bitmap = ((ImageLayer)finishedLayer).getBitmap();
      if (bitmap != null && !bitmap.isRecycled()) {
        bitmap.recycle();
      }
    }
    BaseLayer layer = layers.get(i);
    layer.draw(canvas, parentMatrix, parentAlpha);
    finishedLayer = layer;
  }
}
canvas.restore();
L.endSection("CompositionLayer#draw");

}

Most helpful comment

@leleliu008 I think you may misunderstand the point of Lottie. Lottie is made to render After Effects animations that were created as vectors/shapes. You're much better off with an mp4 or gif if you're just creating a png sequence. This issue has nothing to do with Lottie and is just because your animation is 100 sequential pngs that you're loading into memory.

All 4 comments

@leleliu008 It's actually running out of memory when you try and create your own bitmap for the ImageAssetManager. How many bitmaps are you trying to load in your animation?

100

@leleliu008 I think you may misunderstand the point of Lottie. Lottie is made to render After Effects animations that were created as vectors/shapes. You're much better off with an mp4 or gif if you're just creating a png sequence. This issue has nothing to do with Lottie and is just because your animation is 100 sequential pngs that you're loading into memory.

Was this page helpful?
0 / 5 - 0 ratings