0

I'm getting error of java.lang.OutOfMemoryError and crash the app while run the application for download the multiple images from server and stored into SD card.I have some changes in gradle.properties and change the line # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=1024m -XX:MaxHeapSize\=1024m -XX:+HeapDumpOnOutOfMemoryError -Xmx1024m -Dfile.encoding=UTF-8. And trying to call the System.gc(); also .But getting same error again and again.How to solve this issues. Thanks in advanced.

here is my code

The error at this line imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);

Log.e("fileUrl ", " = " + fileUrl + " ImageName = " + ImageName);
            URL ImgUrl = new URL(fileUrl);
            HttpURLConnection conn = (HttpURLConnection) ImgUrl.openConnection();
            conn.connect();
            int lenghtOfImage_File = conn.getContentLength();
            Log.e("lenghtOfImage_File ", " = " + lenghtOfImage_File);


                System.gc();
                if(imagenObtenida != null)
                {
                    imagenObtenida.recycle();
                    imagenObtenida = null;
                }
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inTempStorage = new byte[16*1024];
                options.inPurgeable = true;
                options.inSampleSize = 1;

                imagenObtenida = BitmapFactory.decodeStream(conn.getInputStream(), null, options);
                newFolder = new File(Environment.getExternalStorageDirectory().getPath() + File.separator + "classnkk_images");

                File file = new File(newFolder, ImageName);

Here is my Log error

10-16 11:16:20.978    1135-1152/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:278)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
            at com.example.tazeen.classnkk.AllPosts_Page.download_PngFile(AllPosts_Page.java:1128)
            at com.example.tazeen.classnkk.AllPosts_Page.getDoenLoaddata(AllPosts_Page.java:735)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:705)
            at com.example.tazeen.classnkk.AllPosts_Page$GetgetDoenLoaddata.doInBackground(AllPosts_Page.java:701)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
7
  • Its better to use Glide or Fresco libraries to load multiple images from server. Commented Oct 16, 2015 at 5:58
  • 1
    Possible duplicate of Strange out of memory issue while loading an image to a Bitmap object Commented Oct 16, 2015 at 6:00
  • This means that you are downloading either too many or too big images. Please make sure that you download only those images which you need and images are of appropriate resolution. Commented Oct 16, 2015 at 6:01
  • I'm downloading too many and too big images from server. Commented Oct 16, 2015 at 6:05
  • @androidTag Big images creates problem Commented Oct 16, 2015 at 9:07

2 Answers 2

2

java.lang.OutOfMemoryError: Requested size exceeds VM limit. This error indicates that a Java application attempts ti allocate an array, whose size is larger than the heap size.

The OutOfMemoryError extends the VirtualMachineError class, which indicates that the JVM is broken, or it has run out of resources and cannot operate.

Verify that your application does not store unnecessary information. Store and maintain only those pieces of information required for the proper execution of your Java application.

Strange out of memory issue while loading an image to a Bitmap object

Sign up to request clarification or add additional context in comments.

Comments

0

The image that u r trying to download from the server might be large and hence while trying to decode it, it results in OOM exception.

Check this post on developer's site for handling bitmaps here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.