OutOfMemory - BufferedImage/Image problems

I’ve searched for posts on this issue and found that others have had this problem. However, I have not seen any way to fix it other than to raise the stack size.

Here’s the culprit of my memory issues:


public class Sprite
{
      private float ANGLE_INC = (float)((Math.PI*2d)) / 90;
      private Image[] images;

      public Sprite()
      {
            this("", false);
      }

      public Sprite(String fileName)
      {
            this(fileName, false);
      }

      public Sprite(String fileName, boolean canRotate)
      {
            Image image = new ImageIcon(Globals.IMAGE_FOLDER + fileName).getImage();

            if(canRotate)
            {
                  BufferedImage imageRotations;
                  images = new Image[90];

                  GraphicsConfiguration gc =       GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
                  BufferedImage src = gc.createCompatibleImage(400, 400, Transparency.BITMASK);
                  {
                        Graphics2D g = src.createGraphics();
                        g.drawImage(image, 150, 150, null);
                        g.dispose();
                  }

                  for(int i = 0; i < 90; i++)
                  {
                        imageRotations = gc.createCompatibleImage(400, 400, Transparency.BITMASK);
                        Graphics2D g = imageRotations.createGraphics();
                        g.rotate(i*ANGLE_INC, 200, 200);
                        g.dispose();
                        images[i] = (Image)imageRotations;
                        imageRotations = null;
                  }
            }
            else
            {
                  images = new Image[1];
                  images[0] = image;
            }
      }

      public Image getImage()
      {
            return images[0];
      }

      public Image getImage(int rotation)
      {
            return images[rotation];
      }
}

Whenever that for loop gets called, my pagefile jumps up 70 megs. This is horrid! 90 400X400 pixel .gifs do not add up to 70 megs! The code in that constructor is executed in the preloading stage of the game. At this point, there are only two images going through that bit. If neither can rotate and they don’t hit the for loop, my memory hovers around 20megs. If one does, everything jumps to 80 megs. If they both do, it hits 150megs! What in the world is going on here?

Old posts have stated that the creation of many small objects can result in the garbage collector freaking out and bloating memory.

I tried adding “imageRotations = null” to see if the garbage collector would wake up. Unfortunatly, this did nothing.

Any clues to remedy this? I could just raise the stack size, but at this rate, I’ll overflow the page file!

90 400X400 pixel .gifs do not add up to 70 megs!

Let’s see…

90400400*4(32bit heh)=57.6mb

Your graphics card doesnt put gifs into it’s vram :slight_smile:

[quote]>90 400X400 pixel .gifs do not add up to 70 megs!

Let’s see…

90400400*4(32bit heh)=57.6mb

Your graphics card doesnt put gifs into it’s vram :slight_smile:
[/quote]
Ack, I didn’t see the 400px I hardcoded into there. I had thought things were being set on a per-image basis (the two images I was loading were 5X10px each). After a long night of coding, at 6:00am when something isn’t working, I figured it’d be nice to see see if my sleep and the board’s responses could help fix the issue.

Even so, I did not know that’s how images were created- such I am learning thanks to you guys. (There isn’t really any documentation on all these little quirks.)

It looks like I might have to find some other methods of preloading other than to store everything at the start of the game. My memory is going to be eaten up at this rate. Time to scale images down and attempt a smaller resolution I guess.

and when 1.5.0beta2 is released, the OGL pipleline will remove the need for precomputing the rotations, yay!