-1

I working on a android project like a instagram. There are a lot of images, datas etc. in the project. I used Glide library for set images in ImageView. So if I open 10 profile pages in succession I get out-of-memory error like:

java.lang.OutOfMemoryError: Failed to allocate a 44236812 byte allocation with 16777216 free bytes and 32MB until OOM

I tried to setFlag to intent :

setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

and tried add Manifest.xml :

android:largeHeap="true"
android:hardwareAccelerated="false"

and tried add in Glide functions one by one :

.apply(new RequestOptions().format(DecodeFormat.PREFER_RGB_565)

.skipMemoryCache( true )

.diskCacheStrategy( DiskCacheStrategy.NONE ) and DiskCacheStrategy.DATA

But none of them couldnt fix my error.

Also this screenshot is my Profiler output:

Profiler Output

I test in Profiler memory can be up to 600 mb and than application will dump.

How can I fix that? I need someone's help.

Boken
  • 4,825
  • 10
  • 32
  • 42
Selman Tosun
  • 428
  • 5
  • 14
  • don't use large images statically be sure size and resolution not high – Abhinav Gupta Mar 26 '19 at 13:42
  • You likely have a memory leak. Use some memory profiler tools and see what is leaking. – David Wasser Mar 26 '19 at 13:48
  • I decreased size as much as possible but didn't work because there was too much data. – Selman Tosun Mar 26 '19 at 13:48
  • 1
    Generate heap dumps, and look for leaks. If none are found, take the largest objects and try to reduce their number and size. There's no quick and easy answer to give for memory issues, the problem could be anywhere in your app. I can tell you that the first 2 things you tried will never have any effect. – Gabe Sechan Mar 26 '19 at 13:50

1 Answers1

0

I got similar issues with my app.I will suggest you to decrease the quality of pictures or resize them. For decreasing the size you can use this :

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
    }

// bitmap = getResizedBitmap(bitmap, 1000); //1000 is the maximal size of the image.

Or you can compress your image by :

Bitmap bitmap = null;
try {
     bitmap = BitmapFactory.decodeStream(new FileInputStream(image));
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
                FileOutputStream fos = new FileOutputStream(dirname + imageName);
                bitmap = getResizedBitmap(bitmap, 1000);
                // quality is 80%
                bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
Houssein Zouari
  • 712
  • 6
  • 18