Java Virtual Machine (2) automatic memory management mechanism, Java Virtual Machine

Source: Internet
Author: User

Java Virtual Machine (2) automatic memory management mechanism, Java Virtual Machine
1. Summary

In the deep understanding of Java Virtual Machine (JVM) study Note 1 Automatic Memory Management Mechanism (I), we listed the following issues:

This article mainly summarizes question 4, question 5 and Question 7. The overall situation is as follows:


2. algorithms used to determine whether an object is alive

There are two common algorithms used to determine whether an object is alive: one is the reference counting algorithm and the other is the root search algorithm. For example, the Python language uses the reference counting algorithm for memory management. Java, C #, and ancient Lisp use the root search algorithm to determine whether an object is alive. The following describes the two algorithms.

2.1 reference counting algorithm

The main steps for referencing a counting algorithm are as follows:

This algorithm is easy to implement, but it is difficult to solve the issue of circular reference between objects. Therefore, Java does not use this algorithm to manage memory. 2.2 search algorithms
The root search algorithm uses a series of objects named "GC Roots" as the starting point to start searching down from these nodes. The paths searched are called reference chains, when an object is connected to GC Roots without any reference chain, it is proved that this object is unavailable. 3. GC Algorithm
3.1 Mark-Sweep Algorithm
The algorithm is divided into two phases: "tag" and "clear". First, all objects to be recycled are marked and all marked objects are recycled after the tag is complete. The algorithm is as follows:
This algorithm is the most basic collection algorithm. The subsequent collection algorithms are based on this idea and the shortcomings are improved. 3.2 Copying Algorithm


To solve the efficiency problem, the replication algorithm divides the available memory into two equal-sized blocks by capacity, each using only one of them fast. When the memory of this block is used up, we will talk about copying the living objects to the other block, and then clearing the used memory space. The algorithm is as follows:


Today, commercial virtual machines all use this collection algorithm to reclaim the new generation.

3.3 Mark-Compact Algorithm


The replication collection algorithm performs many replication operations when the object survival rate is high, and the efficiency will be very low. Therefore, this algorithm cannot be used directly in the old age. To solve this problem, the tag-sorting algorithm was born. The algorithm tag process is still the same as the "tag-clear" algorithm, but the subsequent steps do not directly clean the recyclable objects, but move all the surviving objects to one end, then, the memory outside the end boundary is cleared directly. The algorithm is as follows:



3.4 generation Collection Algorithms


Currently, the garbage collection algorithm is used for commercial virtual machines. This algorithm divides the memory into several blocks based on different object lifecycles. Java heap is generally divided into the new generation and old generation. The new generation generally uses the replication algorithm, and the old generation generally uses the tag-clearing algorithm or the tag-sorting algorithm.

3.5 Advantages and Disadvantages of several algorithms
4. Garbage Collector

Knowledge about the garbage collector is not introduced here, interested can refer to the http://book.51cto.com/art/201107/278913.htm

5. Summary of the garbage collector Parameters

6. Some Supplements to GC
According to the above descriptions, it can be found that garbage collection has the following characteristics:

1. the unpredictability of garbage collection: Because different garbage collection algorithms are implemented and different collection mechanisms are adopted, it may occur at regular intervals, 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 includes two aspects:
A. the garbage collector can accurately mark living objects;
B. the garbage collector can precisely locate the reference relationship between objects.
The former is the premise to completely recycle all discarded objects, otherwise it may cause 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. This allows object replication and Object Memory reduction, effectively preventing memory fragmentation.

3. there are now many different types of garbage collectors, each with its algorithms and their performance is different, both when the garbage collection begins to stop the application running, 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 adopt different garbage collection methods, and the JVM memory model determines which types of garbage collection can be used by the JVM. Now, the memory systems in the HotSpot series JVM use advanced object-oriented framework design, which enables the series JVM to adopt the most advanced garbage collection.

5. with the development of technology, modern garbage collection technology provides many optional garbage collectors, and you can set different parameters 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 to deal with garbage collection, and provides a method to forcibly execute garbage collection-call System. gc (), but this is also an uncertain 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, incremental collectors are 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 issue 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. -------------------------------------------------- Full text ---------------------------------------

Copyright statement: I feel very grateful if I still write well, I hope you can use your mouse and keyboard to give me a thumbs up or give me a comment! _______________________________________________________________ You are welcome to reprint. I hope to add the original address while you reprint it. Thank you for your cooperation.

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.