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.

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?

If your taking the minecraft approach thats fine, but still, 1000 chunks of probably 8x8, 16x16, 32x32 blocks or similar data? With the 2D game I am making I could achieve infinite chunks with only 16 ever having to be loaded at a single time.

I have chunks with 256 blocks (16x16), one block size is 8 pixels. And each block have tons of info about that block.

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<size> 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.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org