Start by asking yourself a question

This STW problem is caused by the dump thread deadlock checking heap DUpm

** * Several other factors that cause STW: * dump thread * deadlock check * heap dupm * garbage collection algorithm: to make STW longer, increase the space of the old generation and use the serial old garbage collection algorithm. ** * JVM garbage collection parameters: -Xms512m -Xmx512m -Xmn4m -XX:+PrintGCDetails -XX:+UseSerialGC * </code> * @author zhanghua * */ public class GenerateSTW Private List<byte[]> Content =new ArrayList<byte[]>(); public static void main(String[] args) { GenerateSTW stw=new GenerateSTW(); stw.start(); } private voidstart() {
		while(true){ try { content.add(new byte[1024]); } catch (OutOfMemoryError e) {// Clean up part of the space when it is no longer available and continue running, which will soon result in the next garbage collectionfor(int i=0; i<1024; i++){ content.remove(i); } } } } }Copy the code

Is there a way to minimize one STW pause? What are the downsides?

A: Reduce one STW pause. Here I answer in three ways.

1. Garbage algorithm selection

Garbage algorithm selection: Now all are multi-core CPUS, can use parallel and concurrent collectors, if the response time optimization system application, then jdK6 version is mediocre

The garbage collection algorithm chosen is: XX:+UseConcMarkSweepGC, namely CMS collector, this collector garbage collection time is short, but the total garbage collection time is longer, so as to reduce the amount of swallowed, the algorithm uses mark-sweep, The concurrent collector does not compress or defragment the memory space, so it can become “fragmented” after running for a while, making it less efficient. CMSFullGCsBeforeCompaction after how much time the GC to run this value is set to compress memory space

2. One is that the program uses heap Settings

Program using heap Settings: should be based on the program running conditions, through the Jvm garbage collection analysis, set a more appropriate heap size, do not mean that the heap is set too large, resulting in a large amount of program recycling space, so it will lead to a long STW time.

3. Release useless objects as soon as possible

Release unused objects early: If unused objects are used, set null as early as possible and recycle them as early as possible to reduce the full GC pause time