Here is the following error when executing the program. How can I avoid it, please help?

W/art: Throwing OutOfMemoryError "Failed to allocate a 51916812 byte allocation with 16777216 free bytes and 39MB until OOM" E/AndroidRuntime: FATAL EXCEPTION: Thread-10 Process: com.example.work, PID: 25823 java.lang.OutOfMemoryError: Failed to allocate a 51916812 byte allocation with 16777216 free bytes and 39MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:650) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:626) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:664) at com.example.work.MainStudent$1.run(MainStudent.java:115) at java.lang.Thread.run(Thread.java:760) W/AndroidRuntime: finished raiseRlimit, rlim_cur:4096 rlim_max:4096 String sql2 = "SELECT photo1,photo2,photo3 FROM post where email ="+"'"+Login.User+"'"; try { rs = stmt.executeQuery(sql2); } catch (SQLException e) { e.printStackTrace(); } try { while (rs.next()) { if(rs.getBlob("photo1") != null) { Blob imageBlob = rs.getBlob("photo1"); binaryStream = imageBlob.getBinaryStream(1, imageBlob.length()); postImage.add(BitmapFactory.decodeStream(binaryStream)); } else{ postImage.add(null); } if(rs.getBlob("photo2") != null) { Blob imageBlob = rs.getBlob("photo2"); binaryStream = imageBlob.getBinaryStream(1, imageBlob.length()); postImage.add(BitmapFactory.decodeStream(binaryStream)); } else { postImage.add(null); } if(rs.getBlob("photo3") != null) { Blob imageBlob = rs.getBlob("photo3"); binaryStream = imageBlob.getBinaryStream(1, imageBlob.length()); postImage.add(BitmapFactory.decodeStream(binaryStream)); } else { postImage.add(null); } } } catch (SQLException e) { e.printStackTrace(); } 
  • one
    Probably you are trying to display much more pictures. Without details, nothing more can be said. Try to reduce the pictures. - YurySPb
  • How to reduce BitmapFactory.decodeStream (binaryStream) size programmatically? - user221117 3:18 pm
  • android: largeHeap = "true" - Style-7

2 answers 2

Try to reduce memory consumption by lowering quality in color and size:

 BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Config.RGB_565; options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options); 
  • Well, and if the list will be a lot of images? - user221117 pm
  • @ user221117, then nothing will help) Everything displayed is loaded into RAM, and if there is a lot of things there, it will lag and fall. - Yuriy SPb 4:16 pm
  • Before downloading, check the size of the image without downloading. Using optionsNewBitmap.inJustDecodeBounds = true; If it is too big, then resize using options.inSampleSize, then download. - Yurii

First, reduce the quality of images in the database.

Secondly, to put the pictures in a separate table, and in the list of letters to leave the image id. When you need pictures, select them not with the whole bundle at once, but one at a time, trimming the size to what is imputed, as already advised in the comments if they are still larger than the screen resolution / size of the view in which it will be displayed.

Thirdly, if the first two points are not enough, add the android attribute: largeHeap = "true" to the manifest in the tag. It will increase the maximum amount of RAM for the application.

Fourthly, to hang up on the stream "interceptor of unclosed exceptions", since OutOfMemory is not caught by catch. Implement Thread.UncaughtExceptionHandler and hang it on the main thread

 Thread.currentThread().setUncaughtExceptionHandler(this); 

Then at least you can react and show the user an error message, and not just die

Fifth, it is better not to work with pictures in the main stream. This process can be painful. I recommend to take a library to download images, for example Picasso. Its use will take out the loading of pictures into a separate stream, it will process OutOfMemory and any other error when loading so that the program will continue to work, just replace the desired picture with an error picture. In general, a useful thing :)