Comprehensive analysis of Java's garbage collection mechanism 3

Source: Internet
Author: User

The above program creates many chair objects, and at some point after the garbage collector starts running, the program stops creating the chair. Because the garbage collector may run at any time, we don't know exactly when it starts. Therefore, the program uses a tag called Gcrun to indicate whether the garbage collector has started running. Using the second tag, F,chair can tell main () that it should stop generating the object. Both of these tokens are set internally in Finalize () and are tuned for garbage collection. The other two static variables--created and finalized--are used to track the number of objects that have been created and the number of objects that the garbage collector has finished finishing. Finally, each chair has its own (non-static) int I, so it can track how much it is specific. Chair with number 47 completes the finishing touches, the tag is set to true to end the Chair object creation process.

A few additions to garbage collection

After the above description, you can find that garbage collection has the following characteristics:

(1) The unpredictable nature of garbage collection: Due to the implementation of different garbage collection algorithms and the use of different collection mechanisms, it may be timed to occur, it is possible to occur when the system is idle CPU resources, it may be the same as the original garbage collection, wait until memory consumption limit occurs, This is related to the choice of the garbage collector and the specific settings.

(2) The accuracy of garbage collection: mainly includes 2 aspects: (a) The garbage collector can accurately mark the living objects; (b) The garbage collector is able to pinpoint the referential relationships between objects. The former is a precondition for completely reclaiming all discarded objects, or it may cause a memory leak. The latter is necessary for the realization of algorithms such as merging and copying. All unreachable objects can be reliably recycled, all objects can be reassigned, objects are copied and object memory is shrunk, which effectively prevents fragmentation of memory.

(3) There are many different kinds of garbage collectors, each with its algorithm and its performance, both stopping the application when garbage collection starts, and allowing the application's thread to run when garbage collection starts, as well as the garbage collection multithreading at the same time.

(4) The implementation of garbage collection is closely related to the specific JVM and the JVM's memory model. Different JVMs may have different garbage collections, and the JVM's memory model determines what types of garbage collection the JVM can take. The memory systems in the HotSpot family of JVMs are now designed with an advanced object-oriented framework, allowing the JVM to use state-of-the-art garbage collection.

(5) With the development of technology, modern garbage collection technology provides many optional garbage collectors, and in the configuration of each collector can also set different parameters, which makes it possible to obtain the best application performance according to the different applications environment.

   Shenyang 463 Plastic Surgery HospitalProgram for the above characteristics, we use the time to pay attention to:

(1) Do not attempt to assume that garbage collection occurs at any time, all of which are unknown. For example, a temporary object in a method becomes a useless object when the method call is complete, and its memory can be freed at this time.

(2) Java provides a number of classes that deal with garbage collection, and provides a way to enforce garbage collection-calling System.GC (), but this is also an indeterminate approach. Java does not guarantee that each call to this method will be able to start garbage collection, it will only issue to the JVM such a request, whether or not to actually perform garbage collection, everything is unknown.

(3) Pick a garbage collector that suits you. In general, if your system does not have special and demanding performance requirements, you can use the JVM's default options. Otherwise, you might consider using a targeted garbage collector, such as an incremental collector, which is more suitable for systems with higher real-time requirements. The system has a high configuration, there are more idle resources, you can consider using the parallel tag/purge collector.

(4) The key is also difficult to grasp the problem is the memory leak. Good programming habits and rigorous programming attitude is always the most important, do not let your own a small error caused a large memory leak.

(5) Release the reference of the useless object as soon as possible. Most programmers use temporary variables when the reference variable is automatically set to null after exiting the active domain (scope), implies that the garbage collector collects the object, and must be aware that the referenced object is listening, and if so, remove the listener and then assign a null value. Shenyang 463 Plastic Surgery Hospital http://www.hengnaya.com/

Concluding remarks

In general, Java developers can not focus on the allocation of heap memory and garbage collection in the JVM, but fully understanding the Java feature allows us to use resources more efficiently. Also note that the Finalize () method is the default mechanism for Java, and sometimes you can write your own Finalize method to ensure explicit release of object resources.

Comprehensive analysis of Java's garbage collection mechanism 3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.