Memory failure error

8

I'm trying to create a game but when I pass the cell phone to try it I throw the error of lack of memory. I do not know why this error is occurring because the images I use do not take up much of anything. How can you solve it?

Here is the game: link

This is the 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) 
    
asked by Agustin Val 14.05.2018 в 01:24
source

2 answers

2

Definitely the problem is related to the consumption of memory when your application tries to allocate memory to manipulate the objects, in this case the images are the ones that should be optimized.

  

Caused by: java.lang. OutOfMemoryError : Failed to allocate to 1280012   byte allocation with 590376 free bytes and 576KB until OOM Caused by:   java.lang. OutOfMemoryError: Failed to allocate to 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

In your program this is the code where the problem occurs:

   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);

     ...
     ...
     ...
    }

It is very important to optimize the images, for this you can see several options here:

Good Resolution of an image causes "OutOfMemoryError"

by this you could have your application working:

    
answered by 14.08.2018 в 23:29
0

Make sure to optimize resources, reduce images and disconnect services / subscriptions that are not being used dynamically.

You could also increase the size of dynamic memory allocation (heap) in the manifest (I see that you do not have it in your project).

AndroidManifest.xml

<application
       # (otras opciones)
       android:largeHeap="true"> 
    
answered by 17.08.2018 в 17:08