私は、1 つの親 Activity と、Battle を拡張してそれらのクラスにコンテキストを渡すことによって BattleActivity の静的変数にアクセスするいくつかの子クラスを持つ複雑な Battle システムを持っています。
これはすべて正常に機能しているようですが、すべての AnimationDrawables をメモリから解放する際に問題が発生しています。戦闘全体で合計 24 個の DrawableAnimation を使用できます。これは今のところ問題ありませんが、ユーザーが新しいモンスターに遭遇するたびに、さらに 4 つの AnimationDrawables がメモリに追加され、ゆっくりと確実にアプリがメモリ不足の例外でクラッシュします。
したがって、戦闘システムを終了するとすぐに、戦闘システムが占有するすべてのメモリを解放する方法を見つける必要があります。現在、私は Sony Z2 でテストしており、ユーザーが戦闘に入るとメモリが 91mb から 230mb に増加します。戦闘が終了したら、このメモリ使用量を 91 MB に戻す必要があります。アプリが現在どのように流れているか、およびメモリを解放するために何をしようとしているのかを理解できるように、いくつかの非常に基本的なコード スニペットを追加しました。
public class Battle extends Activity
{
// I have several AnimationDrawables loaded into memory
// These are then assigned the relevent animation to a button swicthing between these animations throughout my battle
ImageButton btnChr1;
AnimationDrawable cAnimHit1;
}
//This is the use of one of those AnimationDrawables
public class Battle_Chr_Anim extends Battle
{
protected Context ctx;
private ImageButton btnChr1;
AnimationDrawable cAnimHit1;
public Battle_Chr_Anim(Context c, ImageButton _btnChr1, AnimationDrawable _cAnimHit1) {
this.ctx = c;
this.btnChr1 = _btnChr1;
this.cAnimHit1 = _cAnimHit1;
}
// Bound the ImageButton
int id = ctx.getResources().getIdentifier("idle", "drawable", ctx.getPackageName());
img_chr1.setBackgroundResource(id);
frameAnimation = (AnimationDrawable)img_chr1.getBackground();
frameAnimation.start()
// Loaded into memory ready so I can swicth them over quickly when user attacks
int ca1 = ctx.getResources().getIdentifier("attack", "drawable", ctx.getPackageName());
cAnimHit1 = (AnimationDrawable)chrHit1.getBackground();
cAnimHit1.start();
}
public class Battle_Ended extends Battle
{
protected Context ctx;
public Battle_Ended(Context c) {
this.ctx = c;
}
//This is a dialog popup when the user completes the battle closing the battle activty
void EndBattle()
{
ImageButton btnSubmit = (ImageButton)dialog.findViewById(R.id.imgBtnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
RecyleAllAnimations();
dialog.dismiss();
((Activity) ctx).finish();
}
});
}
void RecyleAllAnimations()
{
// I want all AnimationDrawables from memory here, I believe the below line removes the one currently in use, however I have no way of releasing the other animations sitting in memory.
img_chr1.setBackgroundResource(android.R.color.transparent);
System.gc();
}
}