9

私は、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();
    }
}
4

2 に答える 2

0

ドローアブルはステートフルであるため、静的なドローアブルまたはビューを使用するとメモリ リークが発生します。

他の静的な Drawable(s) または View(s) をチェックしてそれらを取り除くか、呼び出すことができます

drawable.setCallback(null);

Drawable オブジェクトをリサイクルすることはできず、System.gc() を呼び出すことはお勧めできません。

アプリのどこかでメモリ リークが発生しているようです。上記を試してもメモリがその割合で拡張する場合は、メモリ分析を行って問題を絞り込むことを検討してください。

于 2014-11-13T10:52:49.427 に答える
0

まず第一にAnimationDrawable、それらが宣言されている範囲を離れない限り、リサイクルを行うことはできないと思います. 中継せずSystem.gc();、GC が自動的にそれらのメモリを解放するようにします。

BattleActivity の静的変数にアクセスする

これはあなたの最初の間違いです。アクティビティを離れても静的変数を使用すると、それらはすべてそこにあり、間違いなくOOMになります。

したがって、戦闘システムを終了するとすぐに、戦闘システムが占有するすべてのメモリを解放する方法を見つける必要があります。

すべての変数を非静的変数として宣言すると、それらが定義されているスコープを離れるときに OOM を取得できません。AnimationDrawableただし、アクティビティの実行中に境界なしで作成すると、OOM が発生する可能性があります。

于 2014-11-08T03:59:58.180 に答える