Reputation: 569
Every time, when the following message appears in the console, I notice a big lag in my game.
[GC (Allocation Failure)
Desired survivor size 133693440 bytes, new threshold 7 (max 15)
[PSYoungGen: 916500K->130553K(918016K)] 1018868K->444063K(1232384K), 1.2505777 secs] [Times: user=1.87 sys=0.43, real=1.25 secs]
[Full GC (Ergonomics) [PSYoungGen: 130553K->127049K(918016K)] [ParOldGen: 313509K->314172K(555008K)] 444063K->441222K(1473024K), [Metaspace: 9374K->9374K(1058816K)], 3.7134878 secs]
I run my program with the following arguments:
-Xms1024M
-Xmx2048M
-XstartOnFirstThread
-XX:+PrintGCDetails
-XX:+PrintClassHistogram
-XX:+PrintTenuringDistribution
-XX:+PrintGCApplicationStoppedTime
Is there any possibility to reduce the time which does the GC needs? How can I see the class of the objects, which are collected by the GC?
Upvotes: 1
Views: 1774
Reputation: 121
The most effective way to reduce the amount of time spent on garbage collection is to reduce the amount of garbage, plainly.
You can use a profiler like JvisualVM (comes with the JDK) to see exactly how your program uses the memory it's allotted and how much time is spent on garbage collection.
In the case of a game program, where you're going for a consistent frame-rate: you'll want to refactor your code such that objects are re-used as much as possible, rather than creating new ones and allowing them to be collected later (which will lead to noticeable lag-spikes whenever garbage collection is inevitably needed). (This is why memory-managed languages like Java aren't the best for games in particular, unfortunately.)
Upvotes: 1