2011-10-05 10 views
9

私はたくさんのアニメーションを持つAndroidアプリケーションを持っています。AnimationDrawableに画像が追加され、プログラムでメモリリークが発生する

私は、プログラム(AnimationDrawableを使用して)アニメーションを作成非Javaオブジェクトは、(DDMSヒープ]タブに表示される)私はロード、決して私のアニメーションを解除しますた後もバック縮小し、すべての新しいアニメーションで成長します。

私は書いたラッパーオブジェクトからそれぞれのAnimationDrawableオブジェクトへの参照が1つしかありません。このオブジェクトはfinalizeメソッドをオーバーライドして呼び出されたことを確認して解放されることを確認しました。

最終的にアンドロイドはイメージのロードを停止し、「メモリ不足」エラーをログに出力します。

興味深いのは、これは一部のデバイス(Motorola Xoom、Sony Experia)のみで、他のデバイス(Galaxy Sなど)では発生しないということです。

この問題は、私が与えたデバイスの例からわかるように、特定のハニカムまたはプリハニカムではありません。私は現在のアニメーションで行われていますが、助けていないようです後の各フレームのリサイクルを呼び出す

  1. 私が試した事のいくつか。

  2. は私がmyAnimation.addFrame(...)
+0

各デバイスの合計RAMを投稿できますか?また、あなたはどんなアニメーションについて話していますか?初期化コードの例を投稿できますか? –

+0

XOOMには1GBのRAMがあり、マニフェストに "large-heap"フラグを使用しています。ギャラクシーSには512MB – Gu1234

+0

があります。この 'myAnimation.addFrame'は意図通りに使用していない可能性があります。小さなスニペットを投稿してください。 –

答えて

0

をコメントアウトしたら、問題が消えていることを確認してください描画可能なアニメーションへの参照を保持しているクラスに関連した静的変数が存在しないことを確認することAnimationDrawbleオブジェクト

  • にnullを代入
  • これは正確な答えではなく、正確なリークがどこで発生しているかを見つけるのに役立つヒントです。メモリを再利用することを期待した後、ヒープダンプを実行し、死んだはずのオブジェクトがまだ生きている理由を確認します。

    eclipse用のメモリアナライザーツールを取得していることを確認してください。 (http://www.eclipse.org/mat/)

  • 0

    ビットマップを作成するときに最初に2つ、ビットマップをBitmapDrawableに変換するときに2つの理由が考えられます。あなたのコメント(new BitmapDrawable(currentFrameBitmap)から見ることができるようになりました。BitmapDrawable(getResources(),currentFrameBitmap)リソースリファレンスがなければ、正しくスケーリングされていても、ビットマップが正しくレンダリングされないことがあります。ビットマップを効率的にロードするには、適切にスケーリングできます。

    public class BitmapDecoderHelper { 
    private Context context; 
    public BitmapDecoderHelper(Context context){ 
        this.context = context; 
    } 
    public int calculateInSampleSize(
    BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 
    Log.d("height reqheight width reqwidth", height+"//"+reqHeight+"//"+width+"///"+reqWidth); 
    if (height > reqHeight || width > reqWidth) { 
        if (width > height) { 
         inSampleSize = Math.round((float)height/(float)reqHeight); 
        } else { 
         inSampleSize = Math.round((float)width/(float)reqWidth); 
        } 
    } 
    return inSampleSize; 
    } 
    public Bitmap decodeSampledBitmapFromResource(String filePath, 
         int reqWidth, int reqHeight) { 
    
        // First decode with inJustDecodeBounds=true to check dimensions 
        final BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inJustDecodeBounds = true; 
    
        BitmapFactory.decodeFile(filePath, options); 
        // Calculate inSampleSize 
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
        Log.d("options sample size", options.inSampleSize+"///"); 
        // Decode bitmap with inSampleSize set 
        options.inJustDecodeBounds = false; 
        // out of memory occured easily need to catch and test the things. 
        return BitmapFactory.decodeFile(filePath, options); 
    } 
    public int getPixels(int dimensions){ 
        Resources r = context.getResources(); 
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensions, r.getDisplayMetrics()); 
        return px; 
    } 
    public String getFilePath(Uri selectedImage){ 
        String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
        Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String filePath = cursor.getString(columnIndex); 
        cursor.close(); 
        return filePath; 
    } 
    
    } 
    
    関連する問題