Java garbage collection mechanism and memory allocation

Source: Internet
Author: User

Introduction

The garbage collection technology was not the first in the Java language, and the 1960 birth of the Lisp at MIT was the only language that really used memory dynamic allocation and garbage collection techniques. The three issues to consider in the garbage collection technology are:

What memory needs to be recycled?
When do I recycle?
How to recycle?

http://segmentfault.com/a/1190000002931555 the distribution of the Java Memory Runtime area, where the program counter, the virtual machine stack, and the local method area are all born with threads and are extinguished with threads. So there is no need to think too much about recycling in these areas. But the heap and the method area are different, and only when the program is running do we know what objects will be created, and this part of the memory allocation and recycling is dynamic. The garbage collector is concerned with this part of the memory.

An object of death criterion

Garbage collector before the collection of an object, the first thing to determine whether the object in the program is still possible to use, the necessary and sufficient condition is not accessible by the program to point to the object instance . The simplest way is to add a reference counter to the object instance, whenever there is a reference to it, the counter adds one, and when the reference fails, the counter is reduced by one, and if the counter value is 0 it means that no reference points to it and can be recycled. However, the counter in this method is 0 is not a necessary condition, for example, to generate two object instances, each object instance properties are pointing to each other, then the two object instances have at least one reference.

java uses Accessibility analysis algorithm , which is to find part of the object as " GC Roots " node, starting from these nodes to search down, when an object to the GC Roots node when no path is reached, this object is not available. The nodes in Java that are "GC Roots" include:

  1. The objects referenced in the virtual machine stack,
  2. The object referenced by the method area static property,
  3. The object that the method area constant refers to,
  4. The referenced object is called locally in the local method area.

Reference extensions

If the value stored in data of type reference is the starting address of another piece of memory, then this memory represents a reference. An object in this state can only be referenced and not referenced in two states. Java extends the reference concept by dividing the reference into强引用(new),软引用(SoftReference),弱引用(WeakReference),虚引用(phantomreference).If a strong reference exists, the garbage collector does not reclaim the object. If the system is about to have a memory overflow exception, 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 life time.

The object of self redemption

When the garbage collector discovers that an object is not reachable by the "GC Roots" path, it first determines whether the object overrides the Finalize () method, or whether the Finalize () method is executed. If the method is overwritten and has not been executed, the object is placed in the lower priority finalizer thread queue to execute the Finalize () method, and if the object is referenced in the Finalize () method, there is a chance to escape being recycled.

Recovery of method areas

Obsolete constants and useless classes are primarily recycled in the method area. For constants, if there is no reference to the constant, the constant is recycled. For a class to be recycled is a lot of trouble, first of all to determine that the class is 无用的类 , 无用的类 to meet three conditions:
1. Instances of all classes are recycled.
2. The classloader that loaded the class have been recycled.
3. Class is not referenced, and the method of the class is not accessed through reflection.

Two garbage collection algorithm marker-purge algorithm (mark-sweep)

The algorithm is divided into two stages: first mark the object to be recycled, and after the mark is complete, all the tagged objects are collected uniformly.
Problems that exist:
1. Mark and clear efficiency are not high
2. When a tag is cleared, it creates a large amount of memory fragmentation that may trigger another garbage collection when a large object is allocated.

Replication Algorithm (Copying)

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

The advantage is to avoid the generation of memory fragments, the disadvantage is to waste 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 using Eden space and a survior space, when copying cleanup, copy the surviving objects in the Survivor Space and Eden space to another survivor space. When the survivor space is not enough, it will rely on the old age for distribution guarantees.

Mark-and-Organize algorithm (mark-compact)

In view of the high survival rate of the old age, the replication algorithm is obviously inappropriate, so the labeling and labeling algorithm is the same, and the second is to let all the surviving objects move to one end and then clear out the memory outside the boundary.

Collection of generations

The current virtual machines are collected on a generational basis and are based on the lifetime of the objects. The general Cenozoic survival rate is low and the replication algorithm is adopted. The survival rate of the old age is high. Use marker finishing or marker removal.

Three garbage collectors


Because virtual machines are collected in generational collections, they are also different for different generations of collectors. Is the hotspot virtual machine garbage collector, the line indicates that can work together.

Serial收集器, a copy algorithm, which is a single-threaded collector, and pauses other threads while it is being collected, which by default is the new generation collector in client mode.

ParNew收集器is the multi-threaded version of the serial collector, which is the first concurrent collector.

Parallel Scavenge收集器Throughput can be precisely controlled (user code run time/(user code time + garbage Collection Time))

SerialOld收集器is the old version of the serial collector, using the tag finishing algorithm, the same is a single-thread collector.

ParallelOldis the older version of the Parallelscavenge collector, using multithreading and tagging algorithms.

CMS收集器It is a collector with the shortest recovery pause time, which is applied in the system of attention response speed by using the Mark clearing algorithm. However, the disadvantage is that it is sensitive to CPU resources, cannot handle floating garbage, and is prone to memory fragmentation.

G1收集器Is the newest collector, can be applied in jdk1.7u4 and above. It divides the memory into several region, and the new generation and the old age contain multiple region respectively. G1 tracking each region to determine the value of garbage, priority to reclaim the region of the largest value.

Four memory allocation and recovery policy


The allocation of objects, which is allocated on the heap, is primarily allocated in the new generation of Eden regions, and if the local thread allocation buffer is started, it is assigned by thread first in Tlab. A few cases may also be directly assigned to the old age.

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

If an object requires a large amount of contiguous memory space, such as string types and arrays. Large objects are bad news for virtual machine memory allocations, and the big object of Asao twilight is a deadly bad news. Frequent occurrence of large objects can result in multiple departure garbage collection. For such objects, you can set 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 region, each time the GC is passed, and the survivor is deposited, the counter adds one. When the age is increased to a certain level (default 15), it will be credited to 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: copy algorithm , mark-Clear algorithm , tag-cleanup algorithm .

Garbage collector features: New generation with replication, the old age with the Mark Clean, the CMS with the mark clear.

The default ratio of Eden space size and survivor space size is 8:1, that is, the Cenozoic 10% space is used to hold the copied object.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java garbage collection mechanism and memory allocation

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.