A use case for a manual GC invocation?

后端 未结 2 656
梦谈多话
梦谈多话 2020-12-12 06:14

I\'ve read why is it bad practice to call System.gc(), and many others, e.g. this one describing a really disastrous misuse of System.gc(). However, there are c

2条回答
  •  情深已故
    2020-12-12 06:45

    For low latency trading systems I use the GC in an atypical manner.

    You want to avoid any collection, even minor ones during the trading day. A way to do this is to create less than 300 KB of garbage a second. This is around 1 GB per hour, or up to 24 GB per day. When you use a 24 GB Eden space it means there is no minor/major GCs. However to ensure a GC occurs at a time which is planned and acceptable, a System.gc() is called at say 5 AM each morning and you have a clean Eden space for the next day.

    There are times, when you create more garbage than expected e.g. failing to reconnect to a data source, and you might get a small number of minor collections. However this only happens when something is going wrong.

    For more details http://vanillajava.blogspot.co.uk/2011/06/how-to-avoid-garbage-collection.html

    by avoiding garbage is not exactly trivial and makes the code harder to maintain.

    Avoiding garbage entirely is near impossible. However 300 KB/s is not so hard for a JVM. (You can have more than one JVM these days on one machine with 24 GB Eden spaces)

    Note if you can keep below 50 KB/s of garbage you can run all week with out a GC.

    Periodically select a server, let no more requests be send to it, let it finished its running requests, let it do its GC, and re-activate the server.

    You can treat a GC as a failure to meet your SLA condition. In this case you can remove a server when you determine this is about to happen from your cluster, Full GC it and return it to the cluster.

提交回复
热议问题