我怎样才能清除不断增长的堆?

时间:2012-02-22 07:37:17

标签: java android memory-leaks

我有一个项目作为ImageViews的演示文稿,通​​过点击动画。它被实现为具有go_in和go_out动画的ViewFlipper。每个ImageView都以XML格式显示每个演示幻灯片。应用程序适用于Android 3.0及更高版本的选项卡。堆的大小随着每次切换而增长,并且logcat看起来像(在dalvik给出错误之前:oof)......

I/dalvikvm-heap(5313): Grow heap (frag case) to 205.584MB for 3850256-byte allocation
D/dalvikvm(5313): GC_CONCURRENT freed 0K, 1% free 210440K/210759K, paused 2ms+9ms
I/MediaPlayer(5313): prepareAsync called in state 4
D/dalvikvm(5313): GC_FOR_ALLOC freed 166K, 1% free 210478K/210887K, paused 29ms
I/dalvikvm-heap(5313): Grow heap (frag case) to 209.293MB for 3850256-byte allocation
D/dalvikvm(5313): GC_FOR_ALLOC freed <1K, 1% free 214238K/214663K, paused 33ms
D/dalvikvm(5313): GC_CONCURRENT freed <1K, 1% free 214238K/214663K, paused 3ms+10ms
D/dalvikvm(5313): GC_FOR_ALLOC freed 4K, 1% free 214838K/215303K, paused 32ms
I/dalvikvm-heap(5313): Grow heap (frag case) to 213.551MB for 3850256-byte allocation
D/dalvikvm(5313): GC_FOR_ALLOC freed 0K, 1% free 218598K/219079K, paused 33ms
D/dalvikvm(5313): GC_CONCURRENT freed <1K, 1% free 218598K/219079K, paused 1ms+10ms
I/MediaPlayer(5313): prepareAsync called in state 4
D/dalvikvm(5313): GC_FOR_ALLOC freed 7K, 1% free 220438K/220743K, paused 29ms
D/dalvikvm(5313): GC_FOR_ALLOC freed 2K, 1% free 222253K/222535K, paused 29ms
D/dalvikvm(5313): GC_FOR_ALLOC freed <1K, 1% free 222634K/222855K, paused 33ms
I/dalvikvm-heap(5313): Grow heap (frag case) to 221.165MB for 3850256-byte allocation 

所以,问题是 - 如何使堆空或重启应用程序成为可能?或者如何组织应用程序而不是让它增加堆?

这是我使用的代码

ImageView buttonNext_13 = (ImageView)findViewById(R.id.Button_next_13);
buttonNext_13.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view131) {
        flipper.setInAnimation(AnimationUtils.loadAnimation(
            view131.getContext(), R.anim.go_next_in));
        flipper.setOutAnimation(AnimationUtils.loadAnimation(
            view131.getContext(), R.anim.go_next_out));
        flipper.showNext();
        mp_file.release();
        mp_file = MediaPlayer.create(getApplicationContext(), R.raw.theme);
        mp_file.start();
        return;
    }
});
ImageView buttonPrevious_13 = (ImageView)findViewById(R.id.button_preious13);
buttonPrevious_13.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view132) {
        flipper.setInAnimation(AnimationUtils.loadAnimation(
            view132.getContext(), R.anim.go_prev_in));
        flipper.setOutAnimation(AnimationUtils.loadAnimation(
            view132.getContext(), R.anim.go_prev_out));
        flipper.showPrevious();
        return;
    }
});

3 个答案:

答案 0 :(得分:3)

请使用以下代码,

BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither=false;                     //Disable Dithering mode
bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024]; 
CameraTricks.SdCardImage= BitmapFactory.decodeFile(CameraTricks.yu,bfOptions);

CameraTricks.yu是我的位图路径

答案 1 :(得分:2)

好像你在泄漏记忆。所以解决方案是停止这样做。这里有困难的部分:实际上是在阻止泄漏记忆。随着视图切换期间内存的增长,最可能的原因是您在启动和停止视图时没有正确处理引用。显示更多代码,然后我们将能够帮助您

答案 2 :(得分:0)

当图像不可见时,使用drawable.setCallback(null);

将其从内存中删除

您可以将其从ImageView imageView.getDrawable().setCallback(null);

中删除