JVM run-time memory structure

Source: Internet
Author: User

Connection: http://my.oschina.net/sunchp/blog/369707

1.JVM memory model

JVM Runtime memory = Shared memory area + Thread memory Area

1). Shared Memory Area

Shared memory Area = Persistent Band + heap

Persistence Band = Method Area + Other

Heap =old Space+young Space

Young Space=eden+s0+s1

(1) Durable belt

The JVM implements the method area with the persistence band (Permanent Space), which mainly holds all loaded class information, method information, constant pool, and so on.

The-xx:permsize and-xx:maxpermsize can be used to specify the persistence of the initialization and maximum values.

Permanent space is not the same as the method area, but the hotspot JVM uses Permanent space to implement the method area, and some virtual machines do not Permanent space and use other mechanisms to implement the method area.

(2) heap

Heap, which is used primarily to store object instance information for a class.

The heap is divided into old space (aka, tenured Generation) and young space.

Old space primarily stores live objects with long life cycles in the application;

Eden (Eden) mainly stores the new objects;

S0 and S1 are two memory areas of the same size, primarily storing Eden-surviving objects after each garbage collection, as objects from Eden to old space buffer zone (s refers to the English word Survivor Space).

The heap is partitioned to facilitate object creation and garbage collection, which is explained later in the garbage collection section.

2). Thread Memory Area

Thread memory Area = Single thread memory + single thread memory + ....

Single thread memory =pc REGSTER+JVM Stack + local method stack

JVM stack = stack frame + stack frame + .....

Stack frame = local variable area + operand area + frame data area

In Java, a thread corresponds to a JVM stack (the JVM stack), and the running state of the thread is recorded in the JVM stack.

The JVM stack is made up of stack frames, and a stack frame represents a method call. Stack frame consists of three parts: local variable area, operand stack, frame data area.

(1) Local variable area

The local variable area, which can be understood as an array-managed memory area, counts from 0, and the space for each local variable is 32 bits, or 4 bytes.

The basic type Byte, char, short, boolean, int, float, and object references occupy a local variable space, and the values of type short, Byte, and Char are converted to an int value before being deposited into the array; long, double accounts for two local variable spaces, When accessing local variables of type long and double, you only need to take the index of the first variable space.

For example:

?
1234567 publicstaticintrunClassMethod(inti,longl,floatf,doubled,Object o,byteb) {       return 0;   }              publicintrunInstanceMethod(charc,doubled,shorts,boolean b) {       return0;   }

The corresponding local variable area is:

Runinstancemethod local variable area The first item is a reference (reference), which specifies a reference to the object itself, which is our usual this, but in the Runclassmethod method, there is no such reference, That's because Runclassmethod is a static method.

(2) Stack of operations

The operand stack, like the local variable area, is also organized into an array of word lengths. But unlike the former, it is not accessed by the index, but by the stack and the stack. The operand stack is the storage area for temporary data.

For example:

?
123 inta= 100;int b =5;intc = a+b;

The corresponding operand stacks change to:

It can be concluded that the operand stack is actually a temporary data storage area, which is operated by a stack and a stack.

PS:JVM implementation, there is a stack-based instruction set (Hotspot,oracle JVM), there is a register-based instruction set (DALVIKVM, Android JVM), what is the difference between the two?

The stack-based instruction set has the advantages of simple access, hardware independence, compact code, and the allocation of stacks without regard to physical space allocation, but because the same operation requires more access to the stack operation, so the memory consumption is larger. The most important advantage of register-based instruction set is that the instruction is small and fast, but the operation is relatively cumbersome.

Example:

?
1234567891011121314 packagecom.demo3;publicclassTest {    publicstaticvoidfoo() {        inta = 1;        intb = 2;        intc = (a + b) * 5;    }    publicstaticvoidmain(String[] args) {        foo();    }}

The execution process of the stack-based hotspot is as follows:

The register-based DALVIKVM execution process is as follows:

Both of these methods end up through the JVM execution engine, and the assembly instructions received by the CPU are:

(3) Frame data area

The frame data area holds the pointer address to the constant pool, which accesses the data of the constant pool through the pointer address in the frame data area when certain instructions need to obtain data for the constant pool. In addition, the frame data area holds some data required for normal return and termination of the method.

2. Garbage collection mechanism

1), why garbage collection

The JVM automatically detects and frees memory that is no longer in use, improving memory utilization.

The Java runtime JVM executes the GC so that the programmer no longer needs to explicitly dispose of the object.

2), garbage collection (GC) classification

Minor GC (Secondary recycle)

Full GC (primary recycle)

3), garbage collection (GC) generation process

(1) The newly generated object completes the memory allocation in the Eden area;

(2) When the Eden area is full and the object is created, the MINORGC will be triggered because the application is not space, and the Eden+1survivor area is garbage collected. (Why is Eden+1survivor: Two Survivor always have a Survivor is empty, the empty one is marked to Survivor);

(3) When MINORGC, the object that Eden cannot be reclaimed is placed into an empty Survivor (that is, to Survivor, and Eden is definitely emptied), and the other Survivor (from Survivor) objects that cannot be reclaimed by GC will also be placed in this Survivor (to Survivor), always guaranteeing that a Survivor is empty. (MINORGC to Survivor and from Survivor after completion);

(4) As the 3rd step, if the survivor of the object is found to be full, then the objects are copied to the old area, or the Survivor area is not full, but some objects are old enough (set by the Xx:maxtenuringthreshold parameter) , also be put into Old district;

(5) When the old area is full, complete garbage collection, that is, full GC;

(6) Full GC, when sorting objects in old space, put the surviving objects into old space.

VISUALVM Visual GC plugin diagram in the official JDK monitoring tool:

4), under what circumstances trigger garbage collection

In general, when a new object is generated and the Eden application space fails, the minor GC is triggered, GC is performed on the Eden Zone, the non-surviving objects are cleared, and the surviving objects are moved to the survivor area. Then tidy up the two districts of survivor. This method of GC is carried out on the young generation of the Eden area and does not affect the old generation. Because most objects start in the Eden area, and the Eden area is not very large, GC in the Eden area is frequent. Thus, it is generally necessary to use fast and efficient algorithms, so that Eden can be free as soon as possible.

The full GC is slower than the minor GC because it requires the entire heap to be recycled, including young, old, and perm, so you should minimize the number of complete GC times. In the process of tuning the JVM, a large part of the work is to adjust the FULLGC.

The full GC may be caused by the following reasons:

    • The old generation (tenured) was written full

    • Persistent generation (Perm) is full

    • System.GC () is displayed call

    • Dynamic changes in the domain allocation policy of the heap after the last GC

5), garbage collection typical algorithm

(1). Mark-sweep (Mark-Clear) algorithm

(2). Copying (copy) algorithm

(3). Mark-compact (marker-collation) algorithm

(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.

Most garbage collectors now take copying algorithms for the new generation. 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.

6), Garbage collector

Garbage collection algorithm is the theoretical basis of memory recovery, and garbage collector is the concrete implementation of memory recovery.

(1). Serial/serial old

The serial/serial old collector is the most basic and oldest collector, which is a single-threaded collector and must suspend all user threads when it is garbage collected. Serial Collector is for the new generation of collectors, the use of the copying algorithm, Serial old collector is a collector for the older era, using the mark-compact algorithm. Its advantages are simple and efficient, but the disadvantage is that it will bring a pause to the user.

(2). Parnew

The Parnew Collector is a multithreaded version of the serial collector that uses multiple threads for garbage collection.

(3). Parallel Scavenge

The Parallel scavenge collector is a new generation of multi-threaded collectors (parallel collectors) that do not need to pause other user threads during recycling, using the copying algorithm, which differs from the first two collectors, primarily to achieve a manageable throughput.

(4). Parallel old

Parallel old is the older version of the Parallel scavenge collector (parallel collector), using multithreading and Mark-compact algorithms.

(5). Cms

The CMS (current Mark Sweep) collector is a collector that targets the shortest payback time and is a concurrent collector, using the mark-sweep algorithm.

(6). G1

The G1 collector is the forefront of today's collector technology, a collector for service-side applications that leverages multi-CPU, multicore environments. So it is a parallel and concurrent collector, and it can build a predictable pause-time model.

3.JVM parameters

1). Heap

-XMX: Max heap Memory

-XMS: Initial heap memory

-xx:maxnewsize: Max Young area Memory

-xx:newsize: initial Young area memory

-xx:maxpermsize: Maximum persistent with memory

-xx:permsize: initial persistent with memory

2). Stack

-XSS: Set the stack size for each thread

3). Garbage collection

4. Heap VS Stack

The JVM stack is the unit of the runtime, and the JVM heap is the unit of storage.

The JVM stack represents the processing logic, and the JVM heap represents the data.

An object is stored in the JVM heap. The JVM stack is a reference to the base data type and the objects in the JVM heap.

The JVM heap is shared by all threads, and the JVM stack is thread-exclusive.

is the parameter passing in Ps:java a value or is it a pass-through?

As we all know: the transfer of function parameters in C is: value passing, address passing, and reference passing three kinds of forms. in Java, however, there is only one way to pass the parameters of a method: value passing. A value pass is a copy (replica) of the actual parameter value passed in to the method, and the parameter itself is unaffected.

To illustrate this issue, first make two points clear:

1. References in Java are a data type, with the same status as the base type int, and so on.

2. The program runs always in the JVM stack, so when parameters are passed, there is only a problem passing the base type and object references. The object itself is not passed directly.

In the run JVM stack, the basic type and the reference are handled the same, all of which are value-passing. If it is a quoted method call, it can be understood as a "reference value" of the call, that is, "reference value" was made a copy, and then assigned to the parameter, the reference processing is exactly the same as the basic type. However, when the invoked method is entered, the referenced value passed is interpreted (or found) by the program to the object in the JVM heap, which corresponds to the actual object. If you modify at this point, you modify the reference object instead of the reference itself, which is: the data in the JVM heap is modified. So this is a change that can be maintained.

For example:

?
1234567891011121314151617181920212223 packagecom.demo3; publicclassDataWrap {    publicinta;    publicintb;}packagecom.demo3;public classReferenceTransferTest {    publicstaticvoidswap(DataWrap dw) {        inttmp = dw.a;        dw.a = dw.b;        dw.b = tmp;    }    publicstaticvoidmain(String[] args) {        DataWrap dw = newDataWrap();        dw.a = 6;        dw.b = 9;        swap(dw);    }}

The corresponding memory graph:

Attached: JVM Detail diagram

JVM run-time memory structure

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.