8

estoy intentando crear un juego pero cuando lo paso el celular para probarlo me tira el error de falta de memoria. No se porque se esta dando este error ya que las imágenes que uso no ocupan casi nada. Como se puede solucionar?

Acá esta el juego: https://github.com/agusval1994/PruebaEspacio

Este es el LogCat:

Process: com.example.agusv.pruebaespacio, PID: 1266
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.agusv.pruebaespacio/com.example.agusv.pruebaespacio.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.example.agusv.pruebaespacio.Juego
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
    at android.app.ActivityThread.access$900(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5459)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.example.agusv.pruebaespacio.Juego
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.example.agusv.pruebaespacio.MainActivity.onCreate(MainActivity.java:21)
    at android.app.Activity.performCreate(Activity.java:6259)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
    at android.app.ActivityThread.access$900(ActivityThread.java:157) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5459) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.example.agusv.pruebaespacio.Juego
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
    at com.example.agusv.pruebaespacio.MainActivity.onCreate(MainActivity.java:21) 
    at android.app.Activity.performCreate(Activity.java:6259) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
    at android.app.ActivityThread.access$900(ActivityThread.java:157) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5459) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
    at com.example.agusv.pruebaespacio.MainActivity.onCreate(MainActivity.java:21) 
    at android.app.Activity.performCreate(Activity.java:6259) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
    at android.app.ActivityThread.access$900(ActivityThread.java:157) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5459) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 Caused by: java.lang.OutOfMemoryError: Failed to allocate a 19996428 byte allocation with 14315624 free bytes and 13MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2638)
    at android.content.res.Resources.loadDrawable(Resources.java:2543)
    at android.content.res.Resources.getDrawable(Resources.java:806)
    at android.content.res.Resources.getDrawable(Resources.java:771)
    at com.example.agusv.pruebaespacio.Juego.<init>(Juego.java:123)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
    at com.example.agusv.pruebaespacio.MainActivity.onCreate(MainActivity.java:21) 
    at android.app.Activity.performCreate(Activity.java:6259) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
    at android.app.ActivityThread.access$900(ActivityThread.java:157) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5459) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
6
  • Yo tuve este problema y fue un verdadero quebradero de cabeza. Comprobé absolutamente todo hasta que me dí cuenta del error. (Puede que no sea el mismo, pero igual te ayuda). Mi problema era como el tuyo, tenía unas imagenes que no ocupaban nada y todo estaba bien.... hasta que fui editando mi XML quitando imagenes y poniendo imagenes para ver cual era el error y que imagen era la problematica. Me cree un emulador con una memoria super baja y con una densidad MDPI. Aquí se encontraba el problema: tenía algunas imagenes que estaban en XXHDPI por lo que hacía al dispositivo cargar imagenes d el 14 may. 2018 a las 13:24
  • @SebastianPaduano eso solo aliviaría el problema. Pero seguirá ocurriendo el error de memoria. Analizando el código me di cuenta que dibuja muchos Bitmaps pero no los recicla. Debes limpiar la mayoría de objetos posibles cuando estos no estén en uso. Los bitmaps no deben quedarse como referencias activas. Debes reciclarlo con .recycle(); y volverlos null. Tu solución es mejorar la arquitectura. Desde mi punto de vista es muy fácil hacerlo, ya que manejas casi todas las referencias en una sola clase. el 15 may. 2018 a las 0:18
  • Hola @SebastianPaduano gracias por la respuesta pero no me funciono, sigue el error. el 15 may. 2018 a las 0:35
  • Hola @Andrespengineer gracias por la respuesta, voy a intentar modificar el código para reciclar los bitmaps el 15 may. 2018 a las 0:36
  • yo tuve ese problema y me toco solucionarlos por el GrisBuilder, donde se le asigna el espacio de memoria, no se si sea el mismo, pero a mi me toco asi. ReNiceCode...
    – srJJ
    el 12 ago. 2018 a las 14:55

2 respuestas 2

2
+25

Definitivamente el problema esta relacionado a el consumo de memoria cuando tu aplicación trata de asignar memoria para manipular los objetos, en este caso las imágenes son las que deben optimizarse.

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 1280012 byte allocation with 590376 free bytes and 576KB until OOM Caused by: java.lang.OutOfMemoryError: Failed to allocate a 1280012 byte allocation with 590592 free bytes and 576KB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) Waiting for a blocking GC Alloc Clamp target GC heap from 271MB to 256MB Starting a blocking GC Alloc AllocSpace objects, 0(0B) LOS objects, 0% free, 255MB/256MB, paused 285us total 10.125ms

En tu programa este es el código donde ocurre dicho problema:

   public Juego(Context context, AttributeSet attrs) {
        super(context, attrs);
        ...
        ...
        ...
        Drawable imagen_planeta_drawable = context.getResources().getDrawable(R.drawable.planeta_sprite_sheet2);
        Drawable imagen_asteroides_drawable = context.getResources().getDrawable(R.drawable.asteroide);
        Drawable imagen_espiritu_drawable = context.getResources().getDrawable(R.drawable.espiritu_sprite_sheet);
        Drawable imagen_destruccion_drawable = context.getResources().getDrawable(R.drawable.destruccion_spritesheet);
        Drawable imagen_enemigo_drawable = context.getResources().getDrawable(R.drawable.espiritu_enemigo_spritesheet);
        Drawable imagen_destruccion_planeta_drawable = context.getResources().getDrawable(R.drawable.destruccion_planeta_spritesheet);
        Drawable imagen_otro_planeta_drawable = context.getResources().getDrawable(R.drawable.planeta_espiritu_spritesheet);
        Drawable imagen_espiritu_sin_brillo_drawable = context.getResources().getDrawable(R.drawable.espiritu_sin_brillo_spritesheet);
        Drawable imagen_espiritu_energia_drawable = context.getResources().getDrawable(R.drawable.espiritu_sprite_sheet);
        Drawable imagen_espiritu_en_transicion_drawable = context.getResources().getDrawable(R.drawable.transcicion_spritesheet);
        Drawable imagen_muerte_espiritu_drawable = context.getResources().getDrawable(R.drawable.espiritu_sin_brillo_spritesheet);


        imagen_planeta = ((BitmapDrawable) imagen_planeta_drawable).getBitmap();
        planeta = new Planeta(this, imagen_planeta);

        imagen_asteroides = ((BitmapDrawable) imagen_asteroides_drawable).getBitmap();
        imagen_asteroides = Bitmap.createScaledBitmap(imagen_asteroides, 200, 200, false);

        imagen_espiritu = ((BitmapDrawable) imagen_espiritu_drawable).getBitmap();
        imagen_espiritu_sin_brillo = ((BitmapDrawable) imagen_espiritu_sin_brillo_drawable).getBitmap();
        imagen_espiritu_en_transicion = ((BitmapDrawable) imagen_espiritu_en_transicion_drawable).getBitmap();
        //200 de heght y es 1 sola imagen en horizontal, entonces si tengo 8 imagenes son 200x8
        imagen_espiritu = Bitmap.createScaledBitmap(imagen_espiritu, 1600, 200, false);
        imagen_espiritu_sin_brillo = Bitmap.createScaledBitmap(imagen_espiritu_sin_brillo, 1600, 200, false);
        imagen_espiritu_en_transicion = Bitmap.createScaledBitmap(imagen_espiritu_en_transicion, 1600, 200, false);
        espiritu = new Espiritu(this, imagen_espiritu, imagen_espiritu_sin_brillo, imagen_espiritu_en_transicion);

        imagen_destruccion = ((BitmapDrawable) imagen_destruccion_drawable).getBitmap();
        imagen_destruccion = Bitmap.createScaledBitmap(imagen_destruccion, 400, 200, false);
        asteroides = new Asteroides(this, imagen_asteroides, imagen_destruccion);

        imagen_enemigo = ((BitmapDrawable) imagen_enemigo_drawable).getBitmap();
        imagen_enemigo = Bitmap.createScaledBitmap(imagen_enemigo, 1600, 200, false);
        enemigo = new Espiritu_Enemigo(this, imagen_enemigo);

        imagen_destruccion_planeta = ((BitmapDrawable) imagen_destruccion_planeta_drawable).getBitmap();
        imagen_destruccion_planeta = Bitmap.createScaledBitmap(imagen_destruccion_planeta, 2200, 1600, false);
        destruccionPlaneta = new DestruccionPlaneta(this, imagen_destruccion_planeta);

        imagen_otro_planeta = ((BitmapDrawable) imagen_otro_planeta_drawable).getBitmap();
        otroPlaneta = new OtroPlaneta(this, imagen_otro_planeta);

        imagen_espiritu_energia = ((BitmapDrawable) imagen_espiritu_energia_drawable).getBitmap();
        imagen_espiritu_energia = Bitmap.createScaledBitmap(imagen_espiritu_energia, 800, 100, false);
        espirituEnergia = new EspirituEnergia(this, imagen_espiritu_energia);

        imagen_muerte_espiritu = ((BitmapDrawable) imagen_muerte_espiritu_drawable).getBitmap();
        imagen_muerte_espiritu = Bitmap.createScaledBitmap(imagen_muerte_espiritu, 800, 100, false);

     ...
     ...
     ...
    }

Es muy importante optimizar las imágenes, para esto puedes ver varias opciones aquí:

buena Resolución de una imagen causa "OutOfMemoryError"

mediante esto podrías tener tu aplicación funcionando:

introducir la descripción de la imagen aquí

2
  • Intenta exprotar las imagenes por Photoshop para WEB el 16 ago. 2018 a las 22:36
  • Gracias Saul de hecho el software que comentas puede ayudar precisamente para optimizar las imágenes que es lo que comento en la respuesta, saludos!
    – Jorgesys
    el 21 ago. 2018 a las 21:34
0

Asegura de optimizar recursos, disminuir imágenes y desconectar servicios/suscripciones que no se esten usando en forma dinámica.

También podrias aumentar el tamaño de asignación de memoria dinámica (heap) en el manifiesto (veo que no lo tienes en tu proyecto) .

AndroidManifest.xml

<application
       # (otras opciones)
       android:largeHeap="true"> 

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.