Discussion on Java memory allocation and garbage collection mechanism

Source: Internet
Author: User
Tags compact

One, run-time memory allocation

The Java Virtual machine divides the memory it manages into several different data regions during the execution of a Java program. These zones have their own purpose, as well as the creation and destruction of time, and some regions exist as the virtual machine process starts, and some regions depend on the user thread's start and end to build and destroy.

Thread Private Zone (life cycle is the same as thread)

A) virtual machine stack

The virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame (stack frame[1]) to store 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.

There is a local variable table in the virtual machine stack that holds the various basic data types (Boolean, Byte, char, short, int, float, long, double), object reference (reference type) that are known during compile time, which is not equivalent to the object itself, it may be a reference pointer to the start address of an object, or it may point to a handle that represents an object or other location associated with the object, and the ReturnAddress type (the address of a bytecode directive).

b) Local method stack

The local method stack is very similar to the virtual machine stack, but the difference between them is that the virtual machine stack executes Java methods (that is, bytecode) for the virtual machine (Native) service. The local method stack is the native method service used by the virtual machine.

C) Program counter

because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given moment, a processor (a kernel for a multicore processor) executes only the instructions in one thread. Therefore, in order for the thread to switch back to the correct execution location, each thread needs to have a separate program counter, the counters between the threads do not affect each other, isolated storage, we call this type of memory area is "thread-private" memory.

Shared data area

A) heap

for most applications, the Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area of memory is to hold object instances where almost all of the object instances are allocated memory.

According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous, just like our disk space. When implemented, it can be either fixed or extensible, although the current mainstream virtual machines are implemented in a scalable way (via-XMX and-xms control).

b) Method Area

The method area, like the Java heap, is an area of memory shared by each thread, which is used to store a virtual
Computer-loaded class information, constants, static variables, instant compiler compiled code and other data.

The garbage collection behavior is relatively rare in this area, but it is not the data that enters the method area as "permanent" as the name of the permanent generation. The memory recovery target of this area is mainly for the recovery of constant pool and unloading of type, in general, the recovery of this area "performance" is more difficult to be satisfactory, especially the type of unloading, the conditions are very harsh, but this part of the area is really necessary to recover.

Java Object creation Process

A) The virtual opportunity to a new directive, first checks whether the parameter of this directive can be positioned in a constant pool to the symbolic reference of a class, and checks whether the class that the symbol reference represents has been loaded, parsed, and initialized. If not, you must first perform the appropriate class loading process.

b) Allocate memory for the object (the amount of memory required by the object is fully determined after the class is loaded), and the size of the memory required by the object is fully determined after the class is loaded (How to determine what will be described in section 2.3.2), the task of allocating space for an object is equivalent to dividing a certain size of memory from the Java heap.

Second, the garbage object judgment 1. Reference counting method

Each object has a reference count property that is used to hold the number of times the object is referenced. When the number of references is 0 o'clock, it means that the object is not referenced, and will not be used in this object, it can be judged as a garbage object. However, there is a big bug in this approach, which is that there is no way to solve the problem of cross-referencing or circular referencing between objects: When two objects are referenced to each other, neither of them has a reference relationship with any other object, and neither of them has a reference count of 0, so they are not recycled, but actually they are no longer useful.

2. Accessibility analysis (Root search method)

The mainstream implementation of the mainstream Business programming language (Java, C #, and even the old Lisp mentioned earlier) is called the reachability analysis to determine whether an object survives. The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain (Reference Chain), when an object to the GC Roots no reference chain connected (in the case of graph theory, Is that this object is not available when the GC roots to this object is unreachable). As shown in 3-1, objects 5, 6, and object 7 are associated with each other, but they are not accessible to GC roots, so they will be judged as recyclable objects.

The GC roots objects here include the following:

The object referenced in the virtual machine stack (the local variable table in the stack frame).
The object referenced by the class static property in the method area.
The object referenced by the constant in the method area.
The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.

Note: There are four types of references in Java, no longer elaborate.

Three, typical garbage collection algorithm

After determining which garbage can be recycled, what the garbage collector has to do is start a garbage collection, but there is one problem: how to effectively recycle. Because the Java Virtual Machine specification does not explicitly stipulate how to implement a garbage collector, each vendor's virtual machine can implement a garbage collector in different ways, so it is only a discussion of the core ideas of several common garbage collection algorithms.

1.mark-sweep (Mark-Clear) algorithm

This is the most basic garbage collection algorithm, the reason is that it is the most basic because it is the easiest to achieve, the idea is the simplest. The tag-purge algorithm is divided into two stages: the tagging phase and the purge phase. The task of the tagging phase is to mark out all objects that need to be recycled, and the purge phase is to reclaim the space occupied by the tagged objects. The exact process is as follows:

It is easy to see that the tag-purge algorithm is easier to implement, but one of the more serious problems is that it is prone to memory fragmentation, and too many fragments can cause the subsequent process to allocate space for large objects without finding enough space to trigger a new garbage collection action ahead of time.

2.Copying (copy) algorithm

In order to solve the defect of mark-sweep algorithm, the copying algorithm is proposed. It divides the available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, copy the surviving object to another piece, and then clean up the used memory space once, so the memory fragmentation problem is not easy. The exact process is as follows:

This algorithm is simple, efficient, and not prone to memory fragmentation, but it has a high cost of using memory space because it can use less memory than half the original.

Obviously, the efficiency of the copying algorithm is very much related to the number of surviving objects, if there are many surviving objects, then the efficiency of the copying algorithm will be greatly reduced.

3.mark-compact (marker-collation) algorithm

In order to solve the defect of copying algorithm and make full use of memory space, the mark-compact algorithm is proposed. The algorithm marks the same stage as Mark-sweep, but after the token is completed, it does not clean the recyclable object directly, but instead moves the surviving object to one end and then cleans up memory outside the end boundary. The exact process is as follows:

4.Generational Collection (generational collection) algorithm

The generational collection algorithm is the algorithm used by most of the JVM's garbage collectors today. Its core idea is to divide the memory into several different regions based on the life cycle of the object's survival. In general, the heap zoning is divided into the old age (tenured Generation) and the New Generation (young Generation), the characteristics of the old age is that each garbage collection only a small number of objects need to be recycled, and the new generation is characterized by a large number of objects to be recycled each time the garbage collected, Then we can take the most suitable collection algorithm according to the characteristics of different generations.

At present, most of the garbage collectors take the copying algorithm for the new generation, because each garbage collection in the Cenozoic has to reclaim most of the objects, that is, the number of operations that need to replicate is less, but the actual is not in accordance with the ratio of 1:1 to divide the new generation of space, In general, the Cenozoic is divided into a larger Eden space and two smaller survivor space, each time using Eden space and one of the survivor space, when recycling, Copy objects that are still alive in Eden and survivor to another survivor space, and then clean up Eden and the survivor space you just used.

Because of the characteristics of the old age is that each recycling only a small number of objects, the general use of the mark-compact algorithm.

Note that there is another generation outside the heap that is the permanent generation (permanet Generation), which is used to store class classes, constants, method descriptions, and so on. The recovery of the permanent generation mainly recycles two parts: obsolete constants and useless classes.

If you are working on Java development, you can buy a "in-depth understanding of Java Virtual Machine" to see

Reference:

Https://www.cnblogs.com/baizhanshi/p/5817845.html

"In-depth understanding of Java virtual machines"

Discussion on Java memory allocation and garbage collection mechanism

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.