Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: free up bitmap memory resources programmatically

I am having a dialog where users can freely draw inside the dialog.

The dialog is extending a view, and the drawing area is created by

bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);

However, the drawing need not to be kept, and when the user close the dialog and reopen again, his previous drawing is not needed to be kept and redraw from null.

Detailed codes:

DoodleView:

   // DoodleView constructor initializes the DoodleView
   public DoodleView(Context context, AttributeSet attrs) 
   {
      super(context, attrs); // pass context to View's constructor

      paintScreen = new Paint(); // used to display bitmap onto screen

      // set the initial display settings for the painted line
      paintLine = new Paint();
      paintLine.setAntiAlias(true); // smooth edges of drawn line
      paintLine.setColor(Color.BLACK); // default color is black
      paintLine.setStyle(Paint.Style.STROKE); // solid line
      paintLine.setStrokeWidth(25); // set the default line width
      paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
      pathMap = new HashMap<Integer, Path>();
      previousPointMap = new HashMap<Integer, Point>();
   } // end DoodleView constructor

   // Method onSizeChanged creates BitMap and Canvas after app displays
   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH)
   {
      bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
      bitmapCanvas = new Canvas(bitmap);
      bitmap.eraseColor(Color.parseColor("#80FFFFFF")); // erase the BitMap with white
   } // end method onSizeChanged

   // clear the painting
   public void recycling()
   {
       bitmap.recycle();
   }

Writing board:

public void write_board () 
{
    writing_dialog = new Dialog(Apple.this, android.R.style.Theme_Translucent_NoTitleBar);
    WindowManager.LayoutParams lp = writing_dialog.getWindow().getAttributes();
    lp.dimAmount = 0.5f;
    writing_dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    Window window = writing_dialog.getWindow();
    window.setGravity(Gravity.CENTER);

    writing_dialog.setContentView(R.layout.alert_drawing_pad);
    writing_dialog.setCancelable(true);
    writing_dialog.show();
    writing_dialog.setVolumeControlStream(AudioManager.STREAM_MUSIC);   

    doodleView = (DoodleView) writing_dialog.findViewById(R.id.doodleView);

        alert_close.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                writing_dialog.dismiss();
                doodleView.recycling();
                return;
            }
        });

Question:

I discover that after opening the dialog several times (7 to 8 times using Samsung Note2), the dialog would lag for a few second and the phone without response, and then further press again some while later, the dialog can appear again and everything is ok.

The logcat at this time reports E/OpenGLRenderer(25296): Out of memory!

and it goes on without hanging for some time.

Yet if further press out for the drawing board, it goes with a serious memory error and this time the system hangs.

FATAL EXCEPTION: main
java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
    at com.abc.abc.DoodleView.onSizeChanged(DoodleView.java:60)
    at android.view.View.sizeChange(View.java:15326)
    at android.view.View.setFrame(View.java:15290)
    at android.view.View.layout(View.java:15201)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
    at android.view.View.layout(View.java:15204)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
    at android.view.View.layout(View.java:15204)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:15204)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:15204)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:15204)
    at android.view.ViewGroup.layout(ViewGroup.java:4793)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2263)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2009)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6379)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
    at android.view.Choreographer.doCallbacks(Choreographer.java:591)
    at android.view.Choreographer.doFrame(Choreographer.java:561)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5493)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
    at dalvik.system.NativeStart.main(Native Method)

I have researched and added
bitmap.recycle(); whenever the user closes the dialog. But it still reports the same error.

Are there any way to dispose the bitmap whenever the user closes the dialog?

Thanks!

like image 367
pearmak Avatar asked Dec 21 '13 04:12

pearmak


2 Answers

First invalidate the reference that canvas or any other object has to bitmap

if (bitmapCanvas != null) {
    bitmapCanvas.setBitmap(null);
    bitmapCanvas = null;
}

Second release bitmap resources

if (bitmap != null) {
    bitmap.recycle();
    bitmap = null;
}

If needed, Third, keep the bitmap as WeakReference

WeakReference<Bitmap> bitmap;
if (bitmap != null) {
    bitmap.get().recycle();
    bitmap.clear();
    bitmap= null;
}
like image 116
Brcinho Avatar answered Oct 06 '22 01:10

Brcinho


You should call this on dismiss of your dialog

bitmap.recycle();
bitmap = null;

bitmap.recycle(); releases the native heap that is used in bitmaps. And setting it to null is to assist the GC to quickly collect your reference.

like image 42
Rizwan Avatar answered Oct 06 '22 00:10

Rizwan