Click to enter _ more _java thousand ask
1, how to judge the garbage object
The first step in garbage collection is to first need an algorithm to mark what is garbage and then dispose of the garbage. The usual programming language will be judged using one of the following algorithms:
- Reference count (referencecounting) algorithm
This method is relatively simple and intuitive, the core idea is to add a reference counter to each object, when referenced +1, reference failure-1, equals 0 o'clock means that the object is not referenced, can be recycled.
Flashplayer/python uses this algorithm to be simple and efficient. However, java/c# does not adopt this algorithm because the algorithm does not solve the problem of object referencing, that is, when two objects are referenced by each other and are not referenced by other objects, their reference count is 1, although not 0, but they are still garbage objects that can be reclaimed.
- Root search (GC Roots tracing) algorithm
The rationale is: The gcroot object acts as the starting point (root). If it is accessible from the root to an object, the object is called an object that can be reached (a surviving object, not a recyclable object). Otherwise, the object cannot be reached and can be recycled.
2, the new generation how to clean up rubbish
Garbage cleanup algorithm
The new generation of garbage collectors often assumes that most objects have very short survival times and that only a few objects survive longer. According to this hypothesis, the new generation of garbage removal algorithm is mainly the replication algorithm (Copying). By copying the algorithm, you can clean up objects that are not referenced, and you can put objects that are still alive after several (configurable) cleanup into the Laosheng generation.
Understanding heap Memory Look here: What is the Java heap memory?
Understand the copying algorithm of the heap look here: [What is the Cenozoic copying algorithm][3]
[3]:
Garbage Cleanup Trigger mode
The new generation uses a "free pointer" to control the GC trigger, which keeps the last object in the next generation allocated, and when there is an additional object to allocate memory, it is used to check if the space is sufficient to trigger the GC. The new generation of GC is often called the young GC, sometimes called minor GC.
In the process of continuously allocating objects, the object will follow the replication algorithm from the Eden area to the Survivor area, and finally to the Laosheng generation.
Common configuration
3, Laosheng generation How to clean up rubbish
Garbage cleanup algorithm
Laosheng generation and Cenozoic, the object survived longer and more stable, so the use of marker/collation (also known as tag-Compact, mark-compact) algorithm.
See the mark algorithm for the heap here: [What is Laosheng's Mark algorithm][4]
[4]:
Garbage Cleanup Trigger mode
GC of the Laosheng generation, usually called the full GC, also called the major GC. The GC is triggered by the Laosheng generation, but in general the frequency is not high:
Insufficient space for old generation
When tuning the object in the new generation of GC as far as possible to be recycled, so that the object in the Cenozoic to survive for some time and do not create too large objects and arrays to avoid directly in the Laosheng generation object.
Pemanet Generation Space Shortage
Increase Perm Gen space to avoid too many static objects.
The average size of the GC promoted to Laosheng is greater than the remaining space of the Laosheng generation
Control the proportion of the new generation and the old generation.
Call System.GC manually ()
Garbage collection should not be triggered manually, depending on the JVM's own mechanism.
Common configuration
The initial space of the heap:-XMS, you can calculate the size of the Laosheng generation is-xms minus-XMN
Maximum heap space:-xmx
Maximum age threshold:-xx:maxtenuringthreshold, that is, the number of generations of new generation transferred to Laosheng
Laosheng and Cenozoic ratios:-xx:newratio, for example, 3, indicates that the ratio of Cenozoic to Laosheng generation is 1:3
4. What are the garbage collection methods?
The above is the basis of the Java garbage collection mechanism, the JVM provides us with a number of alternative recycling methods, known as the garbage collector. There are 4 main types:
- Serial garbage collector (Serial garbage Collector)
- Parallel garbage collector (Parallel garbage Collector)
- Concurrent tag Scan garbage collector (CMS garbage Collector)
- G1 garbage collector (G1 garbage Collector)
Here we will describe in detail the various types of garbage collector: [What are the ways Java garbage collection is][5]
[5]:
Java FAQ _07JVM Architecture (008) _java garbage collection mechanism