0

I have two many activities in my app. An in these activities I an using some canvas, paint , bitmaps , images and also images.

Now When I navigate to next activity I see there is a great change and increase in the memory when I analyzed it from android studio memory analyzer.

I got my app crash near 400mb of memory. I know this is too much memory My app is using. But As I am newbie into the android so I do not know how to control the heap and memory to save application to get run out memory.

So here are my questions?

  1. How to do the memory analyzing ?

  2. I am setting every class member to null in ondestroy then Why does there is increase in the memory ?

  3. How can I force the Garbage collection. I have read that this is not the good practice? then What should I dot to release the resources??

Edit 1: Posting code of How I am doing frame by frame animation.

this is my AnimationDrawbaleCallback.java class

public abstract class AnimationDrawableCallback implements Drawable.Callback {


private Drawable mLastFrame;


private Drawable.Callback mWrappedCallback;

/**
 * Flag to ensure that {@link #onAnimationComplete()} is called only once, since
 * {@link #invalidateDrawable(Drawable)} may be called multiple times.
 */
private boolean mIsCallbackTriggered = false;


public AnimationDrawableCallback(AnimationDrawable animationDrawable, Drawable.Callback callback) {
    mLastFrame = animationDrawable.getFrame(animationDrawable.getNumberOfFrames() - 1);
    mWrappedCallback = callback;
}

@Override
public void invalidateDrawable(Drawable who) {
    if (mWrappedCallback != null) {
        mWrappedCallback.invalidateDrawable(who);
    }

    if (!mIsCallbackTriggered && mLastFrame != null && mLastFrame.equals(who.getCurrent())) {
        mIsCallbackTriggered = true;
        onAnimationComplete();
    }
}

@Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
    if (mWrappedCallback != null) {
        mWrappedCallback.scheduleDrawable(who, what, when);
    }
}

@Override
public void unscheduleDrawable(Drawable who, Runnable what) {
    if (mWrappedCallback != null) {
        mWrappedCallback.unscheduleDrawable(who, what);
    }
}

//
// Public methods.
//


public abstract void onAnimationComplete();

}

This is how I am making my animation-list in drawable

<?xml version="1.0" encoding="utf-8"?>

<item
    android:drawable="@drawable/rolling_1"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_2"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_3"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_4"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_5"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_6"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_7"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_8"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_9"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_10"
    android:duration="300" />
<item
    android:drawable="@drawable/rolling_11"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_12"
    android:duration="300" />
<item
    android:drawable="@drawable/rolling_13"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_14"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_15"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_16"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_17"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_18"
    android:duration="300" />

<item
    android:drawable="@drawable/rolling_19"
    android:duration="300" />

As I am showing animation in the Dialog here is my Dialog class

 public class AnimationDialog extends Dialog implements
        android.view.View.OnClickListener {


    public Activity c;
    public Dialog d;
//    public Button yes, no;
    ImageView mImageView;
    int mDrawable;
    AnimationDrawable countdownAnimation;

    public AnimationDialog(Activity a, int drawable) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
        this.mDrawable = drawable;
        getWindow().setWindowAnimations(R.style.DialogAnimation);
       // AnimationDialog.this.getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnim;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        getWindow().setLayout(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.TOP);


        mImageView = (ImageView) findViewById(R.id.iv_animation);

        mImageView.setBackgroundResource(mDrawable);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //Do something after 100ms
                ((AnimationDrawable) mImageView.getBackground()).start();
                countdownAnimation = (AnimationDrawable) mImageView.getBackground();
                countdownAnimation.setCallback(new AnimationDrawableCallback(countdownAnimation, mImageView) {
                    @Override
                    public void onAnimationComplete() {
                        // TODO Do something.
//                        Toast.makeText(c, "Animation Ended", Toast.LENGTH_SHORT).show();
                        TextView tv = (TextView) findViewById(R.id.tv);
                        Button btn = (Button) findViewById(R.id.close);
                        RelativeLayout rl = (RelativeLayout) findViewById(R.id.secondry_frame);
                        final Animation animationFalling = AnimationUtils.loadAnimation(c, R.anim.anim_falling);
                        rl.startAnimation(animationFalling);
                        tv.setVisibility(View.VISIBLE);
                        btn.setVisibility(View.VISIBLE);
                        btn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                AnimationDialog.this.dismiss();
                                countdownAnimation = null;

                            }
                        });
                    }
                });
                countdownAnimation.start();


            }
        }, 1500);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
        dismiss();
    }

}

and here is how I am using it in different Activities

AnimationDialog animationDialog;
animationDialog = new AnimationDialog(Qaaf.this, R.drawable.qaaf_animation);
                        animationDialog.show();
//and setting animationDialog to null in onDestroy of Activity 

Please guide me and share me what can I do to improve the app and How can I minimize the memory usage?

Allay Khalil
  • 674
  • 3
  • 11
  • 31

0 Answers0