Deep understanding of the JVM (i)--JVM memory model

Source: Internet
Author: User
Tags garbage collection

JVM Memory Model

The memory space of the Java Virtual machine (Java MACHINE=JVM) is divided into five parts, respectively:
1. Program counter
2. Java Virtual machine stack
3. Local Method Stack
4. Heap
5. Method area.

The following is an in-depth introduction to these five regions.

1. Program Counter 1.1. What is a program counter.

A program counter is a small memory space that can be viewed as the line number indicator of the byte code that the current thread is executing. In other words, the program counter records the address of the byte-code instruction that the current thread is executing.
Note: However, if the current thread is executing a local method, then the program counter is empty.

1.2. The function of the program counter

The program counter has two functions: the bytecode interpreter can read the instruction sequentially by changing the program counter, which realizes the process control of the code, such as sequential execution, selection, circulation and exception handling. In the case of multithreading, the program counter is used to record where the current thread is executing, so that when the thread is switched back, it is possible to know where the thread last ran.

1.3. The program counter is characterized by a small storage-space thread that is private. Each thread has a program counter. is the only memory area that does not appear outofmemoryerror. The lifecycle is created as the thread is created, and dies as the thread ends.

2. Java Virtual machine stack (JVM stack) 2.1. What is Java Virtual machine stack.

Java Virtual machine stacks are memory models that describe how Java methods run.
The Java Virtual machine stack creates an area called a stack frame for each Java method that is about to be run, which is used to store some of the information that the method needs during its operation, including: Local variables table
A variable that holds the base data type variable, the reference type, and the ReturnAddress type. Operand stack dynamic Link method export information etc.

When a method is about to be run, the Java Virtual machine stack first creates a "stack frame" for the method in the Java virtual machine stack, including a local variable table, an operand stack, a dynamic link, a method export information, and so on. When a method needs to create a local variable in the process of running, the value of the local variable is stored in the local variable table of the stack frame.
When this method is finished, the stack frame corresponding to this method will stack up and free up memory space.

Note: people often say that the Java memory space is divided into "stack" and "heap", the stack of local variables stored in the heap of objects.
This sentence is not entirely correct. The "heap" here is understandable, but the "stack" here represents only the local variables table portion of the Java Virtual machine stack. The real Java Virtual machine stack is made up of stack frames, and each stack frame has: local variable table, operand stack, dynamic link, method export information.

2.2. Java Virtual machine Stack The creation of a local variable table is created when the method is executed, as the stack frame is created. Furthermore, the size of the local variable table is determined at compile time, and it is only necessary to allocate a predetermined size at the time of creation. In addition, the size of the local variable table does not change while the method is running. There are two kinds of exceptions to the Java Virtual machine stack: Stackoverflowerror and OutOfMemoryError.
A) Stackoverflowerror:
If the memory size of the Java Virtual machine stack does not allow dynamic expansion, the Stackoverflowerror exception is thrown when the thread requests that the stack be deeper than the current Java Virtual machine stack's maximum depth.
b) OutOfMemoryError:
If the Java virtual machine stack memory size allows dynamic expansion, and when the thread requests the stack memory ran out, can not be dynamically extended, this time throw OutOfMemoryError exception. Java Virtual machine stacks are also thread-private, with each thread having its own Java Virtual machine stack, and created with the creation of threads that die with the thread's death.

Note: The similarities and differences between Stackoverflowerror and OutOfMemoryError.
Stackoverflowerror indicates that the current thread requests a stack that exceeds the maximum depth of a predetermined stack, but there may be more memory space.
OutOfMemoryError refers to the fact that when a thread applies for a stack, it finds that the stack is full and that the memory is exhausted.

3. Local method Stack 3.1. What is a local method stack.

The local method stack is similar to the Java Virtual Machine stack implementation, except that the local method area is the memory model that the local method runs.

When the local method is executed, a stack frame is also created on the local method stack, which holds the local variable table, operand stack, dynamic link, and export information for the native method.

The stack frame will also stack and free up memory space after the method has finished executing.

Also throws Stackoverflowerror and OutOfMemoryError exceptions. 4. Heap 4.1. What is a heap.

A heap is the memory space used to hold an object.
almost all objects are stored in the heap.

4.2. The characteristics of the heap thread sharing
The entire Java Virtual machine has only one heap, and all threads have access to the same heap. and the program counter, Java Virtual machine stack, local method stack is a thread corresponding to one. Create the primary site for garbage collection when the virtual machine is started. Can be further subdivided into: the new generation, the old age.
The new generation can be divided into: Eden, from Survior, to Survior.
Different areas store objects with different lifecycles. This can be more targeted and more efficient by using different garbage collection algorithms for different areas. The size of the heap can be fixed or extensible, but the size of the mainstream virtual machine heap is extensible, so when a thread requests to allocate memory, but the heap is full and the memory is full and cannot be extended, the OutOfMemoryError is thrown.

5. Method Area 5.1. What is a method area.

The method area defined in the Java Virtual Machine specification is a logical part of the heap.
The method area holds class information that has been loaded by the virtual machine, constants, static variables, compiled code for the Just-in-time compiler, and so on.

5.2. Method area features thread sharing
The method area is a logical part of the heap, and therefore, like the heap, is thread-shared. There is only one method area in the entire virtual machine. Permanent generation
The information in the method area generally needs to exist for a long time, and it is the logical partition of the heap, so we call the method area the old age by using the method of heap partitioning. Low memory recovery efficiency
The information in the method area generally needs to exist for a long time, and only a small amount of information may be invalid after the memory is reclaimed.
The primary goal of memory recycling for the method area is to reclaim the constant pool and unload the type. The Java Virtual Machine specification is more relaxed about the method area requirements.
As with heaps, allows for fixed size, extensible size, and garbage collection is not implemented.

5.3. What is a running-time-constant-volume pool.

The method area holds three types of data: class information, constants, static variables, and Just-in-time compiler-compiled code. Where constants are stored in a running constant pool.

We generally declare a constant in a class through public static final. This class is compiled to generate a class file, and all the information for that class is stored in this class file.

When this class is loaded by a Java virtual machine, the constants in the class file are stored in the Run-time constant pool of the method area. And during run time, you can add new constants to the constant pool. For example, the Intern () method of the String class can add string constants to a constant pool during run time.

Garbage collector recycling is required when some constants in the runtime pool are not referenced by the object and are not referenced by the variable.

6. Direct Memory

Direct memory is memory other than the Java virtual machine, but it can also be used by Java.

An IO method based on channel and buffering is introduced in NiO. It can directly allocate memory outside the Java Virtual machine by invoking the local method, and then manipulate the memory directly through a Directbytebuffer object stored in the Java heap without first replicating the data in the outer memory to the heap, thereby increasing the efficiency of the data operation.

The size of the direct memory is not controlled by the Java Virtual machine, but since it is memory, it throws a Oom exception when there is not enough memory.

To sum up There are two stacks in the memory model of the Java Virtual machine: Java Virtual machine stack and local method stack.
The function of two stacks is similar to the memory model of the method running process. And the two "stacks" have the same internal constructs, all of which are thread-private.
The Java Virtual machine stack, though, describes the memory model of the Java method's running process, and the local method stack is the memory model that describes how the Java native method runs. There are two "heaps" in the memory model of the Java Virtual machine, one is the original heap, the other is the method area. The method area is essentially a logical part of the heap. Heap to hold objects in the method area, containing class information, constants, static variables, and Just-in-time compiler-compiled code. The heap is the largest chunk of memory in a Java Virtual machine and is the main working area of the garbage collector. program counters, Java Virtual machine stacks, and local method stacks are thread-private, meaning that each thread has its own program counter, Java Virtual machine stack, and local method area. And their life cycle is the same as the thread they belong to.
While the heap, method area is thread-shared, there is only one heap, one method stack in the Java Virtual machine. It is created when the JVM is started, and the JVM stops before it is destroyed.

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.