清除堆内存以获取内存不足异常

Bal*_* IT 8 memory android garbage-collection

我知道有关内存不足的问题,但我找不到任何解决方案

在Bitmap Factory中我得到了内存异常,甚至使用

inSampleSize=1
Run Code Online (Sandbox Code Playgroud)

所以我习惯用try catch out of memory of exception来包围它,因为这是一个不好的做法

try{
   .........
   ......
}catch (OutOfMemoryError e)
            {}
Run Code Online (Sandbox Code Playgroud)

内存不足异常也被捕获但我的问题是我们抓住了这个异常之后

清除或重新分配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.
Run Code Online (Sandbox Code Playgroud)

Bal*_* IT 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"
                     />
Run Code Online (Sandbox Code Playgroud)

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

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

}
Run Code Online (Sandbox Code Playgroud)

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

将堆(frag case)增长到11.719MB,用于8192016字节分配