A GCRoots.

1. What if an object can be reclaimed?

Reference counting algorithm

Maintains a counter +1 if there is a reference to the object, and -1 if there is a reference to the object. Circular references cannot be resolved.

Accessibility analysis algorithm

Start with a set of root objects called “GC Roots” and walk down. Objects that are not traversed and form a pathway with GC Roots are marked as “reclaimed”.

2. Which objects can be used as GC Roots?

  1. Object referenced in the virtual machine stack (a local variable in the stack frame).
  2. Objects referenced in the native method stack.
  3. The object referenced by the constant in the method area.
  4. The object referenced by the class static property in the method area.

2. The JVM parameter

1. JVM architecture

The basic structure is the same, except that Java8 has eliminated the previous “permanent generation” and replaced it with “Metaspace”, which is essentially the same. The “permanent generation” uses the JVM’s heap memory, while the “meta-space” is the native physical memory used directly.

2. Three types of JVM parameters

Standard parameters, such as -version, -help, and -showversion, are almost unchanged.

X parameter: rarely used, such as -xint, to explain execution mode; -xcomp, compiler mode; -Xmixed, enable the mixed mode (default).

XX parameter: important, used for JVM tuning.

3. XX parameters

Boolean formula: -xx :+ an attribute, -xx :- an attribute, to enable or disable a function. For example, -xx :+PrintGCDetails enables GC details.

KV key value type formula: -xx: attribute key= value. For example, -xx :Metaspace= 128M and -xx :MaxTenuringThreshold=15.

Xms and Xmx parameters: -xms and -xmx are common for setting the initial heap size and maximum heap size. At first glance, it looks like neither an X parameter nor an XX parameter. In practice -xms is equivalent to -xx :InitialHeapSize, and -xmx is equivalent to -xx :MaxHeapSize. So -xms and -xmx are XX parameters.

4.JVM view parameters

To view a parameter: use JPS -l with jinfo-flag JVM parameter PID. View the Java process with jsp-L and select a process number.

Jinfo-flag PrintGCDetails 8888 You can view the PrintGCDetails parameter of the 8888 Java process.

View all parameters: Use JPS -L with jinfo-flags PID to view all parameters. You can also use Java -xx :+PrintFlagsInitial

View the modified parameters: Use Java -xx :PrintFlagsFinal to view the modified parameters, similar to the above. Only after modification := instead of =.

View common parameters: If you do not want to view all parameters, use -xx :+PrintCommandLineFlags to view common parameters.

5. Common JVM parameters

-xmx / -xms Maximum and initial heap size. The default value is 1/4 of the physical memory, and the default value is 1/64 of the physical memory.

-xss is equivalent to -xx :ThresholdStackSize. This parameter is used to set the size of a single stack. The default value is 0, which does not mean the stack size is 0. It has different values depending on the operating system. For example, 64-bit Linux systems are 1024K, while Windows systems rely on virtual memory.

-XMN Cenozoic size, generally not adjusted.

-xx :MetaspaceSize Sets the metasspace size.

-xx :+PrintGCDetails Displays GC collection information, including GC and Full GC information.

-xx :SurvivorRatio Ratio of Eden zones to two Survivor zones in the new generation. The default value is 8:1:1. -xx :SurvivorRatio=4 changes to 4:1:1

-xx :NewRatio Indicates the ratio between the old and new ages. The default value is 2, that is, the old occupies 2 and the Cenozoic occupies 1. If changed to -xx :NewRatio=4, then the old age accounted for 4, the Cenozoic accounted for 1.

-xx :MaxTenuringThreshold Specifies the time when the new generation will enter the old age. By default, the new generation will enter the old age after escaping 15 GC. If set to 0, objects are not allocated in the new generation and go directly to the old generation.

The big four references

Strong references Objects created using the new method are strong references by default. An OutOfMemoryError thrown during GC does not recycle objects, even if memory is low, or even if they die.

Soft references need to use the Object. The Reference. SoftReference to display to create. If there is enough memory, it is not collected during GC. If the memory is insufficient, it is reclaimed. Often used for memory-sensitive applications such as caching.

Weak references need to use the Object. The Reference. WeakReference to display to create. Whether or not there is enough memory, it is reclaimed during GC and can also be used for caching.

WeakHashMap Traditional HashMap does not reclaim key/value pairs even if key==null. However, if WeakHashMap is used, when memory is insufficient and key==null, the key-value pair will be reclaimed. See WeakHashMapDemo.

Virtual references Soft applications and weak references can get objects through the get() method, but virtual references cannot. Vref can be used by GC at any time. It must be used in conjunction with ReferenceQueue. The sole purpose of setting up a virtual reference is to receive a notification for subsequent action when the object is reclaimed, a bit like Spring’s post-notification. See PhantomReferenceDemo.

Reference queue Weak references and virtual references are collected and put into the reference queue, which can be obtained by the poll method