OOM and Garbage Collector

Posted by gregchet on Wed, 26 Feb 2020 03:34:46 +0100

1. How many OOM s are common?

①java.lang.StackOverflowError②java.lang.OutOfMemoryError:Java heap space③java.lang.OutOfMemoryError:GC overhead limit exceeded④java.lang.OutOfMemoryError:Direct buffer memory⑤java.lang.OutOfMemoryError:unable to create new native thread⑥java.lang.OutOfMemoryError:Metaspace

/**
 * Description: GC overhead Limit
 * jvm Demonstration of parameter configuration: -Xms10m -Xmx10m -XX:+PrintGCDetails-XX:+MaxDirectMemorySize=5m
 * GC Too long a recycle time throws out OutOfMemroyError, which is defined as more than 98% of the time spent on GC and less than 2% of the heap memory reclaimed
 * GCs are thrown several times in a row only when less than 2% of them are recycled in extreme cases. What happens if the GC Overhead limit error is not thrown?
 * CPU Usage has been 100% and GC has not succeeded
 *
 * @author xinjiao.yu@marketin.cn
 * @create 2020/2/24 21:41
 * @since 2.16.3
 */
public class GCOverheadDemo {
    public static void main(String[] args) {
        int i=0;
        List<String> list = new ArrayList<>();
        try{
            while(true){
                list.add(String.valueOf(++i).intern());
            }
        }catch (Exception e){
            System.out.println("**************i:"+i);
            e.printStackTrace();
            throw e;
        }
    }
}
/**
 * Description: Configuration parameters: -Xms10m-Xmx10m-XX:+PrintGCDetails-XX:MaxDirectMemorySize=5m
 * Failure Phenomena: Exception in thread "main" java.lang.OutOfMemroyError:Direct buffer memory
 * Causes:
 * Writing NIO programs often use ByteBuffer to read or write data, which is an I/O method based on channel channel and buffer buffers.
 * He can use the natice library to allocate out-of-heap memory directly, and then use a DirectByteBuffer object stored in the java heap as a reference to that memory
 * This significantly improves performance in some scenarios because it avoids copying data back and forth between the java heap and the native heap
 *
 * ByteBuffer.allocate(capability)The first way is to allocate JVM memory, which is under GC jurisdiction and is relatively slow due to the need for copies
 * ByteBuffer.allocateDirect(capability)The second method is to allocate OS local memory, which is not under GC jurisdiction and does not require a copy so it is fast
 * However, if local memory is allocated continuously and heap memory is rarely used, then the JVM does not need to execute GC and the DirectByteBuffer object will not be recycled, at which point the heap is full, but the local memory has been exhausted
 * Trying to allocate local memory again will cause OutOfMemoryError and the program will crash directly
 *
 * @author xinjiao.yu@marketin.cn
 * @create 2020/2/24 22:00
 * @since 2.16.3
 */
public class DirectBufferMemoryDemo {
    public static void main(String[] args) {
//        System.out.println("Configured maxDirectMemory:"+(sun.misc.VM.maxDirectMemory()/(double)1024/1024)+"MB");
        ByteBuffer bb = ByteBuffer.allocateDirect(6*1024*1024);
    }
}
/**
 * Description:
 * JVM Parameter: -XX:MetaspaceSize=8m-XX:MaxMetaspaceSize=8m
 * java8 Use metaspace instead of permanent generation in the future
 * metaspace Is the implementation of the method area in hotspot. The biggest difference between metaspace and persistent generation is that metaspace does not exist in virtual machine memory but uses local memory
 * That is, in java8, the class metadata is stored in the native mememeory of the metaspace
 * Permanent Generation (replaced by metaspace after java8) stores information:
 * Class information loaded by virtual machine
 * constant pool
 * Static variable
 * Even compiled code
 *
 * @author xinjiao.yu@marketin.cn
 * @create 2020/2/25 13:33
 * @since 2.16.3
 */
public class MetaspaceOOMDemo {
    class OOMTest{}

    public static void main(String[] args) {
        int i = 0;
        while(true){
            i++;
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMTest.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    return methodProxy.invokeSuper(o,args);
                }
            });
        }
    }
}

2.Four ways of garbage collection:
 

Serial garbage collector (Serial) is designed for a single-threaded environment and uses only one thread for garbage collection. It suspends all user threads and is not suitable for production environments. Parallel garbage collector (Parallel) has multiple garbage collection threads working in parallel, where user threads are suspended and concurrent garbage collector (CMS) user threads and garbage collection threads execute simultaneously (not necessarily)Is parallel, may execute alternately) does not need to pause user threads, Internet companies use him more, for scenarios where response time is required.(4) The G1 garbage collector divides the heap memory into different areas and recycles it concurrently.

3.G1 Garbage Collector:

The.G1 garbage collector is a server-side garbage collector used in multiprocessor and large memory environments. It achieves high throughput and meets the garbage collection pause time requirements as much as possible. It also has the following characteristics: 1. Like the CMS collector, it can execute concurrently with application threads; 2. Clean up idle time is faster; 3. It takes more time to previewMeasuring GC pause time Don't want to sacrifice a lot of throughput Don't need a larger Java Heap, he performs better than CMS in the following ways: G1 is a garbage collector with a clean memory process, does not generate a lot of memory fragmentation, G1 has a more controllable top Thr World (STW), G1 adds a prediction mechanism on the pause time, and users can specify the expected pause time.The main change in G1 is that the memory regions such as Eden,Survivor,Tenured are not contiguous, but instead become a region of the same size, with each region ranging from 1 m to 32 M. One region may belong to Eden,Survivor,Tenured memory regions.

4.G1 Garbage Collector Features:

G1 can make full use of CPU.Hardware advantage of multi-core environment, try to shorten STW2 G1 as a whole using tag-collation algorithm, partially through the replication algorithm, will not produce memory fragmentation 3 macroscopically, G1 is not distinguished between the younger and the older, the memory is divided into several independent sub-areas 4 G1 collector will mix the entire memory area together, but its own is still in a small range for yearsThe distinction between the younger and the older retains physical isolation but is no longer a collection of regions and does not require regions to be continuous G1 Although it is a generational collector, there is no physical distinction between the younger and the older in the entire memory partition, nor does it require a completely independent survivor heap to prepare for replication. G1 has only the logical concept of generational.Each partition may switch back and forth between generations as G1 runs.

5.liunx command:

(1) top,Reduced version of uptime system performance command II mpstat-P ALL 2 view all CPU information III pidstat-u 1-p process number: amount of decomposition information per process using CPU III free application available memory IV view additional: pidstat-p process number-r sample interval seconds df-h view disk remaining space iostat-xdk 2 3: disk IO performance evaluation pidstat-d-pSampling Interval Time-p Process Number: View Disk IO

6. If the cpu usage in the build environment is too high, would you like to talk about your analysis ideas and positioning?

First, use top command to find out the highest proportion of cpu. Second, locate ps-ef or jps to know what kind of background program is causing. Third, locate specific threads or codes: ps-mp process-o THREAD, tid.Time, parameter explanation-m shows the CPU usage time of all threads-p-p id processes, -o changes the parameter to a user-defined format jstack process id | grep TID (hexadecimal thread id lowercase English) -A60

249 original articles published. 18% praised. 70,000 visits+
Private letter follow

Topics: Java jvm less