1

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:

Increase heap size in Java

java.lang.OutOfMemoryError: Java heap space

dynamically increasing java heap space

trincot
  • 317,000
  • 35
  • 244
  • 286
zindarod
  • 6,328
  • 3
  • 30
  • 58
  • 1
    Instead of trying to increate the amount of free memory to *resize* the heap space, you should start your application with the right amount of memory and if necessary redesign your application to make sure it never exceeds that amount. Note that you need to test your application with different memory size and do a benchmark of the best size. – Luiggi Mendoza Aug 09 '13 at 16:03
  • 1 GiB should be enough heap space to run a very large, very complex enterprise app. Do some heap analysis to find out where your application is wasting / holding on to so much memory. – Michael Aug 09 '13 at 16:05
  • @LuiggiMendoza Yes I have doing that for the past day and also using jvisualvm to pinpoint where this heap consumption is really taking place so that I could optimize my code there for better performance. – zindarod Aug 09 '13 at 16:10

3 Answers3

2

You can check in Runtime how much free space you have:

Runtime.getRuntime().freeMemory();

But you cannot increase it.

nkukhar
  • 1,975
  • 2
  • 18
  • 37
  • Maybe I could run the algorithms first then get remaining available heap space at different intervals and if it has reached it's max limit stop running algorithms until some memory has been released back to the heap and then repeat the procedure all over again. – zindarod Aug 09 '13 at 16:06
  • 1
    @HaseebRamish Of course you can do in such way. But keep in mind that this operation is not always relevant. Because it shows free memory in current point of time. So it means if your app uses 400m live objects and 500m garbage objects(that will be removed with GC) freeMemory will show appx 100m (with -Xmx1g) of free memory (and that is not true as you have 100 free + 500 that you will get after GC) – nkukhar Aug 09 '13 at 16:21
1

The size of the heap grows dynamically already, it is not something you need to set.

The maximum size of the heap is fixed (it is the maximum) I suggest you set it so high that you would want the program fail rather than use any more.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
Runtime.getRuntime().freeMemory();

Will give you available free memory in jvm.

To see how much your app used so far, you can do-

Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

You cannot increase dynamically. Even if you could, it wouldn't be a good idea since you do not know in a client's machine, what other processes are being run. This would result to a horrible slow experience.

Your goal should be to find out the reasonable amount of memory a game app can use considering your audience. I can tell you from mobile game development perspective; a game should not take more than 20mb in an android device if you want to support mid range devices.

You can always do memory profiling to see if you can optimize any section of your app.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • Thank you. I know there is something seriously wrong with my code to consume this much memory so soon. I am using jvisualvm to find out where I made a mistake. – zindarod Aug 09 '13 at 16:42
  • @HaseebRamish I hope you will find and fix it. G'luck! :) – Sajal Dutta Aug 09 '13 at 16:45