Java memory management and garbage collection

Source: Internet
Author: User
Tags compact

Notes, in-depth understanding of Java virtual machines

Java run-time memory area

program counter , thread exclusive, the line number indicator of the byte code that the current thread executes, each thread needs to record where it is executed, and the next time it is scheduled to continue, this area is the only one that will not happen to Oom.

stack , thread exclusive, containing a virtual machine stack or native method stack for storing local variables

heap , thread sharing, which is used to distribute object instances, and the underlying memory management and garbage collection is basically all about the heap.

method zones , thread sharing, for storing classes, constants, and static variables loaded by virtual machines; Java Virtual machine specification, the method area described as the logical part of the heap, so also known as "permanent generation", in a large number of use of reflection, dynamic Proxy, ClassLoader scenario, to consider the permanent generation of recycling

For a simple, object-generated

Object obj = new Object ();

Will involve 3 districts,

The diagram depicts two types of object access, with a handle that can mask changes in the actual address of the object (the object address is often changed in the GC), without a handle more efficient

Garbage collection Refuse determination

For how to judge the object is rubbish, the textbook answer is reference count, and also get good application in Com,python

The problem with reference counting is that it is difficult to solve the problem of circular references,
A.instance = B; B.instance =a
This causes the reference count of a, B to not be 0

So for Java,c#,lisp is the way of using GC Roots tracing,
The principle is also very simple, is to select a series of GC roots objects, only the objects from the root object can be reached, the rest is garbage

The GC roots in Java contains,

    • The referenced object in the Javastack
    • object to which a static reference in the method area points
    • The object to which the constant reference in the method area points
    • The object that is referenced by JNI in the native method

Garbage collection algorithm

Mark-sweep, the most primitive idea, first mark out all the garbage objects, and then recycle; the problem is that it's inefficient and generates a lot of memory fragmentation.

Copying, the simplest Copying, divide the memory in two halves, use half first, and then copy the valid objects to the other half when you recycle them.

This first applies only to younger generations, i.e., the case of a higher proportion of garbage objects
Furthermore, space is so wasted that it can only be used in half

So, now actually using the version for, divided into a larger Eden area, two smaller survivor area
The proportions are generally 8:1:1, and one of the survivor areas is empty, so that only 10% of the space is wasted.
The premise to do this is that only a maximum of 10% of the objects will survive each time
The object is first placed in the Eden area, the Eden area is full, the minor GC is placed, and the surviving objects of the Eden area and the survivor area with the data are put into another empty survivor area.
Two survivor districts, although named from and to, but in fact there is no difference, completely equal

When there is not enough space in the survivor area, you need to put it into the old generation (called handle promotion, that is, the old generation to provide security for the survivor, you have not enough space, you can put me this, similar to the loan guarantee), if the old generation of space is not enough or unacceptable handle Promotion failure, we need full GC to reclaim the old generation

Mark-compact, said before the copying only applicable to the survival of the lower proportion of the case, so suitable for the younger generation, but for the elderly generation such, with the copying is certainly not possible;
Mark-compact,mark is still the same, but when recycled, the object will be translated to eliminate debris

Garbage collector

Serial collector

The simplest, single-threaded, collection will stop the world when all user threads are paused
Can be used to collect new generation or old age
Collect new generation, use copying algorithm
Collect old age, with Mark-collation, called serial

Simple and efficient for single CPU environments; is the default collector for virtual machine client mode
Disadvantage, will stop the world

Parnew Collector

Just serial multi-threaded version, suitable for multi-core environment, if on a single core machine, efficiency is not as good as serial
The advantage is that it can be used in conjunction with the CMS collector, since the CMS as a collector of the old age can only match serial or parnew as a new generation of collectors

CMS (Concurrent Mark Sweep) collector

With the shortest pause time as the target collector, suitable for the Web site or b/s system service side, attention to service response speed scene.
The collector is relatively complex and the whole process is divided into
1. Initial tag (CMS initial mark), Stop the world, flag GC roots directly associated to the object, fast
2. Concurrent tagging (CMS concurrenr mark), the process of parallel GC Roots tracing
3. Re-tagging (CMS remark), stop the world, because concurrent tagging and user threads are executed concurrently, the markup needs to be last modified, consuming much less time than the concurrency tag time
4. Concurrent purge (CMS concurrent sweep), last sweep

Disadvantages
A. CPU-sensitive, frequent GC can cause the CPU to die
B. Unable to process floating garbage (floating garbage), new garbage generated during the concurrent cleanup phase cannot be reclaimed at this time
C. Need to reserve a large amount of memory, because the CMS collection process and user application concurrency, so can not wait for the old age to be full again to do, need to be collected in advance, the default is set to 68%, which is more conservative settings can be reduced to reduce the number of GC, but if in the CMS collection process, If the user application memory is not enough, the "Concurrent Mode Failure" will occur, so that the virtual machine can only use fallback scheme, the serial old collector for the collection of older generation (because the application can not be executed concurrently), so that the application will be a long pause time, So you need to set a reasonable proportion
D. Because of the use of sweep, there will be a lot of debris

Parallel Scavenge Collector

The new generation collector, designed to achieve a controllable THROUGHPUT,CPU runtime user code time/CPU total elapsed time, in plain view, is to ensure that the CPU executes more user code rather than GC
Suitable for background operations, less need for interactive scenes

But the fish and bear can not have both, throughput and GC pause time is needed tradeoff
Reducing the space size of the Cenozoic and reducing GC intervals can reduce GC downtime, but also reduce throughput

And the collector, which supports the Useadaptivesizepolicy parameter, does not require the user to specify the Cenozoic size, the Eden and survivor proportions, the age of promotion, and so on, the virtual opportunity to optimize the adjustment according to the performance monitoring

Parallel Old Collector

The previously mentioned parallel Sacvenge collector is not available with the CMS collector, so it can only be used with serial old to collect older generations, resulting in low efficiency
Parallel old as serial old multi-threaded version, can better and Parallel sacvenge collector Mate, really achieve throughput first collection

G1 (garbage first) collector

A new generation of collectors, unlike the previous collectors, collects the entire Cenozoic or old age
G1 can significantly improve collection efficiency by dividing the entire Java heap into multiple fixed-size areas and tracking the level of garbage accumulation, and each time the most garbage collection is collected.

JVM Collector corresponding Parameters

Memory allocation and recycling policies

Objects are often allocated in the new generation of Eden, Eden Space is not enough, launch minor GC, will be Eden and a survivor area of the surviving object copy to another survivor area, if another survivor area is not enough space, into the old age

Large objects go straight into the old age, such as long strings or large arrays, and large objects are bad news for JVM memory allocations because large objects need to find contiguous memory, or they trigger a GC, so a short-lived large object needs to be avoided as much as possible.

Long-term survival of the object into the old age, the object in the Cenozoic each experience a minor GC, age plus 1, by default reached 15 years old will enter the old age

Each time the GC is minor, the virtual opportunity detects whether the average size of each promotion to the old age is greater than the current remaining size of the old age and, if less, the full GC

Java memory management and garbage collection

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.