6, Garbage collection of JVM

Keywords: Java jvm less REST

GC log

-Xmx1024m -Xms1024m -XX:+PrintGCDetails

Heap
PSYoungGen total 305664K, used 26214K [0x00000000eab00000, 0x0000000100000000, 0x0000000100000000)
eden space 262144K, 10% used [0x00000000eab00000,0x00000000ec499be8,0x00000000fab00000)
from space 43520K, 0% used [0x00000000fd580000,0x00000000fd580000,0x0000000100000000)
to space 43520K, 0% used [0x00000000fab00000,0x00000000fab00000,0x00000000fd580000)
ParOldGen total 699392K, used 0K [0x00000000c0000000, 0x00000000eab00000, 0x00000000eab00000)
object space 699392K, 0% used [0x00000000c0000000,0x00000000c0000000,0x00000000eab00000)
Metaspace used 3224K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 351K, capacity 388K, committed 512K, reserved 1048576K

Next, write another code to observe the triggering operation of GC

public class Demo {
    public static void main(String[] args){
        Random random = new Random();
        String  val = "test";
        while (true){
            val+=val+random.nextInt(999999999)+random.nextInt(999999999);
        }
    }
}

[GC (Allocation Failure) [PSYoungGen: 2031K->488K(2560K)] 2031K->676K(9728K), 0.0013870 secs] [Times: user=0.06 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2441K->504K(2560K)] 2629K->1254K(9728K), 0.0010120 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1950K->488K(2560K)] 2700K->1951K(9728K), 0.0011297 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1940K->488K(2560K)] 4796K->4049K(9728K), 0.0012419 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1257K->488K(2560K)] 6212K->5443K(9728K), 0.0009412 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 488K->496K(1536K)] 5443K->5491K(8704K), 0.0005513 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 496K->0K(1536K)] [ParOldGen: 4995K->2727K(7168K)] 5491K->2727K(8704K), [Metaspace: 3281K->3281K(1056768K)], 0.0066911 secs] [Times: user=0.09 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 30K->32K(2048K)] 6938K->6940K(9216K), 0.0004666 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 32K->0K(2048K)] [ParOldGen: 6908K->2030K(7168K)] 6940K->2030K(9216K), [Metaspace: 3281K->3281K(1056768K)], 0.0082892 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 19K->0K(2048K)] 6231K->6211K(9216K), 0.0003457 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 0K->0K(2048K)] [ParOldGen: 6211K->4817K(7168K)] 6211K->4817K(9216K), [Metaspace: 3281K->3281K(1056768K)], 0.0027242 secs] [Times: user=0.08 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(2048K)] 4817K->4817K(9216K), 0.0003852 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(2048K)] [ParOldGen: 4817K->4798K(7168K)] 4817K->4798K(9216K), [Metaspace: 3281K->3281K(1056768K)], 0.0095410 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

GC log analysis

JVM garbage collection

Determination standard of garbage object

The GC work of jvm is mainly aimed at heap memory. Before doing GC work, it is necessary to determine whether the object instance in heap memory is garbage. Generally, the following two algorithms are used to define it

1. Reference counting algorithm

When java is running, when there is a place to reference the object instance, it will add 1 to the object instance, and when the reference fails, it will reduce 1. When the jvm scans memory, it is found that the reference count value of 0 is garbage object, and the count value greater than 0 is active object.

At present, there is no reference counting algorithm in garbage collection algorithm, because it is impossible to determine whether the two objects are garbage objects when they refer to each other.

2. Root search algorithm

The root search algorithm searches downward with "GC ROOTS" as the starting point, and all the objects passing through are combined into a reference chain. In this reference chain, the objects that do not exist are called garbage objects, and the objects in the reference chain are active objects. What kind of object can be called "GC ROOTS"? The following four can

  • Objects referenced in the virtual machine stack (local variable table in stack frame).
  • Object referenced by class static property in method area.
  • Object referenced by a constant in the method area.
  • The reference object of JNI (Native method) in the local method stack.

Garbage collection algorithm

1. Mark sweep

The jvm will scan all the object instances, mark the active objects through the root search algorithm, scan all the objects again, and clear the unmarked objects. Only the cleaning action is taken, without any processing, so there will be a lot of memory fragments in the result.

2. copying

The jvm scans all objects, marks the referenced objects through the root search algorithm, and then applies for a new memory space, copies the marked objects to the new memory space. After copying the surviving objects, the original memory space will be emptied, and the new memory will be the most jvm object storage space. This solves the problem of memory fragmentation, but if there are many objects, it will be very big to reapply new memory space. In the case of insufficient memory, it will have a great impact on the operation of the jvm

3. Mark compact

In fact, tag defragmentation is an optimization of tag removal algorithm. After the whole process of tag removal, memory is defragmented again to move all living objects to one end, which solves the problem of memory fragmentation.

4. Recycling by generations

At present, the common recovery algorithm of jvm is generational recovery, the young generation is mainly replication algorithm, and the old generation is mainly tag sorting algorithm. The reason is that there are many objects in the younger generation, and there are many garbage objects in each garbage collection, and the objects with short life cycle should be reduced as soon as possible, and the number of surviving objects is less. At this time, the replication algorithm is more suitable, as long as the marked objects are copied to another internal storage area, the rest are cleared, and the number of replication is less, and the efficiency is higher; while the old age is The objects screened by the younger generation are more marked and less to be deleted. Obviously, the efficiency of marking is higher.

Posted by beyzad on Mon, 20 Jan 2020 22:25:14 -0800