Search code examples
javagarbage-collectionlimitoverhead

Java GC overhead limit exceeded. Lots of hashmaps and a priorityqueque. What can i do?


I have searched for an answer, but I didn't find anything that could answer 100% my question. :D
So, I need to create from an hashmap other 3 new hasmaps, exchanging 2 of its values in 3 different ways (I use map2.putAll(map1) and later switch 2 values), later I have to store the hashmaps in a ordered priorityqueque, and later poll one of them and repeat the process.

The problem starts since I have to do this A LOT of times, since a special condition isn't satisfied...and there's no way to reduce the number of maps I do (well, I may block duped ones, but it would be very expensive i think :/ ).

Actually, I don't really mind if I store all the maps somewere (from what I read, I figured out that's GC fault) or if I have to do other tricks... I just want to get my code working, without (if possible) using stuff like that '-XX:-UseGCOverheadLimit' because it's a sort of homework (even if with no mark), and it must work with 'java classname' from prompt.

The only limit is on time (which is not a problem, 100% sure about it, already tested), and it will be tested with just simple inputs (in which is already good), but I'd like to made it able to do even difficult ones.

I thank you in advice.

Oh, the maps are of this kind: <Integer,Integer> and have size of 16. Unfortunately, I can't post the code here since the homework is not ended yet (even if I submitted mine).


Solution

  • It's hard to judge from the description what happens in your code exactly, but if the memory overhead of allocating and trashing maps is what's killing you you can just cache Map instances and reuse them. Use some object pooling implementation, there's loads around, or just use a Stack as an object pool if there's just one thread.

    The other issue you have is using Integer will cause massive memory churn, and as Integer is immutable, that's a bit of a pain. Replace your Map with the Trove int/int map and you're sorted :)