studio error android out-of-memory image-load

studio - android java out of memory error



Android OutOfMemoryError:? (7)

Correcciones comunes:

1. Arregla tus contextos:

Intente usar el contexto apropiado: por ejemplo, dado que un Toast se puede ver en muchas actividades en lugar de en solo una, use getApplicationContext() para getApplicationContext() , y dado que los servicios pueden seguir ejecutándose aunque una actividad haya terminado, inicie un servicio con:

Intent myService = new Intent(getApplicationContext(), MyService.class)

Use esta tabla como una guía rápida para el contexto apropiado:

Artículo original sobre contexto aquí .

2. Comprueba que realmente estás terminando tus servicios.

Por ejemplo, tengo un intentService que utiliza la API de servicio de ubicación de Google. Y me olvidé de llamar a googleApiClient.disconnect(); :

//Disconnect from API onDestroy() if (googleApiClient.isConnected()) { LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, GoogleLocationService.this); googleApiClient.disconnect(); }

3. Comprobar el uso de la imagen y de los mapas de bits:

Si está utilizando la biblioteca de Picasso de Picasso , descubrí que estaba perdiendo memoria al no usar .fit() , lo que redujo drásticamente mi huella de memoria de 50 MB en promedio a menos de 19 MB:

Picasso.with(ActivityExample.this) //Activity context .load(object.getImageUrl()) .fit() //This avoided the OutOfMemoryError .centerCrop() //makes image to not stretch .into(imageView);

4. Si está utilizando receptores de difusión, anúltelos.

5. Si está utilizando java.util.Observer (patrón de observador):

Asegúrese de usar deleteObserver(observer);

Esporádicamente estoy obteniendo un OutOfMemoryError: (Heap Size=49187KB, Allocated=41957KB) en una de mis aplicaciones. ¿Qué puedo hacer para diagnosticar esto?

01-09 10:32:02.079: E/dalvikvm(8077): Out of memory: Heap Size=49187KB, Allocated=41957KB, Limit=49152KB 01-09 10:32:02.079: E/dalvikvm(8077): Extra info: Footprint=48611KB, Allowed Footprint=49187KB, Trimmed=7852KB 01-09 10:32:02.079: D/skia(8077): --- decoder->decode returned false 01-09 10:32:02.079: D/AndroidRuntime(8077): Shutting down VM 01-09 10:32:02.079: W/dalvikvm(8077): threadid=1: thread exiting with uncaught exception (group=0x40a97228) 01-09 10:32:02.079: E/AndroidRuntime(8077): FATAL EXCEPTION: main 01-09 10:32:02.079: E/AndroidRuntime(8077): java.lang.OutOfMemoryError: (Heap Size=49187KB, Allocated=41957KB) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:486) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.content.res.Resources.loadDrawable(Resources.java:2044) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.content.res.Resources.getDrawable(Resources.java:675) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.view.View.setBackgroundResource(View.java:11776) 01-09 10:32:02.079: E/AndroidRuntime(8077): at com.blsk.bigtoss.ImageLoader.DisplayImage(ImageLoader.java:81) 01-09 10:32:02.079: E/AndroidRuntime(8077): at com.blsk.bigtoss.MatchActivity$MatchAsyncTask.onPostExecute(MatchActivity.java:1768) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.os.AsyncTask.finish(AsyncTask.java:602) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.os.AsyncTask.access$600(AsyncTask.java:156) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.os.Handler.dispatchMessage(Handler.java:99) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.os.Looper.loop(Looper.java:156) 01-09 10:32:02.079: E/AndroidRuntime(8077): at android.app.ActivityThread.main(ActivityThread.java:4987) 01-09 10:32:02.079: E/AndroidRuntime(8077): at java.lang.reflect.Method.invokeNative(Native Method) 01-09 10:32:02.079: E/AndroidRuntime(8077): at java.lang.reflect.Method.invoke(Method.java:511) 01-09 10:32:02.079: E/AndroidRuntime(8077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 01-09 10:32:02.079: E/AndroidRuntime(8077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 01-09 10:32:02.079: E/AndroidRuntime(8077): at dalvik.system.NativeStart.main(Native Method) 01-09 10:32:02.099: E/EmbeddedLogger(1612): App crashed! Process: com.blsk.bigtoss 01-09 10:32:02.099: E/EmbeddedLogger(1612): App crashed! Package: com.blsk.bigtoss v6 (1.2) 01-09 10:32:02.129: E/EmbeddedLogger(1612): Application Label: Cricket

Esta es la línea donde está ocurriendo:

LinearLayout resultMatchHeaderContainer = new LinearLayout(activity); if (!resultImagePath.equals("")) { imageLoader.DisplayImage(resultImagePath,resultMatchHeaderContainer, -1,modifiedHeight, R.drawable.matches_placeholder_result2x); } else { try { resultMatchHeaderContainer.setBackgroundResource(R.drawable.matches_placeholder_result2x); } catch (OutOfMemoryError e) { e.printStackTrace(); } }


Antes de cargar imágenes en la memoria, comprime tus imágenes usando

Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg")); ByteArrayOutputStream out = new ByteArrayOutputStream(); original.compress(Bitmap.CompressFormat.PNG, 100, out); Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); Log.e("Original dimensions", original.getWidth()+" "+original.getHeight()); Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());

Si obtiene su mapa de bits de un recurso, en cuyo caso la dimensión del mapa de bits dependerá de la densidad de la pantalla del teléfono.

Bitmap bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.img_1024x768)).getBitmap(); Log.e("Dimensions", bitmap.getWidth()+" "+bitmap.getHeight());


Esto puede ocurrir por varias razones, es posible que mantenga las referencias a otras partes de su código durante demasiado tiempo. Es posible que esté cargando en grandes mapas de bits que, junto con muchas referencias, le dan OOM, etc.

Normalmente, cuando se produce una OOM, se crea un hprof (instantánea del montón) en la raíz de la sdcard (o almacenamiento interno si no existe la sdcard), que puede leerse mediante herramientas como Eclipse MAT (incluida en las herramientas de Android si utiliza Eclipse). Primero, uno podría necesitar convertir el hprof con la herramienta hprof-conv . Aquí hay un tutorial de cómo usar Eclipse MAT: Investigar el uso de la memoria RAM . El informe de sospechosos de fugas es una buena primera lectura cuando hprof se carga en Eclipse MAT

Después de crear un perfil, puede leer cómo cargar imágenes de manera efectiva desde Mostrar mapas de bits de manera eficiente

También hay varias bibliotecas de carga de imágenes populares, como el cargador de imágenes universal y picasso , que hacen lo que necesita con facilidad.



Puedes hacer lo siguiente para evitar esto.

Drawable drawable = resultMatchHeaderContainer.getDrawable(); if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable != null) { Bitmap bitmap = bitmapDrawable.getBitmap(); if (bitmap != null && !bitmap.isRecycled()) bitmap.recycle(); } }

Cargar mapa de bits en Imageview siempre ha sido una causa de problemas de memoria, es muy común, por lo que tenemos que manejar la vista de imágenes y los mapas de bits con mucho cuidado. Lo que puede hacer es Al establecer cualquier mapa de bits de fondo en su imagen, obtener primero el dibujo y reciclarlo para que se elimine de la memoria y luego establecer el nuevo mapa de bits. Esto ayudará a evitar cualquier problema de OOM. Promover. Puede usar BitmapFactoryOptions para reducir el tamaño de su mapa de bits. me gusta:

// decodes image and scales it to reduce memory consumption private Bitmap decodeFile(File f) { try { // decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream stream1 = new FileInputStream(f); BitmapFactory.decodeStream(stream1, null, o); stream1.close(); // Find the correct scale value. It should be the power of 2. int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < REQUIRED_WIDTH || height_tmp / 2 < REQUIRED_HIGHT) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; FileInputStream stream2 = new FileInputStream(f); Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2); stream2.close(); return bitmap; } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); } return null; }


tal vez esto te ayude?

añadir manifiesto

android> v3

<application .... android:largeHeap="true">


sonrisa de mapa de bits final = BitmapFactory.decodeResource (getResources (), R.drawable.emo_im_happy);

Llamada

String pathname=BitMapToString(smile);

y luego llamar

setImagesNew(linearview,pathname,activity);

...

public String BitMapToString(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); String temp = Base64.encodeToString(b, Base64.DEFAULT); return temp; } public static void setImagesNew(LinearLayout linearLayout, String pathName, Activity activity) { Bitmap bmp = decodeSampledBitmapFromResource(pathName, getDeviceWidth(activity), getDeviceHeight(activity)); linearLayout.setBackgroundDrawable(bmp); bmp = null; System.gc(); Runtime.getRuntime().gc(); } public static Bitmap decodeSampledBitmapFromResource(String pathName, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(pathName, options); } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and // keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } @SuppressLint("NewApi") public static int getDeviceWidth(Activity activity) { int deviceWidth = 0; Point size = new Point(); WindowManager windowManager = activity.getWindowManager(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { windowManager.getDefaultDisplay().getSize(size); deviceWidth = size.x; } else { Display display = windowManager.getDefaultDisplay(); deviceWidth = display.getWidth(); } return deviceWidth; } @SuppressLint("NewApi") public static int getDeviceHeight(Activity activity) { int deviceHeight = 0; Point size = new Point(); WindowManager windowManager = activity.getWindowManager(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { windowManager.getDefaultDisplay().getSize(size); deviceHeight = size.y; } else { Display display = windowManager.getDefaultDisplay(); deviceHeight = display.getHeight(); } return deviceHeight; }

por favor, ponga todas las funciones en su actividad y llame solo a setImageNew() y pase el parámetro en imageview, sdcardpathname y activity

Espero que no se bloquee después de implementar este código. Porque me surge el mismo problema que tu ..