Results 1 to 2 of 2
- 02-13-2011, 03:10 PM #1Member
- Join Date
- Feb 2011
- Rep Power
Suggestions needed for decreasing memory usage
Iím new around here though I do have some experience with Java. I signed up because Iím hoping I can get some recommendations and suggestions for the smart way to solve a problem I have with a hobby project :)
Some explanation is needed. My program is used for administering a map campaign, played in real time. The map is divided into a number of provinces, each owned by a team. The participating players are divided into the teams. I think a picture will help explain the concept:
Here the provinces are marked with borders in the colour of their owning team. The players are marked with flags in the colour of their team. The borders are laid out in a way so that if two provinces share a border, a player can travel between them.
Now the idea of the campaign is that players move between provinces and when they meet up with a player of another team, a battle ensues. If the attacking player wins this battle, he can capture the province for his team. Since itís real time the player icons move slowly across the map as time passes.
Now for the Java part of it. The program has already been used for the first time but I had to cut some corners to get it done since I was a bit short on time. For now my problem is mostly a memory issue. Each province has an image for each team colour. With 5 teams and 27 provinces itís 135 images (at about 16 Kb each itís about 2.2 Mb), each loaded as a BufferedImage. Besides that there are the map image (160 Kb), 24 player icons (less than 1 Kb each) and a few extra small icons.
When I loaded all of it I started getting memory heap errors, which I so far have solved with giving the desktop application extra memory (the -Xmx512m option).
Right, Iím finally getting to where I need some help. Iíd like to find a better way to solve this memory issue if possible. One reason is to improve the desktop application, since it takes a short while to repaint every province; another reason is that Iím working on a way to display the map as playback on a website and initially Iím thinking using an applet since I can reuse a lot of my current classes. The trouble is itís not feasible to increase the memory available in an applet as far as I know.
Options Iím considering:
2) Donít load every single province image but only those needed at the time being, and load a new image when a province changes owner. But Iím not sure if thatíll be enough.
3) Make the desktop application save an image each time a province change owner. Since the provinces donít change owners that often, the applet would have a smaller amount of images to load, as it wouldnít have to load any specific province images. Each image would be larger, though.
4) A better way to do it?
Can anyone help?
(oh, and please ask if some of it doesnít make sense :))
- 02-14-2011, 06:12 AM #2Senior Member
- Join Date
- Jan 2009
- Rep Power
Here are some options:
- reduce the image buffer sizes. You can do this by reducing the width and height, or if you're currently using an int color map, you can switch to an IndexColorModel based on bytes instead of int's ( a 4x reduction in memory usage).
- cache to disk anything that is not needed nor expected to be needed soon
- don't use an applet.
- have your applet change the default heap sizeif necessary. If your applet is going to do it, it'll have to be a signed applet and you'll have to put code in for all the operating systems you plan to support since they are all different.
- walk the user through changing the default heap size for the java plugin.
Sun Java Plugin - How to Increase the Java Applet Memory Limit
- complain to Oracle that there should be an API for changing the heap size programmatically
- By Moon_E in forum New To JavaReplies: 20Last Post: 07-09-2010, 10:53 PM
- By jon80 in forum Threads and SynchronizationReplies: 1Last Post: 06-30-2009, 06:56 AM
- By arnab321 in forum New To JavaReplies: 2Last Post: 12-07-2008, 07:13 PM
- By lardum in forum New To JavaReplies: 7Last Post: 06-26-2008, 03:30 AM
- By Jimmy Zhang in forum XMLReplies: 0Last Post: 03-03-2008, 10:49 PM