This topic is 2304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Before I start I'd like to say that this is the first really big game I've worked on. So far my programming experience involves a high school level intro to Java class, and the writing of Snake, Pong, Space Invaders, Pac Man and Tetris. I realize that all of them are relatively simple compared to what I'm trying to do now, but I'm learning so if I'm doing something blatantly wrong please be gentle. I still am stumbling around in the dark.

With that said, I'm writing a 2D sidescroller, and everything (so far) was working fine, until I tried to load my first background. The background is a large image that would scroll as the player moves. (It's an 11000 px x 6000 px .png file that is 6.3 MB)
I have several other animations/images that loaded perfectly fine before this.

The way I'm handling animations and images (if this is a bad idea please let me know so I can fix it.) is to load them all in the constructor of the game into two separate handler objects. For example, I have a DataStreamReader that reads an initial_load.txt file and passes the names, paths, and number of frames from this txt file to an AnimationHandler object as Strings. The animation handler takes these strings and uses them to load each frame into an animation object and then the animation into an ArrayList<Animation>.
 for(int i=0;i<numFrames;i++){ ImageIcon img = new ImageIcon(this.getClass().getResource(path+name+i+".png")).getImage(); frames.add(new Frame(img)); } animations.add(new Animation(frames, name)); 
For each object I create, I pass it the animationHandler and it finds the appropriate animations for itself. This way I'm not loading the images for each separate object. (This is how I've done it for my earlier games.)

What I've learned is that Java creates a separate Thread for each image it is trying to load. After too many threads are loaded it will produce the error I'm getting:
 Exception in thread "Image Fetcher 0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41) at java.awt.image.Raster.createPackedRaster(Raster.java:458) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015) at sun.awt.image.ImageRepresentation.createBufferedImage(ImageRepresentation.java:230) at sun.awt.image.ImageRepresentation.setPixels(ImageRepresentation.java:535) at apple.awt.OSXImageRepresentation.setPixels(OSXImageRepresentation.java:58) at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120) at sun.awt.image.PNGImageDecoder.sendPixels(PNGImageDecoder.java:531) at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:452) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136) 

Nothing I've read has really explained how to go about fixing this.

That's everything I know. I'm really not sure what to do although I'll keep looking.
Any help is greatly appreciated,
Thanks
Peter

Share on other sites
Your issue is that you ran out of memory, in Java I think the default is set to like 64 or 96 megabytes for your application, if your application takes up more memory then this you get that error. This can be easily remedied by you passing a certain argument when running your java program telling it to allocate more space, whether its 128 megabytes, a gigabyte etc.... I found in game programming it is very easy to go over this initial limit.

The paramater u want to pass is the following.

java -Xmx2g mygame
which will give you 2 gigabytes for your game or
java –Xmx128m mygame
which will give you 128 megabytes for your game.

I think you get the idea here, if you are having any other issues let me know.

Share on other sites
Thanks!
How would I do that from within the game? Because when the game is finished I don't want people to have to go through and do that just to run the game.

I guess I'll go ahead and ask this just to be sure: Is the way I'm handling images and animations ok?

Thanks again
Peter

Share on other sites
You could do it that way, but I believe if you load a sprite sheet instead and and just render certain parts of the sprite sheets (in reference to its current animation), you should see some performance increase.

Unless you plan on the user themselves running the game from console (i.e you tell them to go type in java myGame) then there really shoudln't be an issue, you can obscure (hide) those parameters from the user in many ways, so they basically just click on something to run the game, you might want to look at packaging it all into a jar and looking up making executable jar files. That can be an alternative.

EDIT!!! I just noticed you wrote that the background image is a 11000 px x 6000 px, at first i understood this as a 1100 x 600 px. this is going to take a HUGE chunk of memory and i dont recommend directly loading it, split this image into many pieces and load one up at a time. Even though the image is PNG and depending how you are doing things this image can take up to 264megabytes of DATA!!!. Edited by The_Neverending_Loop

Share on other sites
Thanks again for the advice. I'm not to the point yet where I am doing stand alone builds of the game yet, and I'm running it from within Netbeans. Is it possible to alter the amount of memory available to Netbeans?

Also I split the background apart into pieces. They're now 2000 x 1500 px and as I'm loading them I'm getting the same error as before.
If the problem is the game is loading too many images at once, is there a way to check if an image is finished loading before continuing?

Thanks again,
Peter

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633768
• Total Posts
3013747
×