Java memory and garbage collection knowledge summary

Source: Internet
Author: User

Summing up the knowledge of Java memory, today I do not produce knowledge, I am just a porter of knowledge.

1. Run-time data region

The Java Virtual machine divides the memory it manages into several different data regions during the execution of a Java program.

Data area shared by all threads
    • Heap [Heap]:

      The Java heap is the largest piece of memory managed by a Java virtual machine, and the only purpose of this memory area is to hold object instances. All object instances and arrays are allocated on the heap, but with the development of virtual machine technology, this becomes less absolute. The Java heap is the main area of garbage collection management, so it is often referred to as a GC heap. Depending on the specification of the virtual machine, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

    • Methods area [Method region]:

      The method area is used to store data such as class information, constants, static variables, instant compiler compiled code, and so on, that have been loaded by the virtual machine.

Thread-Isolated data area:
  • Virtual machine stack [VM]:

    The virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.

  • Local methods Stack [Native method stack]:

    The local method stack works very much like the virtual machine stack, except that it serves the native method.

  • Procedure counter [program Counter Register]:

    The program counter is a small memory space, which can be regarded as the line number indicator of the byte code executed by the current thread, and the bytecode interpreter works by changing the value of this counter to select the next byte code instruction to execute.

The above is the area of the runtime data mismatch, and we understand that the following plays, the JVM is very important garbage collection mechanism.

2. The life journey of the object

To avoid the cumbersome memory management work of programmers, the JVM provides a garbage collection mechanism.

2.1 Whether the object is up to

We use accessibility analysis to determine whether an object survives.
The basic idea of this algorithm is to use a series of objects that become "GC Roots" as the starting point, starting from these points to write down the search, searching through the path becomes the reference chain, when an object to the GC Roots no reference chain connected (in the words of graph theory is from the GC Roots to this object unreachable ), it proves that these objects are not available.
In Java, the objects that can be used as GC roots include the following:

    • Objects referenced in the virtual machine stack
    • Objects referenced by static properties in the method area
    • Objects referenced by constants in the method area
    • Objects in the local method stack that are referenced by JNI

Even those unreachable in the Accessibility analysis algorithm are not "immortal", when they are temporarily suspended from probation, if they want to declare an object to die, at least two times the marking process.
The first token is to see if the object is necessary to execute the Finalize () method, which is placed in the F-queue queue if necessary.
Before the second token, an object can be associated with any object on the reference chain, or it can escape being recycled, or it will be recycled.

2.2 Method Zone Recycling

Most of the time we think of the method area as a permanent generation, because the recovery cost of the permanent generation is higher, so we can choose not to recycle the permanent generation, but we can also recycle it. The last two parts of a permanent garbage collection are the following: Obsolete constants and useless classes.
A useless class needs to meet the following three conditions:

    • All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap.
    • The ClassLoader that loaded the class have been recycled.
    • The corresponding Java.lang.class object of this class is not referenced anywhere and cannot be used to access the class's methods at any place.

Virtual machines can reclaim useless classes that meet the above 3 criteria.

2.3 of four references

As seen above, whether an object is recycled, is very much related to references, and the Java language provides four levels of reference

  • Strong references

    Strong references are our most common Object o = new Object() type of reference, as long as a strong reference exists, the garbage collector never reclaims the referenced object.

  • Soft references

    Common methods have softrefrence tsoftref = new Softrefrence (t) and tsoftref.get () method
    Each soft reference can be accompanied by a reference queue, and when the accessibility state of an object changes (from unreachable to unreachable), the soft Reference object enters the reference queue, which can be used to track the recycling of that object. Add a parameter to the reference queue at initialization time.

  • Weak references

    A weak reference is also used to describe a non-required object, but its strength is weaker than a soft reference, and the object associated with the weak reference only survives until the next garbage collection occurs. When the garbage collector studio, regardless of the current memory is sufficient, will be reclaimed only by the weak reference associated objects. Using Weakrefrence to implement weak references, as with soft references, there are two methods of initialization.

  • Virtual reference

    The weakest reference relationship, where an object has no virtual reference, does not affect its lifetime at all, and can be reclaimed by the garbage collector at any time. A virtual reference must be used in conjunction with a reference queue to track the garbage collection process. It is also not possible to get an instance of an object through the Get method. When the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue after the object is reclaimed, notifying the application that the object is recycled. You can use Phantomrefrence to implement virtual references.

3. Garbage collection Algorithm 3.1 single-fight algorithm
  • Reference counting method

    For an object A, it is equipped with an integer counter, as long as any one object refers to a, the reference counter of a is added 1, when the reference expires, the reference counter is reduced by 1, as long as the object reference counter is 0, then object A can not be referenced, will be recycled.
    However, there are two problems with this algorithm: (1) failure to handle circular references, which can cause memory leaks (2) each time a reference is generated and eliminated, it needs to be accompanied by a counter-change operation that affects performance.

  • Mark Clear Method

    As its name, is divided into two stages. The tagging phase, implemented through the root node, marks all objects that are accessible from the root node, so that objects that are not tagged are garbage objects that are not referenced. Then, in the purge phase, clear all objects that are not marked. The biggest problem that can be created by marking a clear algorithm is space fragmentation.

  • Replication Algorithms

    It divides the available memory into two blocks of equal size, one at a time, when this piece of memory is used up, copies the surviving objects to another piece, and then cleans up the used memory space once. This allows the entire half of the memory to be reclaimed each time, regardless of memory fragmentation and other complex situations, only need to move the heap top pointer, in order to allocate memory. The flaw in this approach is that it costs too much to shrink the available memory to half its original size. So in the old age, this method is not suitable for a scenario where only a small fraction of each collection is not recycled.

  • Tagging-sorting algorithms

    An algorithm based on the characteristics of the old age. After the first stage tag, all surviving objects are moved to one end and then the memory of the end boundary is cleaned up directly.

3.2 Collection of people, generational collection algorithm

Based on the survival characteristics of Java objects, this paper proposes a generational collection algorithm combined with the advantages of the above algorithms.
The generational algorithm divides the memory interval into several pieces according to the characteristics of the object

  • Cenozoic

    space to store young objects. A young object is an object that has just been created, or is experiencing a few garbage collection times. It is divided into Eden Zone, Survivorfrom space and survivorto space. It is usually 8:1:1, because most of the new objects are born and gone, and the object of garbage is far more than the surviving object.
    When garbage collection occurs, the surviving objects in the Eden area are copied into unused survivor space (assuming to), and young objects in the survivor space (assuming from) are also copied into the to space (large objects, or older objects that go directly into the old age, If the to space is full, the object goes straight into the old age). At this point, the remaining objects in the Eden and from spaces are garbage objects that can be emptied directly, and the to space holds the objects that are still in stock after the collection. This improved replication algorithm not only guarantees the continuity of space, but also avoids the waste of large amount of memory space. The
    is set by the parameter Survivorradio , which is Survivor/enden, which defaults to 8.
    The Cenozoic is full or the area is MINORGC.

  • Parameters: Xms Set initial heap size, Xmx set maximum heap size, Newradio Set the ratio of the old to the Cenozoic, The sum of the size of the old and new Cenozoic is the size of the heap. When did the
    enter the old age?
    Large object: pretenuresizethreshold . Units are bytes. By default it is 0, that is, no promotion size is specified, and everything is determined by the running condition.
    Old Age object: maxtenuringthreshold . The default is 15, which means a maximum of 15 GC, the 16th GC will enter the old age. The new generation of the
    has a high recovery frequency and is short time consuming. The old age is less frequent, but consumes more time.
    The old age is full of FULLGC, often accompanied at least once minorgc, 10 times times slower than MINORGC.

  • Permanent generation? No! metaspace!

    In the versions of JDK1.6 and JDK1.7, the method area can be understood as a permanent generation, and it is accurate that the JVM uses the permanent generation to implement the method area. This stores the class information and metadata. Size can be set by parameter PermSize and maxpersize.
    But this will obviously face a problem, that is not flexible enough, it is easy to overflow.
    So in JDK1.8, the permanent generation is completely removed and replaced by Metaspace, which uses local memory to store class information and metadata.
    See the following two links for more information here:
    Where did Java PermGen go?
    Explore the new features of Java 8 (ix) Say goodbye to Oom:permgen.

Today is the day to summarize the knowledge of the garbage collector tomorrow.
Resources:
"In-depth understanding of Java virtual machines"
Java Virtual Machine in action

?

Java memory and garbage collection knowledge summary

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.