Handling Garbage Collection with Large Number of Objects

I’m looking for some strategies/advice on how to handle garbage collection. I have a Tile-based game with procedurally generated tiles. Empty tile objects are created and stored in an ArrayList, and the tiles’ images are set when the tile is on screen.

My issue is, whenever I get about ~1000 tiles away from the starting position, I get an exception saying I’ve run out of GC overhead space. I’m thinking I don’t actually need access to anything other than what is on the screen, because I can regenerate/recalculate everything else I need dynamically - most of the tiles and chunks that are storing the tiles are created and used while they’re onscreen but I don’t need them anymore afterwards. I’m not very familiar with how Java actually handles garbage collection, does anybody have any tips/advice or know of any documentation I could read on Java Garbage Collection?

You don’t have to do anything special. Just let the Objects you don’t need anymore go out of scope.

In other words, remove them from any arrays or Lists or other data structures you’re storing them in, and Java will do the rest.

If that doesn’t work, can you provide an MCVE that demonstrates the problem in as few lines as possible?

maybe it’s just the wrong Collection. ArrayList is more or less just a interface/wrapper to a Object[] or T[] primitive array. maybe switch to a HashMap<position,tile> thing.

That exception isn’t about space, it’s about CPU usage. It basically says that garbage collection takes too much processing power. The cause is in most cases that you have a memory leak somewhere. This exception is very close to an out of memory error. If it wouldn’t happen, you would most likely run into an oom exception a couple of seconds later. Try visualvm (it’s included in newer versions of Java) and check for a memory leak.