Garbage collector common in JVMS

1. One-way garbage collector

Serial and serial old

Features: Single-thread garbage collection, which stops all threads at garbage collection time. I’m just using up to 200 MB of memory.

2. Multithreaded garbage collector

Insane and Parallel Old.

parNew

Features: multi-threaded garbage collector, throughput first, adaptive allocation and set heap size.

3. Response-first garbage collector. (STW decreases)

CMS(Response First) : Concurrent Mark Sweep

  • Initial markup: GCROOTS mark objects directly connected. (Short time, STW)
  • Concurrent markup: Concurrent markup based on the initial markup. (Long time, concurrent marking, no pause)
  • Remarking: Reference changes may occur during concurrent marking. (Short time, STW)
  • Concurrent cleanup: Tag cleanup can be concurrent cleanup. (Long time, no pause)

To reduce STW time, there are two phases in the concurrency tag

Principle: Do as much re-marking as possible in concurrent cleanup.

  • pre-cleaning
  • Concurrency can interrupt precleanup
1) Pre-cleaning (once)
- If the older object B has been marked as unreachable. If a new generation object (Eden region) holds a reference to B, B is marked as reachable. - If in the old era, some areas were not marked but were marked again during concurrent marking, which areas need to be marked as dirty, to provide direction for re-markingCopy the code
2) Concurrent interruptible preprocessing (cyclic execution)
- When objects in the FROM and to sections hold references to older ages (reachable). Causes the older generation to make concurrent tokens, causing tokens to change. (EdNE region memory is required to reach more than 2M) - If in the old era, some regions were not marked originally, and were marked when concurrent marking, which region should be marked as dirty, to provide direction for re-marking - interruptable conditions: 1) cycle times can be set. 2) Time can be set. 3) The maximum proportion in EDEN district (greater than the proportion exits the cycle).Copy the code

Problems in CMS.

  • CPU sensitive: Because of concurrent processing, it is recommended that the number of CPU cores be greater than or equal to 4
  • Floating garbage: New garbage is generated during the concurrent cleanup phase. Need the next garbage collection to clean up. So you need to trigger the garbage collection in advance.
  • Memory fragmentation: mark cleanup results. Serial Old is generated when there is memory but no continuous memory can be allocated

Second, JVM tuning

1. Generation division of JVM memory.

Divide by active data: Active data runs a week to see how stable it is

2. Expand the new generation and improve the efficiency of GC.

The flow of replication algorithm:

1. Scan the Cenozoic for survival. 2. Copy the object to Survivor 0. (Longer)Copy the code

The reason for increasing GC: The Cenozoic spatial variability leads to a longer GC interval. This will result in more objects becoming garbage in the interval between GC occurrences and not going through the process of copying to survivor 0. (less 2. Duplicate process)

3. How cross-generation references avoid scanning the full heap during Minor GC.

Cross-generation reference problem: Only the young generation will be recycled, but the old generation will hold the new generation reference and need to traverse the old generation.

Solution: Card table

Card table: is a Boolean array. In the old days every space was represented by a Boolean value. When there is a cross-generation reference, the corresponding Boolean value changes, which is the dirty data flag. The dirty data is scanned together when garbage young generation garbage collection is performed.

Third, the String

1. The constant pool

  • Class constant pool: Static constant pool, in the. Class file. Contains: literal and symbolic references.
  • Runtime constant pool: In relation to the runtime, symbolic references are converted to direct references.
  • String constant pool: Disputed pool.

2.String

Char at 1.8 []

After 1.9 is byT []

1) The characteristics of string objects: cannot be inherited, cannot be modified

2) String STR = “ABC” creates “ABC” in the String constant pool

3) String st = new String(“abc”). I’m going to have one more object.

4) String st = “a”+”b”+”c”;

The compiled.class will just be String st = “ABC “;

5) String a = “a”; String b = a +”b”; Have a variable

Append concatenation is done by creating a StringBuilder that returns new String().

6) String a = new String(“aa”).inter(); String b = new String(“aa”).inter();

Results may be inconsistent due to different JDK versions.

A and b are two objects, but the inter() method returns the address of “aa” in the string constant pool.