Today, when I tried to implement a new algorithm (MCTS) for the ongoing contest, I faced weird timeout issues.
My code was taking up to 50ms to call a simple function that just does a 6*12 array copy.
I then decided not to lose too much time and tried something else. I quickly ran into similar issues. But this time, I figured out where it came from.
In this second attemp, I use a static List of custom objects. These objects contains an integer, and a List of another custom object that contains 4 integer, up to a total of 5 integers, plus the List structure.
I clear then fill this list every turn, with about 20k of those objects.
That’s not a lot, but apparently it is enough to make the JVM freeze.
The FAQ says that 768Mb of RAM are allocated to our code, but when I call Runtime.getRuntime().totalMemory() and ***Runtime.getRuntime().maxMemory()***, I can see that 30Mb is allocated, and that it can lazily allocate up to 494Mb when necessary.
I’m not a JVM expert, but when I print Runtime.getRuntime().freeMemory() every turn, I can see the GC cycles, and I can see that sometimes, there is close to no memory left.
My theory :
It seems that at each turn, the GC free less memory than my code needs, and 95% of the time, after about 10 turns, the JVM is trying to reallocate some more (maxMemoty = 494Mb). But this reallocation produces a ~50ms freeze, making my code timeout.
The algorithm I was talking at the beginning of my message was also using a relatively big amount of memory, but again not that much.
Now, a few questions :
- Is my theory relevant ?
- If so, are you aware of that issue ?
- Can you change the JVM settings ?
I know Java isn’t the best language for such ressource-heavy algorithms, and I am used to deal with CPU limitations; but this is much more annoying.
It makes impossible for Java developper to use a lot of useful algorithms.
I’m looking forward to your answers
Thanks for reading.
tl;dr; The JVM seems to allocate only 30Mb of RAM. If more than this is used, the program crashes, and you can start crying. I’m learning C++ for the next contest, since it’s the only language in which you can really write efficient algorithms.