I have written a role playing game in Java. The Human Player plays against the computer so the player and the computer have a set of heroes and monsters respectively to control. I have implemented a set of path finding and other algorithms for target acquiring, path finding, skill, accuracy and health. The problem is that by running algorithms for different characters simultaneously the game soon consumes all the available memory to it and gives Java heap space error. Now I can run my code with this script: (java -Xmx1g Program) and increase the amount Java heap but I don't know how much memory is available to the game and when I miscalculate the amount, the code gives the same error and hangs. My question is: Is there anyway to calculate the amount of available memory at run time? Also I know in Java one cannot increase heap space at run time. So is there any other solution to my problem?
Note: I have read the following questions on SO but couldn't find a solution: