2013-08-31 85 views
8

我知道就出内存已经提出这样的问题,但是我发现没有办法解决清除堆内存

位图厂我离开了内存异常,甚至使用

inSampleSize=1 

,所以我用它来包围它通过尝​​试捕捉内存溢出异常,因为这是一个不好的做法

try{ 
    ......... 
    ...... 
}catch (OutOfMemoryError e) 
      {} 

内存溢出异常也引起了 但我questio n被抓住了这个异常后,应我们

清除或GC的重新分配堆内存

有没有什么解决办法吗?

我使用

的System.gc();

没有用 请帮助!!!!!!!

not even Bitmap also for GridView Orientation 
i found this exception 
Clamp target GC heap from 17.333MB to 16.000MB 
Out of memory on a 140416-byte allocation. 
+0

只要搜索“机器人内存不足”,你将用各种可能的解决方案找到许多问题。 – Simon

+0

[将图像加载到位图对象时出现内存不足问题]的可能重复(http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-对于位图对象) – Simon

+0

@Simon遐我看到了,我也用它来做代码,但用来显示的图像会模糊不清,看不到 –

回答

11

后奋斗了3天,我发现不堆内存增加的解决方案通过使用 此

我代替我所有的ImageView这样

<com.example.util.SingleShotImageView 
        android:id="@+id/grid_image" 
        android:layout_width="170dp" 
        android:layout_height="240dp" 
        android:adjustViewBounds="true" 
        android:layout_centerInParent="true" 
        /> 

使用这个类我用来清除onDetachedFromWindow函数中的图像位图堆大小

public class SingleShotImageView extends ImageView { 

    public SingleShotImageView(Context context) { 
     super(context); 
    } 

    public SingleShotImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public SingleShotImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onDetachedFromWindow() { 
     setImageDrawable(null); 
     setBackgroundDrawable(null); 
     setImageBitmap(null); 
     System.gc(); 
    } 

} 

现在它工作正常,我的堆内存仍然

成长堆(断枝情况下),以11.719MB为8192016字节分配

+0

嗨,我将我的代码更改为您所说的内容,但问题出在onDetachedFromWindow()方法从不在调用活动时调用,而是在更换片段时调用!所以,我的应用程序仍然崩溃的活动。 – Hesam

+0

好的解决方案! – Ganesh

+0

哦,上帝,我需要它,一些晚上没有睡觉找到解决方案,onDetachedFromWindow方法清理自定义ImageView – AndroSco