OutOfMemoryError on Android

edited November 2013 in Android Mode

So I have tried everything to my knowledge under the sun to fix this error message with my application , but with dismay.

To give some background, I am creating an app that requires large amount of image frames to be indexed in an array to begin animations. My code is rather dense, but to give you an explaination of the "skeleton" of my app, I have instantiated a state machine, which allows me to trigger between different sets of animations per "page" (each enum state). The images have been resized correctly (I know using img.resize() can add some overhall, especially when I am triggering 50+ frames).

My first page works fine, but when I break out of the first state to the second, this is where my issue begins. it successfully begins, loads my initial background image without a hitch. When I begin to instantiate my new class, It causes the outofmemory crash. I've ensured all of the images are resized (manually, not through the method resize), i've adjusted my manifest file and added the line,

               android:largeHeap="true">

below the <application bracket. Here is my class i've created that is giving me the issue:

class TELEVISION {
  PImage tv;
  int channelFrames = 103;  // The number of frames in the animation
  int framePos = 0;
  float windowTime =0;

  float windowDelay = 1000/60f;
  boolean shapeChannel = true;

  PImage[] firstChannel = new PImage[channelFrames];


  TELEVISION() {
    tv = loadImage("tv.png");
    // tv.resize(340,500);
    for ( int i = 0; i< firstChannel.length; i++ )
    {

      firstChannel[i] = loadImage("channel" + i+".png");
    }
  }



  void play() {
    if (shapeChannel==true) {
      if ( millis() > windowTime ) {


        framePos = (framePos+1) % channelFrames;  // Use % to cycle through frames
        pushMatrix();
        translate(displayWidth/1.24, displayHeight/1.67);
        rotateZ(radians(5));
        //rotateY(radians(10));
        image(firstChannel[framePos], 0, 0);



        windowTime+=windowDelay;
        // g.removeCache(firstChannel[framePos]);

        popMatrix();
      }
    }


    image(tv, displayWidth/1.27, displayHeight/2.3);
  }
}

Answers

  • Make sure that you're properly disposing of the TELEVISION class after you're done with it (clearing the reference). Keep in mind that, even with android:largeHeap="true" (I'm not entirely sure if that works, but it might), Android has severe memory limitations... you're running on a phone, after all...

  • Make sure that you're properly disposing of the TELEVISION class after you're done with it (clearing the reference).

    by this, you mean setting the reference, (object created with "TELEVISION" class ) to null?

  • Answer ✓

    It depends on the implementation. If the object is part of a list, then removing it should be enough. Generally, removing any persisting reference to the object will allow the garbage collector to do its work...

  • I am revisiting this thread with the same issue. To give a general idea:

    im using a Nexus 7 that is triggering a large set of image frames in arrays to create animations. I have compressed the images significantly (not utilizing the resize function). This is an interactive story that contains "pages". Ive structured it like so as a state machine for each page. I still get crashes (outofmemoryerror) when transitioning to the next "state" sometimes. Is this descriptive enough to get a generalized idea?

  • edited November 2013

    Can you provide the code involved in the transitions?

  • edited November 2013

    eek, I retracted my code. found my errors.

  • edited November 2013

    Thanks for the suggestion. a combination of adjustments (altering the manifest file, clearing class objects, image adjustments, etc) smoothed things out a bit.

Sign In or Register to comment.