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!