How to free / free up memory in android game
I have created a 2d game for android using bitmaps. After closing the game, the Android studio memory section shows that the memory is not de-allocated. So how can I allocate memory allocation that is allocated for bitmaps. I have a bitmap array named chopper
and I initialize as
chopper[0] = BitmapFactory.decodeResource(getResources(), R.drawable.copter1); chopper[1] = BitmapFactory.decodeResource(getResources(), R.drawable.copter2); chopper[2] = BitmapFactory.decodeResource(getResources(), R.drawable.copter3);
So, while closing the game, I tried to free memory like
chopper[0]=null;
But it doesn't show free memory in android studio memory graph. Is this a way to free up memory? If not, how can I free memory before closing the game. thanks in advance
source to share
In Java, creating a null object will not free memory. The memory can be reclaimed by the garbage collector the next time the garbage collector runs, if there are no references to that object anywhere.
In your case, assuming you are using this array in an activity, close all resources that might be using this array in onDestroy()
your activity's method , and make sure there are no objects around the object that reference this object after the activity is destroyed.
source to share
You have to call the API bitmap.recycle()
to actually free the memory. See the official article on Memory Management Bitmap
source to share