Akshay Bhat 'AB'
Akshay Bhat 'AB'

Reputation: 2700

How to deallocate/free memory in android game

I created a 2d game for android using Bitmaps. After closing the game, android studio memory section shows that the memory is not de-allocated. So how can i de-allocate memory which is allocated for bitmaps. I have a Bitmap array called chopper and i am initializing like

    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 during 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 it the way to free memory? If not how can i free the memory before closing the game. Thanks in advance

Upvotes: 3

Views: 1127

Answers (2)

dasar
dasar

Reputation: 5361

You have to call bitmap.recycle() API to actually free memory. See official article about bitmaps memory management

Upvotes: 0

Arnab Bhagabati
Arnab Bhagabati

Reputation: 2725

In Java, making a object null will not release the memory. The memory might be released by the garbage collector when the garbage collector runs next time if are no references to that object anywhere.

In your case, assuming you are using this array in a activity, close all the resources that might be using this array in the onDestroy() method of your activity, and ensure there are no objects floating around that refers this object after the Activity is destroyed.

Upvotes: 1

Related Questions