清除堆内存的内存溢出异常 [英] Clearing heap memory for Out of Memory Exception

查看:202
本文介绍了清除堆内存的内存溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道就出内存已经被问这个问题,但我没有发现任何解决方案

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

  inSampleSize = 1
 

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

 尝试{
   .........
   ......
}赶上(OutOfMemoryError异常E)
            {}
 

内存不够的异常也引起了  但我的问题是后抓住了这个例外的我们应

  

结算或GC的重新分配堆内存

有没有什么解决办法吗?

我用

  

的System.gc();

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

 甚至没有位图也为GridView的方向
我发现这个异常
钳位目标GC堆从17.333MB为16.000MB
超出内存在140416字节分配。
 

解决方案

在奋斗3天我发现不增加堆内存的解决方案通过使用 这种

我代替我所有的ImageView这样

 < com.example.util.SingleShotImageView
                    机器人:ID =@ + ID / grid_image
                    机器人:layout_width =170dp
                    机器人:layout_height =240dp
                    机器人:adjustViewBounds =真
                    机器人:layout_centerInParent =真
                     />
 

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

 公共类SingleShotImageView扩展ImageView的{

    公共SingleShotImageView(上下文的背景下){
        超(上下文);
    }

    公共SingleShotImageView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公共SingleShotImageView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }

    @覆盖
    保护无效onDetachedFromWindow(){
        setImageDrawable(空);
        setBackgroundDrawable(空);
        setImageBitmap(空);
        System.gc()的;
    }

}
 

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

  

成长堆(破片的情况下),以11.719MB为8192016字节分配

I Know the question regarding out of memory already been asked ,but i found no solution

In Bitmap Factory i got out of memory Exception, even use

inSampleSize=1

so i use to surrounded it by try catch out of memory exception since it is a bad practice

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

out of memory exception also caught but my question is after caught of this exception shall we

Clearing or reallocating heap memory of GC

is there any solution ?

i use

System.gc();

no use please help!!!!!!!

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.

解决方案

After 3 days of struggle i found a solution for not increasing Heap memory by using this

i replace all my ImageView like this

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

Using this class i use to clear the Image bitmap heap size in onDetachedFromWindow function

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();
    }

}

now it works fine and my heap memory remains

Grow heap (frag case) to 11.719MB for 8192016-byte allocation

这篇关于清除堆内存的内存溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆