avoiding FULL GCs

hello,

i noticed, that whenever a full garbage collection kicks in, the complete game is halted for about 0.1 second, which is of course extremely noticeable in a scrolling game.

now i was wondering, if there is any way to avoid these full garbage collections and only do the “regular” garbage collections. maybe by using a jvm argument?

thanks!

There’s no way to completely avoid full GC but there are some tricks you can use.
Firstly, don’t generate so much garbage if you can help it :wink:
Secondly, try tuning the heap size so the amount of garbage that can accumulate before a collection isn’t so big
Thirdly, try the incremental collector (-Xincgc) and/or the parallel collector (can’t remember the switch).
Fourthly, try calling System.gc() at points in the game where a GC won’t be noticed (in between levels for example)

Cas :slight_smile:

thanks for your answer,

onto your second point, setting the heapsizes with -Xms and -Xmx what are good values for that? and what are the default values?

I can’t remember -Xms’s default but -Xmx defaults to 64mb. You might not need quite so much.
There’s a new flag now, -XX:+UseAdaptiveSizePolicy, which tunes the size of the Eden space as well, which helps prevent stuff getting promoted into the main heap too early in the first place.

Cas :slight_smile:

Setting the max heap size is always a trade off.

The larger it is, the less often you will fullGC, but the greater the pause wil lbe when it does.
Also you NEVER want tos et max heap so big you start swapping to disk at run-time.

As much as possible, if you can get away with it (you cant always) it good to let the system work these thinsg out.

I think however you may be putting the cart before the horse. You have not yet determiend why yo uare seeing full GCs during game-play. Most games dont because they only do two kidsn of allocations:
(a) Short lived objects that live in the eden space and never promote.
(b) Objects that exist for the entire level and only get freed up and GCd at level chnage.

Step one of performance tunign is always, always, always to profile your code. It might be that it is an object leak that is causing your entire problem.

ok, thanks again everybody.

it seems i am generating a lot of garbage due to calculation while the player moves around and due to network messages.

btw: which profiler do you recommend (i am using eclipse)
there seem to be really lot of them and i dont wanna try every single one out.

thanks!

edit: btw: is there any way to be notified when a FULL GC happens, other than -verbose:gc? maybe from inside the code?

Maybe you can find something helpful there: Java Garbage Collection Tuning

WHich shoudl be short lived yes?

So it shoiuldnt be causing a full GC. Something esle is definitely going on…

Ive searched and have yet to find a good, usuabl;e, free profiler for Eclipse.

The best free profielr i knwo of is unfrotunately specific to netbeans. You can fidn it on the netbeans page.

there seem to be really lot of them and i dont wanna try every single one out.

nope, EXCEPT by connecting to the JPDA (Java Profilign and Debugging Interface), which is hat a profiler does.
But this has an impact on your code performance.

my secret tip:

http://jiprof.sourceforge.net/

and a tutorial for usage:
http://www-128.ibm.com/developerworks/java/library/j-jip/?ca=dgr-lnxw01JavaProfiling#resources

the profiler takes advantage of the Java 1.5 agent interface, it hooks up direktly to the vm, causing much less impakt on performance of the messured application