2012-02-22 48 views
1

我有一个项目,作为一个ImageViews的演示文稿,通​​过单击它们的动画。它被视为一个带有go_in和go_out动画的ViewFlipper。每张幻灯片都以XML格式显示每个ImageView。应用程序是针对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; 
    } 
}); 

回答

2

代码看来你正在泄漏内存。所以解决办法就是停止这样做。而这里的难点在于:实际上停止泄漏内存。随着内存在视图切换期间增长,最可能的原因是您在启动和停止视图时不能正确处理引用。显示更多的代码,那么我们就可以帮您

+0

代码如下删除... – timonvlad 2012-02-22 08:13:02

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是我的路径为位图

+0

我的Imageviews以XML格式的imegeviews为特色!你说什么路径?它们位于文件夹中:@/drawable,代码如下:R.id。*** – timonvlad 2012-02-22 07:55:06

0

当图像变为不可见,从内存中删除使用drawable.setCallback(null);

你可以从ImageView这样imageView.getDrawable().setCallback(null);