JAVA garbage collection mechanism overview, java garbage collection Overview
Garbage collection is a major knowledge point in JAVA and also a famous knowledge point. After all, JAVA always carries garbage collection when it claims to be advanced. As a result, it has become a frequent visitor in the interview. The interviewer will explain what garbage collection is and how it works. Of course, although, and certainly, the interviewer who asked you about garbage collection, he only knows nothing about 99%. When the interviewer asks you what is the garbage collection mechanism, you should seriously ask: Which of the VMS do you ask about?
The concept and algorithm involved in garbage collection are complex. If you want to understand all the details, it is worth the candle. However, if we master the following summary of the garbage collection mechanism, we believe that you will not lose points in most interviews.
1. What is garbage?
For example, if you don't need an object, it is garbage, for example:
Public void test01 (){
User user = new User ();
//...
}
After the test01 method is executed, the user object is useless, so it is junk.
Ii. Why is garbage collection required?
We know that objects are stored on the heap. How big is the heap? Although the command parameters can be adjusted, in a 32-bit system, the Java heap size is usually set to 2 GB, and 500 MB is allocated to the new generation (YoungGen) 1.5 GB is allocated to the OldGen space. Even if it is 64-bit, think about how much memory our PC hardware can have.
Therefore, useless garbage is recycled to make out the memory space for other objects.
Iii. JDK's default HotSpot VM garbage collection mechanism
1: heap memory Classification
To understand this mechanism, you must first understand the classification of the heap. Yes, we only know that the object exists on the stack, but we do not know that there are several spaces in the heap, as shown in:
Young/New Generation
It is also divided into Eden and two consumer vor spaces. All newly created objects will be allocated to the new generation,
Old/Tenured Generation age
Objects used to store programs that survive after several garbage collection
(PS: Permanent Generation non-heap memory, used to store static files, such as Java classes and methods. Persistent generation has no significant effect on garbage collection.)
2: recycling order
The execution sequence of each space is as follows:
Iv. Garbage Collector and collection Algorithm
Both types of collectors have their own collectors, and each collector uses different algorithms. Remember, for beginners, we do not need to master every algorithm principle.
New Generation collectors: Serial, PraNew, Parallel Scavenge
Collectors used in earlier years: Serial Old, Parallel Old, and CMS
The algorithm is as follows,
Serial collector (replication algorithm)
The new generation of Single-thread collectors, marking and cleaning are single-threaded, with the advantage of being simple and efficient.
Serial Old collector (tag-sorting algorithm)
The single-thread collector of the old generation and the old version of the Serial collector.
ParNew collector (STOP-copy algorithm)
The new generation collector can be considered as a multi-threaded version of the Serial collector, which has better performance than Serial in a multi-core CPU environment.
Parallel Scavenge collector (STOP-copy algorithm)
The parallel collector pursues high throughput and uses the CPU efficiently. Throughput is generally 99%, throughput = user thread time/(User thread time + GC thread time ). Suitable for background applications and other scenarios with low interaction requirements.
Parallel Old collector (STOP-copy algorithm)
Earlier versions of Parallel Scavenge collectors, Parallel collectors, and throughput first
CMS (Concurrent Mark Sweep) Collector (tag-cleanup algorithm)
High concurrency, low pause, pursuit of the shortest GC recovery pause time, high cpu usage, fast response time, short pause time, multi-core cpu pursuit of high response time
5. When will garbage collection be executed?
Two types of garbage collection are available: Scavenge GC and Full GC.
Scavenge GC is triggered when a new object is generated and the Eden application fails. At this time, the new generation of garbage collection will be carried out.
When Tenured is full, Perm is full, and System. gc () is shown to dynamically change the allocation policies of Heap domains after the last GC call. Full GC is executed.
Note: no matter the type of garbage collection, it does not mean that all the garbage will be recycled. Instead, it is determined by the algorithm itself that a certain amount of garbage will be removed within a period of time, we cannot know the time and quantity.
The above is the garbage collection mechanism you must know.