Jvm gc knowledge

Source: Internet
Author: User

the purpose of garbage collection is to clear objects that are no longer in use. GC determines whether to collect the object by determining whether the object is referenced by the active object. Two common methods are reference count and Object Reference traversal.
(1) reference count: The reference count stores the number of all references to a specific object, that is, when the Program is used to create a reference, the JVM must increase or decrease the number of references. When the reference number of an object is 0, garbage collection can be performed.
(2) object reference traversal: In the early days, JVM used reference counting. Currently, most JVMs use Object Reference traversal. Object Reference traversal starts from a group of objects and recursively identifies reachable objects along each link in the entire object graph. If an object cannot be reached from one (at least one) of these root objects, it is collected as garbage. In the object traversal stage, GC must remember which objects can arrive in order to delete inaccessible objects. This is called a mark (marking) object.
GC then deletes the inaccessible objects. During deletion, some GC simply scans the stack, deletes untagged objects, and releases their memory to generate new objects. This is called clearing ). The problem with this method is that the memory will be divided into many small segments, but they are not enough for new objects, but they are very large in combination. Therefore, many GC can reorganize the objects in the memory and compact them to form available space.
for this reason, GC needs to stop other activities. This method means that all the work related to the application is stopped and only GC is run. As a result, many mixed requests are added or subtracted during the response. In addition, more complex GC increases or runs at the same time to reduce or clear application interruptions. Some GC uses a single thread to complete this task, while others use multiple threads to increase efficiency.

Several garbage collection mechanisms
1. Mark-clear collector: This collector first traverses the object graph and marks reachable objects, and then scans the stack to find unmarked objects and release their memory. This type of collector generally uses a single thread to work and stops other operations.
2. Mark-compression COLLECTOR: it is sometimes also called Mark-clear-compression collector, which has the same mark stage as Mark-clear collector. In the second stage, the tag object is copied to the new domain of the stack to compress the stack. This collector also stops other operations.
3. Replication COLLECTOR: This collector divides the stack into two fields, which are often called semi-space. Only half of the space is used each time, and the new object generated by JVM is placed in the other half of the space. During GC running, it copies the reachable objects to the other half of the space, thus compressing the stack. This method is applicable to objects with a short lifetime, and objects with a long lifetime of continuous replication result in lower efficiency.
4. incremental COLLECTOR: the incremental collector divides the stack into multiple domains and collects garbage from only one domain at a time. This can cause small application interruptions.
5. Generational COLLECTOR: This collector divides a stack into two or more fields to store objects of different life cycles. New objects generated by JVM are generally placed in a certain domain. After a period of time, the existing object will get the validity period and be transferred to a domain with a longer life cycle. The generational collectors use differentAlgorithmTo optimize performance.
6. Concurrent COLLECTOR: the concurrent collector runs simultaneously with the application. These collectors generally have to stop other operations at a certain point (such as during compression) to complete specific tasks, but because other applications can perform other background operations, therefore, the actual time for interrupting other processing is greatly reduced.
7. Parallel COLLECTOR: the parallel collector uses a traditional algorithm and uses multiple threads to execute their work in parallel. Using multithreading on multiple CPU machines can significantly improve the scalability of Java applications.

By default, hotspot uses the replication collector in the new domain. This domain is generally divided into three parts. The first part is Eden, which is used to generate new objects. The other two parts are called Rescue space. When Eden is full, the collector stops the application and copies all reachable objects to the current from rescue space. Once the current from rescue space is full, the collector copies the reachable objects to the current to rescue space. From and to rescue space swap roles. Objects that maintain the activity will be continuously replicated in the rescue space until they are used and transferred to the old domain.

Hotspot uses the replication collector for the new domain and the tag-clear-compression collector for the old domain. Using the replication collector in the new domain makes a lot of sense, because most of the objects generated by the application are short-lived. Ideally, all transition objects will be collected when they are removed from the Eden space. If this is the case, and objects removed from the Eden space are long-lived, then theoretically they can be immediately moved to the old domain to avoid repeated replication in the rescue space. However, applications cannot fit this ideal state because they have a small portion of long-lived objects. It is best to keep these long-lived objects in the new domain, because copying a small part of the object is always cheaper than compressing the old domain.

-The verbose. GC switch displays the GC operation content. Open it to display the time when the busiest and idle collection behaviors occur, the memory size before and after collection, and the time required for collection. Turn on the-XX: + printgcdetails switch to learn more about GC changes. Turn on the-XX: + printgctimestamps switch to learn about the time of the garbage collection, measured in seconds after the JVM starts. Finally, you can use the-XX: + printheapatgc switch to learn more about the heap. To learn about the new domain, you can use the-XX: = printtenuringdistribution switch to get the object permission during use.


Source: http://www.iteye.com/topic/244277

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.