0

I'm writing a 2D game in Java and I'm having trouble loading images into an image handler. First off, I want to say that the system works until I start to load more than 15 images.

I have an AnimationHandler class that is given the names and paths of images and is supposed to load them into Animation classes and store those animations. I'm doing this so that each instance of an object in my game can use the same animations rather than loading them all separately.

Anyways, my problem is that when I try and load a lot of images (I'll explain why I'm going to have so many in a sec.) I get the following error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I'm loading in the images with:

BufferedImage im = ImageIO.read(getClass().getResources(imgName));

The reason I'm going to have to load so many images is because a) one image per frame or at least one larger image per animation. b) some animations (eg. backgrounds) are huge so I have each frame of these animations split into parts.

From what I looked at, the only response anyone seems to get is to increase the amount of memory available to the JVM, but I'm not building out to .jar files yet, and I'm executing from NetBeans and don't know how to change it from inside NetBeans.

Also, the way it's loading images is to create a new thread for each image that's being loaded. I don't know if this would help but is there a way to keep track of how many images are being loaded currently, and based on that number choose to wait to load the next image?

Any help would be really appreciated, Thanks Peter

Edit: Heres the full stack trace:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:59)
at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397)
at java.awt.image.Raster.createWritableRaster(Raster.java:938)
at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1056)
at javax.imageio.ImageReader.getDestination(ImageReader.java:2879)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1263)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1560)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1374)
at Loaders.ImageLoader.loadImage(ImageLoader.java:25)
at MediaHandlers.AnimationHandler.loadAnimation(AnimationHandler.java:53)
at MediaHandlers.AnimationHandler.initAnimations(AnimationHandler.java:38)
at MediaHandlers.AnimationHandler.<init>(AnimationHandler.java:22)
at Main.PlatformGame.<init>(PlatformGame.java:90)
at Main.PlatformGame.main(PlatformGame.java:105)
Peteyslatts
  • 123
  • 1
  • 7
  • Q: You're closing all the files after you've finished reading them, correct? Q: It's simply storing the images in memory that's causing you to run out of heap memory, correct? Q: How is this impacting your OS memory? Have you tried "-Xmx" (IIRC) to increase JVM heap size? – paulsm4 Aug 30 '12 at 04:47
  • try with bitmap option with sampleSize check this links http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html – Pratik Aug 30 '12 at 05:06
  • paul: I think its the reading that's causing the error. I'm not sure though. – Peteyslatts Aug 30 '12 at 05:24
  • 1
    *"the only response anyone seems to get is to increase the amount of memory available to the JVM,.."* Transform the entire animation into a video format. (Just for variety.) – Andrew Thompson Aug 30 '12 at 06:11
  • @Peteyslatts - that's exactly my point. *None* of us really knows ... because the OP isn't giving us the right info. The operation might be inherently memory consuming ... or he might be doing something wrong or inefficiently. Configuring VM parameters might help ... or the PC might not have enough memory. We need more details to give any intelligent answer. In the meantime, I thought I'd ask the obvious "Gee, are you closing the file/free resources?" It's surprising how often the answer is "No" ;) – paulsm4 Aug 30 '12 at 21:42

1 Answers1

1

To change the settings in Netbeans;

  • Right click the main project node
  • Select "Properties"
  • Select the "Run" node
  • Add you memory requirements to the VM Options field ... ie -Xms128M -Xmx1024M

As to your loading process. If you're not already doing so, I'd use a ExecutorService to load the images (probably a fixed thread pool service).

Generally speaking, it will make things a little easier to manage and also help limit the number of runaway threads (cause at some point, having lots of threads brings no more performance benefit then having no threads)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks, increasing the memory worked. Thank you for pointing out how. I've never used an ExecutorService before, but I'm all for making image loading more efficient, so I'll look it up too. Thanks again for the help! – Peteyslatts Aug 31 '12 at 02:26