2

Basically I am making an image feed which is scrollable. And it works.But when memory profiling I don't think my users will be able to load lots of images without crashing.I was also doing memory profiling for Instagram's app and found out though they are loading lots of images but their heap size remains constant no matter how many images i have on my screen.There could be 3 possibilities

1)either they are using native memory for image rendering ,but I had tried implementing that but I am unable to render images without using the heap (if you can help me on this one it would be really cool )

2)they are using inSampleSize greater than 4 but that is really not possibe since the images are clear and medium quality

3)I smell native openGL interface in this , is it ?

Am I missing Out something !! please help !! I am using "ImageView" to load bitmaps


Even when memory profiling "Vine app" I found their java heap remains constant too whereas my java heap increase with the number of bitmaps loaded

I am using this library right now https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations

@Override
protected String doInBackground(String[] Params) {


    ParseFile fileObject = (ParseFile)photoObject.get("image");
    try {
        dataMain =  fileObject.getData() ;

        if(dataMain!= null) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inSampleSize = 1;
            options.inSampleSize = calculateInSampleSize(options, context.getResources().getDisplayMetrics().widthPixels,
                    context.getResources().getDisplayMetrics().widthPixels );

            options.inJustDecodeBounds = false;

            options.inPurgeable = true;

            bitmapHolder = new JniBitmapHolder(BitmapFactory.decodeByteArray(dataMain, 0, dataMain.length, options));

            bitmapHolder.scaleBitmap(widthPixels ,widthPixels* bitmapHolder.getBitmap().getHeight() / bitmapHolder.getBitmap().getWidth(), JniBitmapHolder.ScaleMethod.NearestNeighbour);

            //bmp = null ;

        }

    } catch (ParseException e) {
        e.printStackTrace();
    }


    }
        return "done";


}



    @Override
    protected void onPostExecute(String bitmap) {


            ImageView imageView = imageViewReference.get();

            if(imageView!= null && bitmapHolder.getBitmap() != null) {
                imageView.setImageBitmap(bitmapHolder.getBitmapAndFree());

                bitmapHolder = null;


            }

      }
mohit gupta
  • 160
  • 1
  • 8
  • **Trying out Various approaches** I will be using opengl's c implementation using JNI , Draw pictures on screen pixel by pixel – mohit gupta Jan 01 '16 at 07:23

2 Answers2

0

Use Facebook Fresco. It uses ashmem to store images.

You may also try reusing bitmaps. Read official docs to get more about efficient bitmap loading.

Also consider using Glide or Picasso libraries instead of manual image loading.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    (https://www.youtube.com/watch?v=JEdcPenx8p8) memory profiling of fresco looks horrifying – mohit gupta Dec 31 '15 at 13:57
  • @mohitgupta then use Glide or Picasso. They are simple and smart enough to handle lots of images. – Alexander Batashev Dec 31 '15 at 14:02
  • already used glide and i have been reusing bitmaps as well but no significant results all use java heap – mohit gupta Dec 31 '15 at 14:04
  • That's strange... If you can control server side, consider generating smaller images. There is also a very good presentation by my compatriot about building incredibly fast social apps. It's in Russian, but maybe you'll find it interesting (PowerPoint seems to be able to translate it). https://www.slideshare.net/mobile/ex3ndr/telegram-46277569 – Alexander Batashev Dec 31 '15 at 14:09
  • Also try to use tools such as LeakCanary. It looks like you have a memory leak. – Alexander Batashev Dec 31 '15 at 14:10
  • [alex.bat98](http://stackoverflow.com/users/2595837/alex-bat98) let me just try this check – mohit gupta Dec 31 '15 at 14:22
  • this is not a memory leak – Jyotsna Dua Dec 31 '15 at 14:54
  • @alex.bat98 thats theres no memory leak :'( all the objects are being freed when switching to a blank activity – mohit gupta Dec 31 '15 at 21:52
  • It's useless when you can't test it on an emulator. bad job facebook! it gave me some exceptions about multiple dex stuff I don't remember. – M D P Feb 05 '16 at 12:46
0

I was just overthinking it , turns out a recycler view just did the job

mohit gupta
  • 160
  • 1
  • 8