Java concurrency and GC, java concurrent GC
1. synchronized
Re-entry scenario:
Class of the inheritance relationship, subclass calls the method of the parent class ---------- thread security
Methods with synchronized keywords call each other ---------- thread security
2. volatile
Purpose: The variable is visible in multiple threads. When the variable value is changed (note that the value of the main memory is changed), the working memory of each thread is still the original value;
The thread is forced to read from the main memory (shared memory), rather than from the private memory of the thread, so as to be visible in multiple threads.
Note: This key has no atomicity, only visibility, and cannot replace synchronized.
3. Atomic...
It is atomic and has the boolean and Integer types. However, the atomicity of multiple operations cannot be guaranteed when the secondary keyword is used, and the atomicity of this method can only be guaranteed.
4. Communication between threads
Use the wait and noyify methods to implement communication between threads. Both methods are Object methods.
Wait and notify must be used with the synchronized keyword. The wait method releases the lock, and the notify method does not release the lock.
5. java heap
There are new generations and old ages
New Generation: ende, s0, and s1 (or form and)
The ende area stores the newly created objects (created for the first time). The age of the object increases every GC occurrence. This indicates that the object will enter s0 or s1. The default value is
After the object is 15 years old, if the object is still referenced, it will be allocated to the old age. The s0 and s1 areas are of the same size and the roles can be converted to each other.
6. Garbage collection Algorithm
Reference Counting Algorithm: when an object is referenced, the counter is incremented by 1, and the reference is deprecated by 1. However, the counter cannot be processed when the object is referenced cyclically, and each addition or subtraction operation is a waste of performance;
Mark clearing: as the name suggests, it is to mark the referenced object and clear all the unmarked referenced objects. However, a large amount of space fragments are generated, which wastes space;
Replication algorithm: this algorithm is mainly used for new generation. objects are stored in s0 or s1, and only one area is used each time. When garbage collection is performed, the surviving objects (objects not in the old age) copy to s1 or s0, and then clear s0 or s1;
Mark compression method: Based on the mark clearing method, optimization is made to solve the problem of Space fragmentation. The surviving object is compressed to one end of the memory, then perform garbage cleanup (the GC of the jvm middle-aged and old generation uses the mark compression method );
7. Partition Algorithms
The memory is divided into N regions, and each region can be used independently. This solves the performance consumed by scanning the entire memory during GC, and only needs to clean up a certain area;
8. TLAB (Thread Local Allocation Buffer)
When an object is generated too large, it will be directly put into the old age (another is that the object is allocated to the old age when it is older than 15 ), however, pay attention to the Allocation of priority to the TLAB area. Each Thread has a TLAB (Thread Local Allocation Buffer) area, and this Thread exclusive workspace, the TLAB area generated to Accelerate object allocation is a logical concept (in fact, it is also assigned to the thread by Eden). This article mainly introduces jvm and concurrency, and first blog writing, if any error is found, please correct it in time. Thank you.