2

Hi Lets say when i call Activity A to Activity B

below is my code become

     Intent i = new Intent(TemplateList.this, PictureEditor.class);
     Bundle b = new Bundle();
     b.putString("Key", "2");
     b.putString("Index", imagepathString);
     i.putExtras(b);
     v.getContext().startActivity(i);
     System.gc();
     Runtime.getRuntime().gc();
     finish();

Because of in Activity A there are so many objects (so many images) loaded so i am clearing that all objects by System.gc(); Runtime.getRuntime().gc(); will be cleared and also destroying activity so no any object will be stay allocated :)

and from Activity B i am going call to Activity A

        btnTemplate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(PictureEditor.this, TemplateList.class));
            dg.dismiss();
            if (bmp != null) {
                bmp.recycle();
                bmp = null;
            }
            System.gc();
            Runtime.getRuntime().gc();
            finish();
        }
    });

//Here also i m calling System.gc and Runtime.getRuntime.gc and destroying activity but do not know why if activity A starts than i m getting

below error

     12-19 12:44:48.769: E/AndroidRuntime(7539): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:405)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:418)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at com.redwood.PictureEditor.get_bitmap(PictureEditor.java:257)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at com.redwood.PictureEditor$5.run(PictureEditor.java:218)
     12-19 12:44:48.769: E/AndroidRuntime(7539):    at java.lang.Thread.run(Thread.java:1019)

Can any body solve my problem :(

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150

3 Answers3

2

Its never a good practice to manually call System.gc(); or Runtime.getRuntime().gc();. The better option is to resize/scale your Image according to the screen size using inSampleSize or maybe scaledBitmap(). You can check my answer here which will give you idea that how you can resize/scale the Image before loading it into memory.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • but Lalit ya i am already using options.inSampleSize and doing in sample size image but my actual question is activity A which is loaded with no of images, and although it is loading properly on first time but in second time it is going to exceeding memory size why? i am already destroying from stack and releasing all the objects then why this is happenig – Siddhpura Amit Dec 19 '12 at 10:04
  • maybe because something is still there in memory that is causing memory overloaded. Some static data might be there. – Lalit Poptani Dec 19 '12 at 10:06
  • but Lalit i have not used any static data :( You are right because when activity starts again it loaded with more memory i have tested – Siddhpura Amit Dec 19 '12 at 10:09
  • probably there is some static data which is not releasing your memory due to which you are getting the exception. – Lalit Poptani Dec 19 '12 at 10:18
  • I have posted whole class can you check this class there is no any static data http://stackoverflow.com/questions/13951575/bitmap-size-exceeds-vm-budget-by-starting-activity-again – Siddhpura Amit Dec 19 '12 at 11:38
2

Calling those methods won't work, those methods just say to the GC that "if you want you can run now".

Basically look for those problems:

  • Isn't the bitmap too big? -> Rescale it to the minium required size in your app.
  • Are you copying the bitmap too much?
  • Aren't you leaking the bitmap by leaking the context of your Activity B?

It could come from many places but I would try to downscale it to the minimum required size in your app first.

galex
  • 3,279
  • 2
  • 34
  • 46
  • but galex ya i am already using options.inSampleSize and doing in sample size image but my actual question is activity A which is loaded with no of images, and although it is loading properly on first time but in second time it is going to exceeding memory size why? i am already destroying from stack and releasing all the objects then why this is happenig – Siddhpura Amit Dec 19 '12 at 10:02
1

Try to Scale Down The Images You can use this

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

and from here you can calculate the Size of the image you are getting

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
  • but Usman ya i have use this method and resize image but my actual question is activity A which is loaded with no of images, and although it is loading properly on first time but in second time it is going to exceeding memory size why? i am already destroying from stack and releasing all the objects then why this is happenig – Siddhpura Amit Dec 19 '12 at 10:04
  • try to debug your code with break point and get the Heap size how it is increasing – Usman Kurd Dec 19 '12 at 10:08