Reputation: 2655
I've been trying to solve OutOfMemoryError
Crashes for a while now.
The Problem
I know where the memory use is coming from :
- My App uses the same background image on every activity
- I use the MPAndroidChart
Library for charts
- Other icons and bitmaps from our graphic designers.
I've looked at the memory use view in Android Studio. When I go from Activity A to activity B, memory goes from (example) 40 to 60 mb Then when I go back to activity A from activity B, it goes up again from 60 to 80
The Question (s)
How can I clear the memory taken up by the bitmaps used in Activity A when I go to B ? (Assuming, based on observations of the Memory graph, that it keeps them in memory when going A->B)
Because all activities use the same image on the background, how can I stop the app from loading it into memory again in other activities, and just make it re-use it ? (assuming it doesn't)
Update
I've Already read Android's developer documentation for efficiently loading bitmaps :
https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
But it doesn't say how to remove images from memory when leaving an activity
Upvotes: 2
Views: 259
Reputation: 225
I am kind of new to Android, but this is how I fixed my OutOfMemory:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
BitmapDrawable background = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.image, options));
background.setTileModeX(android.graphics.Shader.TileMode.REPEAT);
bar.setBackgroundDrawable(background);
I found this somewhere, but I don't remember where.
Upvotes: 2