Java garbage collection and Java garbage collection

Source: Internet
Author: User

Java garbage collection and Java garbage collection

There are two steps for garbage collection:

Method for Determining spam objects

Which objects can be considered as "GC Roots" objects?

  • Class-Objects loaded by the system class loader. These classes cannot be recycled. They can be saved as static fields and hold other objects.
  • Thread-Live threads
  • Stack Local-Local variables or parameters of the Java method
  • JNI Local-Local variables or parameters of the JNI Method
  • JNI Global-Global JNI reference
  • Monitor Used-Monitoring objects used for synchronization
  • Held by JVM-It is used for GC-retained objects for special JVM purposes, but in fact this is related to JVM implementation. Some known types are: system class loaders, some important exception classes that the JVM knows, some pre-allocated objects used to handle exceptions, and some custom class loaders. However, the JVM does not provide other information for these objects, so it is only for the analysts to determine which are "JVM-held.
Garbage Collection Method

Let's take a look at three garbage collection algorithms.

Copy Algorithm

Divide the available memory into two equal parts by capacity, and use only one of them at a time. When the memory of this block is used up, copy the still living objects to the other block, and then clear the used memory space.

Mark-Sweep Algorithm

First, all objects to be recycled are marked, and all marked objects are recycled after marking;

There are two major disadvantages: efficiency issues, low efficiency in both marking and clearing processes, and space problems. After marking is cleared, a large number of discontinuous memory fragments are generated;

Tag-Sorting Algorithm

The 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.

So how are junk objects recycled? The answer is generational recovery.

Generation-based recovery policy

Different objects have different lifecycles. Therefore, objects in different lifecycles can be collected in different ways to improve recycling efficiency. The division of generation garbage collection adopts the idea of division of governance to divide generation, and put objects of different life cycles on different generations. Different generations adopt the most suitable garbage collection method for recycling.

Generally, Java heap is divided into the new generation and the old generation, so that the most appropriate collection algorithm can be used according to the characteristics of each age.

In the new generation, a large number of objects die, but only a small number of objects survive. Generally, the replication algorithm is used, and the collection can be completed only at the replication cost of a small number of surviving objects. The new generation is divided into three zones. One Eden zone and two vor zones (rescue space, generally ). Most objects are generated in the Eden area. When the Eden area is full, the surviving objects will be copied to the same vor area (one of the two). The memory of the newly created objects will be allocated from eden, the Minor collection process is to copy the eden and the live objects in the same vor space to the idle consumer vor space. After the objects have gone through the minor collection for a certain number of times in the new generation (through parameter configuration, it will be moved to the old age, called tenuring.

In the old age, because objects have a high survival rate, they do not need to be cleaned up or sorted out in large quantities. Therefore, they are recycled using the "mark-clean" or "mark-clean" algorithms.

There are two types of GC: Scavenge GC and Full GC;

  • Scavenge GC: Perform GC on the Eden region, clear non-surviving objects, and move surviving objects to the same vor region.
  • Full GC: GC of the entire heap, including Young, Tenured, and Perm. In the process of JVM optimization, a major part of the work is to adjust FullGC. Minimize the number of Full GC requests

Full GC may occur due to the following reasons:

  • Tenured is full
  • Perm is full
  • System. gc () is displayed and called
  • Dynamic change of Heap domain allocation policies after the last GC

 

Refer:

Deep understanding of Java Virtual Machine

Http://blog.csdn.net/fenglibing/article/details/8928927

 

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.