I want to know how to avoid “Java heap space” error in big 2D worlds. If i have more than 1000 chunks i get java heap space error. I tried to save chunks and delete them from game (keep data about them in computer hard drive). but when i need to load them my FPS drops to 1 for few seconds.
perhaps you need to load a portion of a chunk each frame, rather than loading the whole chunk in one go? then the game play will still continue
I will try it, if there is no way. Thanks!
If the problem is too much chuncks to load, you can differ loading by using a chuncks list that keeps all new chuncks and treat that list in your game loop by just loading X chuncks every Y (milli)seconds.
On the other hand, if the problem is too much datas to load in one chunck, as LiquidNitrogen said, you problaby need to load portion of a chunck every Y (milli)seconds.
Perharps also, the chunck file format could be a bit modified to speed up loading…
THere are ways to increase heap space, you could increase it then allocate an area of the memory for a cache style usage, then use a seperate thread to handle it instead of your game thread, you wont need millisecond accuracy when doing this.
Since you are saying it has to be an infinite world you need to drop chunks anyways.
What does one of your chunks contain and why the hell do you need to load 1000 of them?
@SwordsMiner: Note that a chunk must not be a grid of blocks but can also be a container of some objects.
so @Doubstract: What are your chunks?
I have chunks with 256 blocks (16x16), one block size is 8 pixels. And each block have tons of info about that block.
Which kind of game?
Its something like a terraria or starbound
You will always run into limits with regards to memory and/or file I/O when processing lots of data. You have several options:
- Reduce size required per block (would both decrease memory usage and delays when reading & writing chunks)
- Speed up file I/O (for example using Kryo), or by writing your own fast chunk save/load routines.
- Increase available memory (e.g. using the JVMs -Xmx parameter)
I’d personally also try to optimize things in this order, especially if youre having ‘tons’ of data per block.
… and sometimes one must recognize that the plan was too big for the current computers.
An infinite 2D world is possible if you can keep changes local to the players vinvinity. There is one active area where the player is, and 8 surrounding areas where the player might go to next.
You need these 9 areas in memory.
If the player changes from the middle area to one of the 8 surrounding areas, you drop the three “far” areas from memory and load up the 3 new neighbor areas. You can do the loading in paralell to the game, so the IO won#t block the game.
Choose the areas big enough that the loading is done before the player can traverse an area and it should be fine.
What I do for my game is I have an array that stores objects that are currently loaded, and each time the update loop goes through I check to see the players movement to see if I have to load/unload any more objects.