8

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.
Bald bcs of IT
  • 892
  • 6
  • 14
  • Just search for "android out of memory", you will find many questions with every possible solution. – Simon Aug 31 '13 at 08:03
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Simon Aug 31 '13 at 08:03
  • @Simon ya i seen that , i also use to do that code but the image which is use to display will be blurred not good to see – Bald bcs of IT Aug 31 '13 at 08:08
  • see accepted ans- http://stackoverflow.com/questions/17990086/out-of-memory-while-creating-bitmaps-on-device/17990482#17990482 – T_V Aug 31 '13 at 08:37
  • http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Muhammad Babar Aug 31 '13 at 09:58

1 Answers1

11

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

Bald bcs of IT
  • 892
  • 6
  • 14
  • Hi, I change my code to exactly what you said but the problem is onDetachedFromWindow() method never calls in activities while calls when I'm replacing fragments! So, my app still crashing on activities. – Hesam Mar 06 '14 at 02:27
  • Oh GOD , I need It , Some Nights Didn't Slept To Find Solution , onDetachedFromWindow method Clean Custom ImageView – Hossein Kurd Feb 22 '15 at 22:40