1.java Garbage Collection process
The root search algorithm (GC Roots tracing) is used to determine if an object is alive.
The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down,
The path traversed by the search is called the reference chain (Reference Chain), when an object is not connected to a GC roots any reference chain (in the case of graph theory, from the GC roots to the object unreachable)
, it proves that this object is not available.
Objects that are unreachable in the root search algorithm will undergo at least two marking processes:
If the object discovers that there is no reference chain connected to the GC roots after the root search, it will be marked for the first time and filtered for the condition that this object is necessary to execute the Finalize () method.
When the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "no need to execute".
If the object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue named F-queue, and then executed by a low-priority finalizer thread that is automatically created by the virtual machine at a later time.
The so-called "execution" here refers to the virtual opportunity to trigger this method, but does not promise to wait for it to run over.
If an object executes slowly in the Finalize () method, or if a dead loop (more extreme) occurs, it is likely to cause other objects in the F-queue queue to be permanently waiting, or even to crash the entire memory-recycling system.
Later, the GC will mark the object in F-queue for a second small size, if the object is to be in Finalize (), as long as it is re-associated with any object on the reference chain, such as assigning itself (the This keyword) to a class variable or to a member variable of an object. It will be removed from the collection of "will be recycled" at the second mark; If the object has not escaped at this time, it is going to die.
Characteristics of 2.JAVA garbage collection mechanism
Before we discussed the conditions under which the objects in the Java program met the conditions being recycled, and how the garbage collection thread reclaims the "garbage", the following two attributes are followed.
1. Automatic. Java technology provides a system-level thread, the garbage collector thread, to track each piece of allocated memory space, and when the Java Virtual machine is in an idle loop, the garbage collector thread automatically checks each piece of allocated memory space and then automatically reclaims every piece of useless memory that can be reclaimed.
2. Non-predictable. An object becomes garbage, but you cannot assert that the object will be purged immediately after this line, even if the object still occupies memory after the program finishes. Software such as Windows is often out of memory, and Java programs rarely occur because of the ability to reclaim memory automatically. However, because Java does not guarantee the timely removal of useless objects, Java programs can also appear out of memory situation, but this situation is rare. The garbage collection thread executes in a Java program automatically and cannot be enforced, even if the programmer can clearly determine that a piece of memory is useless and should be recycled, and the programmer cannot force the garbage collector to reclaim that block of memory. The only thing a programmer can do is to "suggest" a garbage collector by calling the System.GC method, but it can be executed and when it is executed is unknowable.
What is 3.GC? Why do you have a GC?
A: GC is garbage collection (Gabage Collection) in C + +, C memory management is the most cumbersome thing to do not reclaim memory and the wrong recovery memory caused by system crashes and memory leaks GC automatically monitors the scope of the object to determine if the object exceeds the scope if it is more than the automatic recovery of memory There is no method in Java that shows the collection of allocated memory
4. Advantages and principles of garbage collection. and consider 2 kinds of recycling mechanisms.
A: The GC automatically monitors the allocated objects to see if the object's scope can reach the object in Java where there is no ' scope ' but the variable that references the object has a ' scope ', The garbage collector is often used as a low-level thread to purge and reclaim dead and long-unused objects in the heap in an unpredictable way, which can effectively prevent memory leaks improve memory utilization garbage collection can be divided into: Generational replication garbage collection, token garbage collection, incremental garbage collection
5. What is the basic principle of the garbage collector? Can the garbage collector reclaim memory right away? Is there any way to proactively notify a virtual machine for garbage collection?
A: When creating many objects GC will record the address, size, usage of the object GC uses a graph to record and manage objects in the heap in this way, the GC is responsible for retrieving the memory of the unreachable object when the object is unreachable. We can proactively notify the GC to do garbage collection.
System.GC () But the JVM certificate will execute
Java garbage Collection