Java garbage collector and memory allocation policy detailed _java

Source: Internet
Author: User
Tags constant garbage collection

Introduction

Garbage collection technology is not the Java language Initiative, the 1960 was born at MIT Lisp is the first real use of memory dynamic allocation and garbage collection technology language. Three issues to consider for garbage collection technology are:

1. Which memory needs to be recycled

2. When to recycle

3. How to Recycle

The distribution of Java Memory Runtime regions, where program counters, virtual machine stacks, and local method areas are all born with threads, and are extinguished by threads, so these areas do not need to be overly concerned with recycling issues. But the heap and method areas are different, and only when the program is running do we know what objects are created, and the allocation and recycling of this part of memory is dynamic. The garbage collector is concerned with this part of memory.

An object death criterion

Before the garbage collector reclaims an object, it first determines whether the object is still in use in the program, and if it is not referred to by the program and then points to the object instance. The easiest way is to add a reference counter to the object instance, and whenever a reference is pointed to it, the counter adds one, and when the reference fails, the counter is reduced by one, and if the counter value is 0, the reference is not pointed to it and can be recycled. But the counter in this method is 0 is not a necessary condition, for example, to generate two object instances, each object instance's properties point to each other, then the two object instances have at least one reference.

Java is using the Accessibility analysis algorithm, that is, to find part of the object as a "GC Roots" node, starting from these nodes downward search, when an object to the "GC Roots" node does not have a path, this object is not available. Nodes in Java that are "GC Roots" include the objects referenced in the virtual machine stack, the objects referenced by the method area static properties, the objects referenced by the method area constants, and the locally invoked object in the local method area.

Reference extensions

If the value stored in the reference type of data is the starting address of another chunk of memory, the memory represents a reference. In this state, an object can only be referenced and not referenced in two states. Java extends the concept of references into strong references (new), soft references (softreference), weak references (WeakReference), and virtual references (phantomreference). If a strong reference exists, the garbage collector does not reclaim the object. If a memory overflow exception is imminent on the system, the garbage collector reclaims the soft reference object. Weak reference objects can only survive until the next garbage collection. A virtual Reference object does not have any effect on its lifetime.

The self redemption of the object

When the garbage collector discovers that an object is unreachable to the GC Roots path, it first determines whether the object overwrites the Finalize () method or whether the Finalize () method has been executed. If overridden and does not execute the method, the object is placed in a low-priority finalizer thread to execute the Finalize () method, and if the object is referenced in the Finalize () method, there is a chance of escaping the fate of being reclaimed.

Collection of method areas

Discard constants and unwanted classes are mainly recycled in the method area. For constants, if there is no reference to a constant, the constant is reclaimed. For class recycling is a lot of trouble, first of all to determine that the class is useless class, the useless class to meet three conditions: 1 All instances of the class are reclaimed 2 loading the classloader of the class has been recycled 3Class has not been referenced and will not be able to access the class by reflection.

Two garbage collection algorithm

Mark-Purge Algorithm (mark-sweep)

The algorithm is divided into two stages: first the object to be reclaimed at the mark, and the marked object is collected uniformly after the mark completes.

Problem: 1 mark and clean efficiency is not high 2 marks can result in a large amount of memory fragmentation when they are cleared, and another garbage collection may be triggered when allocating large objects.

Replication algorithm (copying)

The algorithm divides memory into two regions of equal size, using only one region at a time. When one area is running out, copy the surviving objects from this area to another area.

The advantage is that memory fragmentation is avoided, and the disadvantage is wasting memory space.

A company study shows that the new generation of object 98% is ephemeral, so the virtual machine divides the Cenozoic memory into a larger Eden space and two smaller survivor space. Each time just use the Eden Space and a survior space, when the replication cleanup, the Survivor Space and Eden space in the surviving objects to another survivor space. When the survivor space is not enough, it will rely on the old age for distribution guarantee.

Mark-Finishing Algorithm (mark-compact)

In view of the high survival rate of the old age object, the replication algorithm is obviously unsuitable, so the tag collation algorithm, marking and tagging algorithm is the same, the second finishing is to let all the surviving objects move to one end, and then clean out the memory outside the boundary.

Generational collection

The current virtual machines are collected by generational, and the generations are based on the survival cycle of the objects. The general Cenozoic survival rate is low, using the replication algorithm. The old age survival rate is high by labeling or labeling.

Three garbage collector

Because virtual machines use generational collection, they are also different for different generations of collectors. The image above is the garbage collector of the hotspot virtual machine, and the wiring representation can work together.

Serial collector, the replication algorithm, which is a single-threaded collector, and pauses other threads when collecting, which defaults to the Cenozoic collector in client mode.

The Parnew collector is a multi-threaded version of the serial collector, which is the first concurrent collector.

Parallel scavenge collectors can accurately control throughput (user code run time/user code time + garbage Collection Time)

The Serialold collector is the old version of the serial collector, using the labeling algorithm, which is also a single thread collector.

Parallelold is the old edition of the Parallelscavenge collector, using multithreading and tagging algorithms.

The CMS collector is a collector with the shortest recovery time as the target, and uses the Mark elimination algorithm in the system which attaches importance to response speed. But the disadvantage is that it is sensitive to CPU resources, unable to handle floating garbage, easy to produce memory fragments.

The G1 Collector is the newest collector and can be used in Jdk1.7u4 and above versions. It divides the memory into multiple region, and the Cenozoic and old years contain multiple region respectively. G1 tracking Each region, judging the size of the garbage value, priority recovery of the most valuable region.

Four memory allocation and recycling policies

The allocation of objects is allocated on the heap, the objects are mainly allocated in the new Eden region, if the local thread allocation buffer is started, the thread priority is allocated in the Tlab. A few situations can also be distributed directly to the old age.

When the object is allocated in the Eden area, when there is not enough space in the Eden area, the virtual opportunity initiates a new generation of garbage collection.

If the object requires a large amount of contiguous memory space, such as String and array. Large objects are bad news for virtual machine memory allocation, Chosheng the big object of the Twilight death is bad news. Frequent occurrence of large objects can lead to multiple sets of garbage collection. For such objects, you can set the parameters to deposit large objects directly into the old age.

Each object has an age counter, and when the object is born in the Eden area, every time the GC is passed, and the survivor is deposited, the counter adds one. When the age increases to a certain extent (default 15), it will be deposited in the old age. At the same time, if the same age object occupies more than 50% space in the survivor space, it will also enter the old age directly.

Summarize

Garbage collection algorithm: Replication algorithm, tag-purge algorithm, tag-cleanup algorithm.

Garbage collector features: New generation with replication, old age with Mark Cleanup, CMS with Mark Clear.

Eden Space size and survivor space size default ratio of 8:1, that is, the Cenozoic 10% of the space used to store the replicated objects.

The above is the entire content of this article, I hope you can enjoy.

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.