Java thousand Ask _07JVM architecture (008) _java garbage collection mechanism is what __java

Source: Internet
Author: User
Tags compact garbage collection xms

Click to enter _ more _java thousand ask 1, how to judge the garbage object

The first step in garbage collection is to need algorithms to mark which ones are garbage, and then to dispose of the garbage. The usual programming language is judged by one of the following algorithms: the reference count (referencecounting) algorithm
This method is relatively simple and intuitive, the core idea is to add a referenced counter to each object, referred to +1, reference failure-1, equal to 0 o'clock means that the object is not referenced, can be recycled.

Flashplayer/python using this algorithm, simple and efficient. However, java/c# does not use this algorithm because the algorithm does not solve the problem that objects are referenced to each other, that is, when two objects are referenced by each other and are not referenced by other objects, their reference count is 1, although not 0, but it is still a garbage object that can be recycled. Root search (GC Roots tracing) algorithm

The basic principle is: Gcroot object as the starting point (root). If it is accessible from the root to an object, the object is called the Unreachable object (the surviving object, the object cannot be reclaimed). Otherwise, the unreachable object can be recycled.

2, how to clean up the new generation of garbage

Garbage Cleanup Algorithm

The new generation of garbage collectors often assume that most objects have a very short survival time, with only a few objects surviving longer. According to this assumption, the new generation of garbage removal algorithm is mainly the replication algorithm (copying). By copying an algorithm, you can clean out objects that are not referenced, and you can put objects that are still alive after several (configurable) cleanup into the new generation.

Understanding heap Memory Look here: What is the Java heap memory
Understanding the copying algorithm of the heap look here: What is the new generation of copying algorithm

garbage cleanup Trigger mode

The new generation uses "idle pointers" to control GC triggers, and the pointer holds the last object in the Cenozoic allocation, and when there is a newer object to allocate memory, the GC is triggered to check for space adequacy. The new generation of GC is often called the young GC, sometimes called the minor GC.

In the process of continuously allocating objects, the objects will gradually go from the Eden area to the Survivor area, and finally the new generation.

Common Configurations

The size of the Cenozoic:-xmn

The ratio of Eden area and survivor area:-xx:survivorratio 3, how to clean up the rubbish in the generation

Garbage Cleanup Algorithm

Compared with the Cenozoic generations, the object survives longer and more stably, so it is marked/collated (also called tag-Compact, mark-compact) algorithm.

Know the mark algorithm for the heap look here: What is the mark algorithm for the generation
garbage cleanup Trigger mode

The GC, usually called the full GC, is also called the major GC. There are many cases in the senior generation that will trigger the GC, but generally the frequency is not high:

Lack of space in the old birth generation
When tuning, try to get objects to be recycled in the Cenozoic GC, allow objects to survive in the Cenozoic for some time, and do not create objects and arrays that are too large to avoid creating them directly in the old generation.

Pemanet Generation Space Shortage
Increase the perm gen space to avoid too many static objects.

The average size of the GC promoted to the generation is greater than that of the remaining space.
Control the proportion of Cenozoic and old generation.

Manually Invoke System.GC ()
Garbage collection should not be triggered manually, relying on the JVM's own mechanisms as much as possible.

Common Configurations

The initial space of the heap:-XMS, you can calculate the size of the generation of-xms minus-XMN

Maximum space for a heap:-xmx

Maximum age Threshold:-xx:maxtenuringthreshold, the number of surviving generations of the Cenozoic

The ratio of the new generation to the Cenozoic:-xx:newratio, for example, the value is 3, which indicates that the ratio of the Cenozoic to the aged is 1:3.

4. What are the methods of garbage collection?

The above is the basis of the Java garbage collection mechanism, the JVM provides us with a number of alternative ways to recycle, that is, we commonly known as the garbage collector. There are 4 main types: Serial garbage collector (serial garbage Collector) parallel garbage collector (Parallel garbage Collector) concurrent tag Scan garbage collector (CMS garbage Collector) G1 garbage collector (G1 garbage Collector)

Here we will describe in detail the various types of garbage collector: How Java garbage collection works

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.