This is my first time asking a question, so I'm sorry if I do something wrong. I've researched extensively on this website and other places to try to figure out why I'm getting this out of memory error, but I can't find anything from people who aren't already doing something that they know uses up a lot of memory. I'm making a small game using a very simple game engine we recently used in my computer science class. I added an Animation class that takes in arrays of BufferedImages that I'm getting from a sprite sheet using .getSubImage(), and I create these arrays/animations in the Player class:
//Arrays for each animation - .getSprite pulls a subImage from the spritesheet
private BufferedImage[] walkingLeft = {Sprite.getSprite(0, 9), Sprite.getSprite(1, 9), Sprite.getSprite(2, 9), Sprite.getSprite(3, 9), Sprite.getSprite(4, 9), Sprite.getSprite(5, 9), Sprite.getSprite(6, 9), Sprite.getSprite(7, 9), Sprite.getSprite(8, 9)};
private BufferedImage[] walkingRight = {Sprite.getSprite(0, 11), Sprite.getSprite(1, 11), Sprite.getSprite(2, 11), Sprite.getSprite(3, 11), Sprite.getSprite(4, 11), Sprite.getSprite(5, 11), Sprite.getSprite(6, 11), Sprite.getSprite(7, 11), Sprite.getSprite(8, 11)};
private BufferedImage[] standingg = {Sprite.getSprite(0, 10)};
private BufferedImage[] pullingWeaponRight = {Sprite.getSprite(0, 19), Sprite.getSprite(1, 19), Sprite.getSprite(2, 19), Sprite.getSprite(3, 19), Sprite.getSprite(4, 19), Sprite.getSprite(5, 19)};
//private BufferedImage[] pullingWeaponLeft = {Sprite.getSprite(0, 17), Sprite.getSprite(1, 17), Sprite.getSprite(2, 17), Sprite.getSprite(3, 17), Sprite.getSprite(4, 17), Sprite.getSprite(5, 17)};
//private BufferedImage[] walkingUp = {Sprite.getSprite(0, 8), Sprite.getSprite(1, 8), Sprite.getSprite(2, 8), Sprite.getSprite(3, 8), Sprite.getSprite(4, 8) };
//some animation states
private Animation walkLeft = new Animation(walkingLeft, 2);
private Animation walkRight = new Animation(walkingRight, 2);
private Animation standing = new Animation(standingg, 10);
private Animation pullWeaponRight = new Animation(pullingWeaponRight, 6);
//This animation gets assigned to walkLeft, walkRight, etc. based on the situation
private Animation animation = standing;
It will run without error with the current arrays and animations, but as soon as I add another array of BufferedImages (for example, if I un-comment pullingWeaponLeft or walkingUp, without even creating an animation with it), I get the error when I try to run the program. Considering the images from the sprite sheet are pretty tiny (64x64 pixels each), I'm really not sure why the memory is running out. Is there something about making the arrays that takes up a ton of space that I don't know about? That's my main question.
I've tried to increase the heap space, which didn't work, but I feel as though I shouldn't be getting this error in the first place, so I don't want to pursue that solution; unfortunately, that's the solution that comes up often when I search this issue.
I also have a Frame class, so let me know if that would be helpful to see. I know questions on here are supposed to be specific, so really I just want to know if created the BufferedImage arrays throws up a red flag for anyone regarding memory usage, I'm sort of new to Java.
Tell me if there's other information I should add to this, thanks!
Edit: I used some code someone wrote to print the max heap space, this is what printed:
Heap Size = 85000192
and this is the code:
public class GetHeapSize {
public static void main(String[]args){
//Get the jvm heap size.
long heapSize = Runtime.getRuntime().totalMemory();
//Print the jvm heap size.
System.out.println("Heap Size = " + heapSize);
}
}
Hope that's helpful.