dothem
dothem

Reputation: 1993

OutOfMemory Exception Android VectorBitmap

I'm using vectordrawable for displaying images. The images are all resources which are bundled with the app (apk). My problem is, that the memory on every new activity massivly get higher until the app crashes with an OutOffMemoryException.

java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:903)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:880)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:847)

I've looked into MAT for memory leaks, but did'nt find out any more than the bitmap errors.

What are the efficients way to display vectordrawables? Maybe the general architecture of my app isnt right with the activity lifecycle?

I didn't find any informations about the common memory ussage of other apps (facebook, aibnb, whatsapp,..). My usage is around 40-70MB.

Upvotes: 2

Views: 407

Answers (1)

Danail Alexiev
Danail Alexiev

Reputation: 7792

There are a few things to be careful about when dealing with images:

  1. Are you loading your images every time when you show them? This can be a potentially very memory consuming operation. It's better to load the images into memory once and then reuse them across the app.
  2. Are you cleaning the memory occupied by the images you no longer use? If you are sure that an image is no longer needed, you should clean the allocated bitmap memory by calling bitmap.recycle(). This invalidates the bitmap and frees all the occupied memory, making it available for other operations.
  3. Are the images too large? There is a limit for the maximum size of a single image. Trying to load a bigger image will also cause an OutOfMemoryError, even though memory can be available. In that case, you may want to optimise the image files that you are trying to load.

Go though your app and check for those potential problems one by one. Also, there are good profiling tools available for the Android platform. They can show you potential problems with the memory management, like excessive memory allocation, etc.

Good luck.

Upvotes: 1

Related Questions