The Java heap is a runtime data zone, and the instance (object) of the class allocates space from it. The Java Virtual Machine (JVM) stack stores running applicationsProgramAll created objects are created through commands such as new, newarray, anewarray, and multianewarray, but they do not need a program.CodeTo release it explicitly. In general, the heap is responsible for garbage collection by zookeeper. Although the JVM specification does not require special garbage collection technology or even does not require garbage collection, due to the limited memory, during implementation, the JVM has a heap managed by garbage collection. Garbage collection is a dynamic storage management technology that automatically releases objects that are no longer referenced by programs and collectsAlgorithmTo automatically Recycle resources.
Significance of garbage collection
In C ++, objects are occupied until the program ends running and cannot be allocated to other objects before explicit release. in Java, when no object reference is directed to a pair
The memory becomes garbage. A jvm system-level thread Automatically releases the memory block. Garbage collection means that the object no longer needed by the program is "useless information", which will be discarded. When
When an object is no longer referenced, the memory recycles the occupied space so that the space can be used by later new objects. In fact, apart from releasing useless objects, garbage collection can also clear memory record fragments. Because the object is created
And the Garbage Collector to release the memory space occupied by the discarded object. Memory fragments may occur. Fragments are idle memory holes between memory blocks allocated to objects. Fragment moves the occupied heap memory to the end of the heap. The JVM will
The sorted memory is allocated to the new object.
Garbage collection can automatically release memory space and reduce programming burden. This makes Java
Virtual machines have some advantages. First, it can improve programming efficiency. When there is no garbage collection mechanism, it may take a lot of time to solve an obscure storage problem. When programming in Java,
The garbage collection mechanism can greatly shorten the time. Second, it protects program integrity. Garbage collection is an important part of Java's security policy.
One potential drawback of garbage collection is that its overhead affects program performance. The Java virtual machine must track useful objects in the running program,
In addition, useless objects are finally released. This process takes processing time. Secondly, due to the incompleteness of the garbage collection algorithm, some garbage collection algorithms used earlier cannot ensure that 100% of the garbage collection algorithms are completely discarded.
Memory. Of course, these problems can be solved with the continuous improvement of the garbage collection algorithm and the improvement of the operating efficiency of software and hardware.
Garbage collection algorithm analysis
The Java language specification does not clearly indicate which garbage collection algorithm is used by JVM, but any garbage collection algorithm generally requires two basic tasks: (1) discovering useless information objects; (2) reclaim the memory space occupied by useless objects so that the space can be used by the program again.
Most garbage collection algorithms use the root SET
Set). The so-called root set refers to the set of referenced variables (including local variables, parameters, and class variables) that can be accessed by Java programs that are being executed ), the program can use reference variables to access the attributes and
The method of calling the object. The first choice of garbage collection is to determine which are reachable and which are inaccessible from the root, and the objects reachable from the root set are all active objects, which cannot be recycled as garbage, this also includes
Accessible objects. Objects that cannot be reached through any path in the root SET meet the garbage collection conditions and should be recycled. The following describes several common algorithms.
1. reference counting collector)
The reference counting method is the only method that does not use the root set for garbage collection. This algorithm uses the reference counter to distinguish between a surviving object and an object that is no longer in use. Generally, each object in the heap corresponds to a reference counter.
When an object is created and assigned to a variable, the reference counter is set to 1. When an object is assigned to any variable, the reference counter adds 1 each time. When the object is out of scope (this object is discarded and is no longer used), the reference
The counter minus 1. Once the reference counter is 0, the object meets the garbage collection condition.
The reference counter-based Garbage Collector runs fast and does not interrupt program execution for a long time. It is suitable for programs that must run in real time. However, the reference counter increases the overhead of program execution because every time an object is assigned to a new variable, the counter is added with 1. Every time an existing object has a scope, the counter is reduced by 1.
2. Tracing Algorithm (tracing collector)
The tracing algorithm is proposed to solve the problem of reference counting. It uses the concept of root set. The Garbage Collector Based on the tracing algorithm starts scanning from the root set to identify which objects can be
Which objects are not reachable, and mark the reachable objects in some way. For example, you can set one or more places for each reachable object. During scanning and recognition, garbage collection based on tracing algorithms is also called Mark and
Clear (mark-and-sweep) the garbage collector.
3. compacting algorithm (compacting collector)
In order to solve the heap fragmentation problem, tracing-based garbage collection absorbs the compacting algorithm IDEA. In the process of clearing, the algorithm moves all objects to one end of the heap, and another heap
The collector updates all references of all objects it moves so that these references can identify the original
. In the implementation of collectors Based on the compacting algorithm, the handle and handle tables are generally added.
4. Copying algorithm (coping collector)
This algorithm is proposed to overcome the handle overhead and solve the garbage collection of heap fragments. At the beginning, it divides the heap into one object plane and multiple idle planes,
The program allocates space for the object from the object surface. When the object is full, garbage collection based on the coping algorithm scans the active object from the root set and
The active object is copied to the idle surface (so that there is no idle hole between the memory occupied by the active object). In this way, the idle surface is changed to the object surface, and the original object surface is changed to the idle surface, the program allocates memory in the new object area.
A typical garbage collection Algorithm Based on the coping algorithm is the stop-and-copy algorithm, which divides the heap into the object plane and the free area plane. During the switching process between the object plane and the free area, the program is suspended.
5. Generation Algorithm (generational collector)
One defect of the stop-and-copy garbage collector is that the collector must copy all the active objects, which increases the program wait time, which is the cause of the inefficiency of the coping algorithm. In programming
The rule is as follows: Most objects have a short time and a few have a long time. Therefore, the generation algorithm divides the heap into two or more sub-heaps as the generation of objects.
(Generation ). Because most objects have a short time, as the program discards unused objects, the garbage collector collects these objects from the youngest child heap. In the generational Garbage Collector
After running, the last surviving object is moved to the Child heap of the next highest generation. Because the child heap of the old generation is not recycled frequently, it saves time.
6. Adaptive Algorithm (Adaptive collector)
In specific cases, some garbage collection algorithms are better than other algorithms. The Garbage Collector Based on the adaptive algorithm monitors the usage of the current heap and selects the garbage collector of the appropriate algorithm.
View Java garbage collection
1. Run Command Line parameters to view the spam collector
2. Using system. GC () can request Java garbage collection regardless of the garbage collection algorithm used by JVM. There is a parameter in the command line-verbosegc to view the heap memory used by Java. Its format is as follows:
Java-verbosegc classfile
Let's look at an example:
Testgc. Java
Public ClassTestgc {Public Static VoidMain (string [] ARGs ){NewTestgc (); system. GC (); system. runfinalization ();}}
Testgc. bat
Javac testgc. Et et classpath=C: \ Users \ Andy \ Desktop; Java-Xms8m-xmx8m-XX: + printgcdetails-XX: + printgctimestamps-Verbose. GC: D: \ gc_study \ log_gc \ verbose. Log testgcpause
In this example, a new object is created and is quickly reachable because it is not used. After the program is compiled, run the following command: Java-verbosegc testgc and the result is:
[Full GC 168 K-> 97 K (1984 K), 0.0253873 secs]
The machine environment is Windows 2000 +
Jdk1.3.1, the data before and after the arrow 168k and 97k respectively indicate the memory capacity of all surviving objects before and after garbage collection, indicating that the object capacity of 168 K-97 K = 71k is returned
The 0.0253873 k Data in the brackets is the total heap memory capacity, and the collection time is seconds (this time varies with each execution ).
2. Run the Finalize method to view the execution of the Garbage Collector
Before the JVM Garbage Collector collects an object, it is generally required that the program call an appropriate method to release the resource, but without explicitly releasing the resource, java provides a default mechanism to terminate this object and release resources. This method is finalize (). Its prototype is:
Protected void finalize () throws throwable
After the finalize () method returns, the object disappears and the garbage collection starts to be executed. Throws throwable in the prototype indicates that it can throw any type of exception.
The reason for using finalize () is that sometimes it is necessary to adopt a method different from the general method of Java, through the allocation of memory to do something with a C style. This mainly applies
It is a way to call non-Java methods from Java. C and C ++ are currently the only languages that support inherent methods. However, since they can be called and written in other languages
So it can effectively call anything. In non-Java code, you may be able to call the malloc () series functions of C and use it to allocate storage space. And unless
Free (). Otherwise, the storage space will not be released, resulting in a memory "Vulnerability. Of course, free () is a C and C ++ function, so we need
Call it in an inherent method of the Department. That is to say, we cannot use finalize () too much. It is not an ideal place for general cleanup work.
To clear an object, the user of the object must call a clearing method at the location where the object is to be cleared. This is slightly in conflict with the concept of C ++ "destructor. In C ++,
All objects are destroyed (cleared ). Or in other words, all objects "should" be damaged. If you create a C ++ object as a local object, such as creating a C ++ object in the stack (which is impossible in Java ),
The cleanup or destruction work will be performed at the end of the scope of the created object represented by "ending curly braces. If the object is created with new (similar to Java), when the programmer calls the C ++
When the DELETE command (Java does not have this command), it will call the corresponding breaker. If the programmer forgets the vulnerability, the attacker will never be called. What we get is a memory "Vulnerability ",
Other parts of the object will never be cleared.
On the contrary, Java does not allow us to create local (local) objects-New is used in any case. But in Java, there is no "delete" command to release the object, because the Garbage Collector will
This helps us automatically release buckets. Therefore, if we stand in a simplified position, we can say that Java has no destructor because of the garbage collection mechanism. However, with the further study
Knowing the existence of the garbage collector does not completely eliminate the need for the damage collector, or does not eliminate the need for the mechanism represented by the damage collector (and it is absolutely impossible to directly call finalize (), so do your best
). To clear a bucket in some other way, you must still call a method in Java. It is equivalent to a C ++ breaker, but it is convenient without the latter.
The following example shows the garbage collection process and summarizes the previous statements.
Chair. Java
Package GC; Public Class Chair { Static Boolean Export UN = False ; Static Boolean F = False ; Static Int Created = 0 ; Static Int Finalized = 0 ; Int I; Chair () {I = ++ Created; If (Created = 47 ) System. Out. println ( "Created 47" );} Protected Void Finalize (){ If (! Alibaba UN) {Alibaba UN = True ; System. Out. println ( "Beginning to finalize after" + Created + "Chairs have been created" );} If (I = 47 ) {System. Out. println ( "Finalizing chair #47," + "setting flag to stop chair creation" ); F = True ;} Finalized ++ ; If (Finalized> = Created) system. Out. println ( "All" + finalized + "finalized" );}}
Garbage. Java
Package GC; Public Class Garbage { Public Static Void Main (string [] ARGs ){ If (ARGs. Length = 0 ) {System. Err. println ( "Usage: \ n" + "Java garbage before \ n or: \ n" + "Java garbage after"); Return ;} While (! Chair. f ){ New Chair (); New String ("to take up space" );} System. Out. println ( "After all chairs have been created: \ n" + "total created =" + chair. Created + ", total finalized =" + Chair. finalized ); If (ARGs [0]. Equals ("before") {System. Out. println ( "GC ():" ); System. GC (); system. Out. println ( "Runfinalization ():" ); System. runfinalization ();} system. Out. println ( "Bye! " ); If (ARGs [0]. Equals ("after" ) System. runfinalizersonexit ( True );}}
The above program creates many chair objects, and when the spam collector starts running, the program stops creating chair. Because the Garbage Collector may run at any time
We cannot know exactly when it will start. Therefore, the program uses a tag named garbage UN to identify whether the Garbage Collector has started running. With the second flag f, Chair can tell
Main () It should stop generating objects. These two tags are both set inside finalize () and are called during garbage collection. The other two static variables -- created
And finalized-used to track the number of created objects and the number of objects that have been completed by the garbage collector. Finally, each chair has its own
Static) int
I, so we can track the specific number of it. After the chair with the ID 47 is finished, the flag is set to true, and the Chair object creation process ends.
Some Supplements to garbage collection
According to the above descriptions, it can be found that garbage collection has the following characteristics:
(1) unpredictability of the occurrence of garbage collection: Because different garbage collection algorithms are implemented and different collection mechanisms are adopted, it may occur on a regular basis, it may occur when the system idle CPU resources occur, or it may be the same as the original garbage collection, when the memory consumption limit occurs, this is related to the selection and specific settings of the garbage collector.
(2) Accuracy of garbage collection: mainly including 2
(A) The garbage collector can accurately mark living objects; (B) the garbage collector can precisely locate reference relationships between objects. The former is the premise to completely recycle all discarded objects, otherwise it is possible
Memory leakage. The latter is a necessary condition for implementing algorithms such as merging and copying. All reachable objects can be reliably recycled, and all objects can be re-allocated, allowing object replication and Object Memory reduction.
And effectively prevent memory fragmentation.
(3) There are many different types of garbage collectors, each of which has its own algorithms and their performance is different. They both stop the application running when the garbage collection starts, in addition, when the garbage collection starts, the application thread is also allowed to run, and at the same time, the garbage collection is run in multiple threads.
(4) The implementation of garbage collection is closely related to the specific JVM and JVM memory models. Different JVMs may use different garbage collection methods, while JVM
The memory model determines which types of garbage collection can be used by the JVM. Now, hotspot
The memory systems in the series of JVMs adopt advanced object-oriented framework design, which enables the series of JVMs to adopt the most advanced garbage collection.
(5) with the development of technology, modern garbage collection technology provides many optional garbage collectors, and different parameters can be set when configuring each collector, this makes it possible to obtain the optimal application performance based on different application environments.
For the above features, we should pay attention:
(1) do not try to assume the time when the garbage collection occurred. This is all unknown. For example, a temporary object in a method becomes useless after the method is called, and its memory can be released.
(2) Java provides some classes dealing with garbage collection, and provides a method to forcibly execute garbage collection-call system. GC (), but this is also an uncertain side.
Method. Java does not guarantee that every time this method is called, it will be able to start garbage collection, but it will send such an application to JVM, whether or not to really execute garbage collection, everything is unknown.
(3) Select a suitable garbage collector. In general, if the system does not have special and demanding performance requirements, you can use the default JVM option. Otherwise, you can consider using targeted garbage collectors,
For example, the incremental collector is suitable for systems with high real-time requirements. The system has high configuration and many idle resources. You can consider using parallel mark/clear collectors.
(4) The key problem is memory leakage. Good Programming habits and rigorous programming attitude are always the most important. Do not make yourself a small error and cause a large memory vulnerability.
(5) Release reference of useless objects as soon as possible. When using temporary variables, most programmers automatically set the reference variable to null after exiting the scope. This implies that the Garbage Collector collects the object, you must also check whether the referenced object is monitored. If so, remove the listener and assign a null value.
Conclusion
Generally, Java developers do not pay attention to heap memory allocation and garbage collection in JVM. However, fully understanding this feature of Java allows us to use resources more effectively. Note
The finalize () method is the default mechanism of Java. Sometimes you can write your own Finalize method to ensure the explicit release of object Resources.
Http://www.blogjava.net/Jack2007/archive/2008/04/11/192288.html