我的应用中有两个很多活动。在这些活动中,我使用了一些画布,颜料,位图,图像和图像。
现在当我导航到下一个活动时,我发现当我从android studio内存分析器分析它时,内存有很大的变化和增加。
我的应用程序崩溃了400mb的内存。我知道这是我的应用程序正在使用的太多内存。但是因为我是新手进入android所以我不知道如何控制堆和内存来保存应用程序以耗尽内存。
所以这是我的问题?
如何进行内存分析?
我在ondestroy中将每个类成员设置为null然后为什么内存会增加?
如何强制垃圾收集。我读过这不是好习惯吗?那我应该点什么来释放资源?
编辑1:发布我如何逐帧动画的代码。
这是我的 AnimationDrawbaleCallback.java 类
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();
}
这就是我在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" />
当我在Dialog中显示动画时,这是我的Dialog类
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();
}
}
以下是我在不同活动中使用它的方法
AnimationDialog animationDialog;
animationDialog = new AnimationDialog(Qaaf.this, R.drawable.qaaf_animation);
animationDialog.show();
//and setting animationDialog to null in onDestroy of Activity
请指导我并分享我如何改进应用程序以及如何最大限度地减少内存使用?