> 
> **Possible Duplicate:**  
> 
> [Bitmap recycle with largeHeap enabled](https://stackoverflow.com/questions/12716574/bitmap-recycle-with-largeheap-enabled) 
> 
> 
> 

hi am doing one app here i am displying images using bitmaps,that images is displaying well,but becz this class i am getting out of memory exception in another class.pls see once my code and how to solve this issue any suhhest me...

```
 public class DesignofatozActivity extends Activity {

BitmapDrawable sounddrawable,erasedrawable,backdrwable,fwddrwable,captiondrwable;
Bitmap soundbitmap,eraseBitmap,backbitmap,fwdbitmap,captionbitmap;
    bitmapOrg,bitmapOrg1,bitmapOrg2,bitmapOrg3,bitmapOrg4,bitmapOrg5,bitmapOrg6;
MyView myview;
File f;
ImageView d1,d2,d3,d4,d5,d6;
public  boolean action=false;
RelativeLayout relativeLayout,layout,relativeLayout2;

 Button c1,c2,c3,c4,c5,c6,c7,c8; 
 MediaPlayer player;
 MediaPlayer mediay2;
 ImageView horn;
 float screenHeight,screenWidth,screendensity;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setNoTitle();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
         screenHeight = displaymetrics.heightPixels;
         screenWidth = displaymetrics.widthPixels;
         screendensity = displaymetrics.densityDpi;
        Log.i("screenHeight",""+screenHeight);
        Log.i("screenWidth",""+screenWidth);
        Log.i("screendensity",""+screendensity);

         setContentView(R.layout.line);
         relativeLayout=(RelativeLayout)findViewById(R.id.relative);

      relativeLayout.setBackgroundColor(Color.WHITE);

      relativeLayout2=(RelativeLayout)findViewById(R.id.relative2);

      RelativeLayout.LayoutParams layoutrel2= (RelativeLayout.LayoutParams)  relativeLayout2.getLayoutParams();    
      layoutrel2.height=(int)(25*(screenHeight/600));  

   int toplay=(int)(90*(screenHeight/600));

      layout=new RelativeLayout(this);
      RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      lp.setMargins(0, toplay, 0,0);

      layout.setLayoutParams(lp);
      relativeLayout.addView(layout,lp); 

         int topmyvi=(int)(45*(screenHeight/600));
        myview = new MyView(this);
             myview.setId(004);
          RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(
         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          myview.setLayoutParams(lp6);

    lp6.setMargins(0, topmyvi, 0,0);
    relativeLayout.addView(myview,lp6);

    ImageView I2=new ImageView(this);

    if(captiondrwable!= null) {
        captionbitmap.recycle();
           captiondrwable= null;
            }

           captionbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.caption22_1));
     captiondrwable = new BitmapDrawable(captionbitmap); 
       I2.setBackgroundDrawable(captiondrwable);

    int left2=(int)(15*(screenWidth/1024));

    int widthhh2=(int)(100*(screenWidth/1024));
    int hifhtttt2=(int)(50*(screenHeight/600));

       RelativeLayout.LayoutParams rlp2=new RelativeLayout.LayoutParams(widthhh2,hifhtttt2);

    rlp2.setMargins(left2, topmyvi, 0, 0);
    relativeLayout.addView(I2,rlp2);

    ImageView b1=new ImageView(this);
    if(erasedrawable!= null) {
         eraseBitmap.recycle();
        erasedrawable= null;
         }
      eraseBitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.eraser_501));
   erasedrawable = new BitmapDrawable(eraseBitmap); 
   b1.setBackgroundDrawable(erasedrawable);

    int leftb1=(int)(650*(screenWidth/1024));
   int topb1=(int)(10*(screenHeight/600));
 int widtb1=(int)(100*(screenWidth/1024));
 int hightb1=(int)(100*(screenHeight/600));

 RelativeLayout.LayoutParams rlp3=new RelativeLayout.LayoutParams(widtb1,hightb1);
 rlp3.setMargins(leftb1, topb1, 0, 0);
 relativeLayout.addView(b1,rlp3);

            b1.setOnClickListener(new OnClickListener() {

  //        @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         try{

                mBitmap.eraseColor(android.graphics.Color.TRANSPARENT);
                Canvas Canvas=new Canvas(mBitmap);

                action=true;
            myview.onDraw(Canvas);

                }catch(IllegalStateException ie){
                    ie.printStackTrace();
                }
    }
});

  int lefth1=(int)(830*(screenWidth/1024));
  int toph1=(int)(35*(screenHeight/600));
int width1=(int)(60*(screenWidth/1024));
int highth1=(int)(60*(screenHeight/600));
horn=new ImageView(this);

if(sounddrawable!= null) {
     soundbitmap.recycle();
    sounddrawable= null;
      }
soundbitmap=BitmapFactory.decodeStream(getResources().openRawResource( R.drawable.horn));
sounddrawable = new BitmapDrawable(soundbitmap); 
horn.setBackgroundDrawable(sounddrawable);

  RelativeLayout.LayoutParams hn=new RelativeLayout.LayoutParams(width1,highth1);
  hn.setMargins(lefth1,toph1,0,0);
  relativeLayout.addView(horn,hn);

  horn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mediay2==null)
            {

             mediay2 = MediaPlayer.create(DesignofatozActivity.this, R.raw.hwprepsheets);

            }
             mediay2.start();
        if(mediay2!=null)
        {

        }

        }

    });

  ImageView next=(ImageView)findViewById(R.id.imv1a);

  if(fwddrwable!= null) {
   fwdbitmap.recycle();
       fwddrwable= null;
            }

  fwdbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.next_50));
      fwddrwable = new BitmapDrawable(fwdbitmap); 
      next.setBackgroundDrawable(fwddrwable);
  RelativeLayout.LayoutParams layoutnxt= (RelativeLayout.LayoutParams) next.getLayoutParams();     
  layoutnxt.height=(int)(30*(screenHeight/600));  
  layoutnxt.width=(int)(50*(screenWidth/1024));
  next.setOnClickListener(new OnClickListener() {

 //         @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Log.i("next","next");
            Intent i =new Intent(DesignofatozActivity.this,Dots.class);
            startActivity(i);
        }   
    });
  ImageView back=(ImageView)findViewById(R.id.back);

  if(backdrwable!= null) {
   backbitmap.recycle();
   backdrwable= null;
       }

  backbitmap=BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.back1_50));
  backdrwable = new BitmapDrawable(backbitmap); 
  back.setBackgroundDrawable(backdrwable);
  RelativeLayout.LayoutParams layoutbak= (RelativeLayout.LayoutParams) back.getLayoutParams();     
  layoutbak.height=(int)(30*(screenHeight/600));  
  layoutbak.width=(int)(50*(screenWidth/1024));
  back.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent i =new Intent(DesignofatozActivity.this,Slantlines.class);
        startActivity(i);
    }
});

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    //mPaint.setColor(0xFFFF0000);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(6);

 }

private void setNoTitle() {
    // TODO Auto-generated method stub
    requestWindowFeature(Window.FEATURE_NO_TITLE);
}
 private Paint       mPaint;
  private Bitmap  mBitmap;

    public void colorChanged(int color) {
        mPaint.setColor(color);
    }

    public class MyView extends View
    {

            private Canvas  mCanvas;
            private Path    mPath;
            private Paint   mBitmapPaint;

        public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
             mPath = new Path();
                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }
         @Override
            protected void onSizeChanged(int w, int h, int oldw, int oldh)       {
                super.onSizeChanged(w, h, oldw, oldh);
                if (mBitmap != null) {
                    mBitmap.recycle();
                    mBitmap=null;
                 }

                mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                mCanvas = new Canvas(mBitmap);
                mCanvas.setBitmap(mBitmap);

            }

          @Override
            protected void onDraw(Canvas canvas) {

              if(action)
              {
                  invalidate();
              }

              Paint painto = new Paint();
               painto.setAntiAlias(true);

               painto.setStrokeWidth(3);
               painto.setStyle(Paint.Style.FILL);

              int leftx1=(int)(15*(screenWidth/1024));
                 int leftx2=(int)(1010*(screenWidth/1024));
                    int topy1=(int)(60*(screenHeight/600));
                    int topy2=(int)(530*(screenHeight/600));

                       canvas.drawLine(leftx1, topy1, leftx2, topy1, painto);   
                       canvas.drawLine(leftx1, topy1, leftx1, topy2, painto);
                       canvas.drawLine(15, topy2, leftx2, topy2, painto);
                       canvas.drawLine(leftx2, topy1, leftx2, topy2, painto);

            bitmapOrg = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_4);
            int leftorg=(int)(150*(screenWidth/1024));
            int toporg=(int)(110*(screenHeight/600));
              canvas.drawBitmap(bitmapOrg, leftorg, toporg, painto);

              bitmapOrg.recycle();
              bitmapOrg=null;

              bitmapOrg1 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_5);
              int leftorg1=(int)(430*(screenWidth/1024));
                int toporg1=(int)(130*(screenHeight/600));
               canvas.drawBitmap(bitmapOrg1, leftorg1,toporg1, painto);
               bitmapOrg1.recycle();
                  bitmapOrg1=null;

       bitmapOrg2 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_6);
       int leftorg2=(int)(650*(screenWidth/1024));
               canvas.drawBitmap(bitmapOrg2, leftorg2,toporg, painto);

               bitmapOrg2.recycle();
                  bitmapOrg2=null;
         bitmapOrg3 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_1);
         int leftorg3=(int)(170*(screenWidth/1024));
         int toporg3=(int)(350*(screenHeight/600));
               canvas.drawBitmap(bitmapOrg3, leftorg3,toporg3, painto);
               bitmapOrg3.recycle();
                  bitmapOrg3=null;
              bitmapOrg4 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_3);
              int leftorg4=(int)(680*(screenWidth/1024));
               canvas.drawBitmap(bitmapOrg4, leftorg4,toporg3, painto);

                  bitmapOrg4.recycle();
                  bitmapOrg4=null;
            bitmapOrg5 = BitmapFactory.decodeResource(getResources(),
                        R.drawable.circles1_2);

            int leftorg5=(int)(400*(screenWidth/1024));
             int toporg5=(int)(300*(screenHeight/600));
               canvas.drawBitmap(bitmapOrg5, leftorg5,toporg5, painto);

               bitmapOrg5.recycle();
                  bitmapOrg5=null;
               Paint paint1 = new Paint();
                paint1.setAntiAlias(true);
                paint1.setColor(Color.BLACK);
                paint1.setStrokeWidth(3);
                paint1.setStyle(Paint.Style.FILL);
                paint1.setTextSize(13);
                canvas.drawText("Get  ready  to  write  place  your  pen  on  the  dot  and  follow  direction ", 120, 20, paint1);
                canvas.drawText("indicated  by  the  arrow . ", 120, 38, paint1);

                Paint p = new Paint();
                p.setAntiAlias(true);
                p.setTextSize(120);
                p.setColor(Color.LTGRAY);
                Typeface font = Typeface.createFromAsset(getAssets(), "font/KINDTRG.TTF"); 

                p.setTypeface(font);            

               canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

                canvas.drawPath(mPath, mPaint); 

       }  private float mX, mY;
        private  final float TOUCH_TOLERANCE = 2;

     private void touch_start(float x, float y) {
      mPath.reset();
        mPath.moveTo(x, y);
            mX = x;
     mY = y;
     }
     private void touch_move(float x, float y) {
     float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
   if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
           mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
       mX = x;
        mY = y;
     }
  }
    private void touch_up() {
        mPath.lineTo(mX, mY);
         // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
 // kill this so we don't double draw
      mPath.reset();
      }

   @Override
   public boolean onTouchEvent(MotionEvent event) {
         float x = event.getX();
         y = event.getY();

  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
      touch_start(x, y);
      invalidate();
      break;
  case MotionEvent.ACTION_MOVE:
      touch_move(x, y);
      invalidate();
      break;
  case MotionEvent.ACTION_UP:
      touch_up();
      invalidate();
      break;
  }
   return true;
  }

   }
    public void clearAllResources() {

        // Set related variables null

        System.gc();
        Runtime.getRuntime().gc();
       }

      @Override
        protected void onPause() {

            if (mediay2 != null){
                mediay2.stop();
                mediay2.release();
                mediay2=null;
          }

            clearAllResources();
            super.onPause();
        }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        System.gc();
        Runtime.getRuntime().gc(); 
        unbindDrawables(findViewById(R.id.relative));
    }

        private void unbindDrawables(View view) {
        // TODO Auto-generated method stub
         if (view.getBackground() != null) {
             view.getBackground().setCallback(null);
         }
         if (view instanceof ViewGroup) {
             for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                 unbindDrawables(((ViewGroup) view).getChildAt(i));
             }
             ((ViewGroup) view).removeAllViews();
         }
        }
        @Override
            protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();

            }

    }

```