3

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
lils
  • 61
  • 1
  • 1
  • 5
  • 8
    I would suggest to watch the execution with VisualVm: https://visualvm.java.net/ – simdevmon Apr 13 '16 at 14:05
  • How much are you increasing your heap space by? – A.Sharma Apr 13 '16 at 14:06
  • There might be going something bad in the Animation class? Make sure to check it out with VisualVM. – tak3shi Apr 13 '16 at 14:10
  • How much memory is it using while it works, and what is your heap max (if you don't know: http://stackoverflow.com/questions/417152/how-do-i-set-javas-min-and-max-heap-size-through-environment-variables ) – Matt Jordan Apr 13 '16 at 14:24
  • when do you do this sprite loading? tpyically games run in an event loop, I hope you're not loading the sprites inside this loop – Gerald Mücke Apr 13 '16 at 14:25
  • 1
    You should use visualvm, as @simdevmon suggested. Comment out all of the arrays and add them back one by one, keeping track of the memory usage as you go. Also make sure that your memory usage is roughly constant throughout the run of the game with visualvm. If the memory usage is increasing, that is a sign of something being wrong. If your game is short, you could post it on pastebin and let us look at the whole thing. – Daniel Centore Apr 13 '16 at 14:29
  • Thanks for the help, everyone! @Matt Jordan I added the max heap space to the post – lils Apr 13 '16 at 17:08
  • This post is missing a lot of relevant code, such as the implementation for `Sprite.getSprite(int, int)` and your `Animation` class. Would be hard for others to help without that information. – Vince Apr 13 '16 at 17:17
  • Possible duplicate of [Is there a command line method to get a jvm heap dump besides jmap?](http://stackoverflow.com/questions/38219660/is-there-a-command-line-method-to-get-a-jvm-heap-dump-besides-jmap) – Paul Sweatte Mar 07 '17 at 21:42

0 Answers0