In-depth understanding of Java Virtual Machines notes Chapter II Java Virtual machine memory area

Source: Internet
Author: User

Introduction

??

This chapter is about the memory area of the Java virtual machine can be divided into several parts, the book can be divided into five parts: heap, Stack, program counter, method area, local method stack

??

Where the heap contains an instance of the object

??

And often said the basic data type, and the object reference is stored in the stack, in fact, is actually stored in the virtual machine stack local variable table, virtual machine stack In addition to the local variable table, but also as described is the Java method execution process memory model of the responsibility;

??

When each method is executed, a stack Frame is created in the virtual machine stack to store information such as operand stacks, dynamic links, local variable tables, and so on.

??

Each method is called until its execution completes and the object has a stack frame in the virtual machine stack in the stack and out of the process.

??

In addition, the program counter, by modifying the value of the counter can choose the next need to execute the bytecode instruction, branch, loop and other basic functions are dependent on the program counter, the method area to store static variables, constants, class information, constant pool in the method area, including the String constant Pool, The last local method stack holds the stack-in information of the calling method when the local method is executed

??

A few large memory area graphs are shown below

??

??

So let's take a closer look at these parts of the memory area, what exactly is it?

??

Memory area of the Java Virtual machine

??

1. Procedure counter (program Counter Register)

??

The program counter can be seen as the line number indicator of the bytecode executed by the current thread .

??

The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute.

??

Basic functions such as branching, looping, jumping, exception handling, thread recovery, and so on, need to rely on this counter to complete.

??

Because the multithreading of a Java virtual machine is implemented in a way that threads switch CPU time slices in turn, at any one time, a processor ( a kernel for a multicore processor ) only the instructions in one thread will be line.

??

So in order for each thread to switch back to the correct execution location, each thread needs a separate program counter that is private to the thread .

??

If the current thread is executing a Java method, this counter is the address of the virtual machine bytecode being executed, and if a Native method is executed, the value of this counter is null (Undefinedd)

??

This area is the only area in the Java virtual machine that does not stipulate any outofmemoryerror .

??

2.Java virtual Machine stack

??

Like the program counter, theJava virtual machine stack (Java Stacks) is also thread-private , with the same life cycle as the thread.

??

The virtual machine stack describes the memory model that is executed by the Java method, and each time the method is executed, a stack Frame is created in the virtual machine stack to store information such as the operand stack, the dynamic link, the local variable table, and so on. Each method is called until its execution completes and the object has a stack frame in the virtual machine stack in the stack and out of the process.

??

??

The virtual machine specification illustrates that theJava virtual machine stack can be implemented as a fixed size, or can be implemented to scale and contract dynamically based on calculations.

??

If it is implemented as a fixed size, it needs to be created independently.

??

The Java Virtual machine implementation may be given to programmers and users to control the initial size of the Java virtual machine stack, of course, if it is the implementation of dynamic expansion and contraction, you can also control the maximum and minimum size of the virtual machine stack.

??

The virtual machine specification provides two exceptions in this area: if the thread request stack depth exceeds the allowed depth of the virtual machine, the virtual machine will throw a stackoverflowerror error if the virtual machine stack can be dynamically extended ( Most of the current virtual machines can be dynamically scaled, except that the Java Virtual machine specification allows a fixed-length virtual machine stack ), TheJava virtual machine throws a outofmemoryerror error when the extension cannot request enough memory or when a new thread is created without enough memory to create an initial size virtual machine stack .

??

3. Local Method Stack

??

The local methods stack (Native method Stacks) is very similar to the virtual machine stack, but the difference is that the virtual machine stack executes the Java method, and the local method stack executes the Native method.

??

The language used by the methods in the local method stack in the virtual machine specification is not mandated by the method and data structure, and the specific virtual machine can be implemented freely. Even virtual machines , such as Sun HotSpot , directly merge the local method stack with the virtual machine stack. As with the virtual machine stack, the local method stack also throws Stackoverflowerror and outofmemoryerror errors.

??

4. Java Heap

??

For most applications, the Java heap ( java heap) is the largest piece of the built-in Java virtual machine management .

??

The Java Heap is an inner area 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.

??

The description in the virtual machine specification is that all instances of the class and array objects are allocated in the heap .

??

The Java Heap is the main area of the garbage collector's role, so it is often called the GC heap (garbage collected heap).

??

From the perspective of memory recycling, the current collector is basically a generational collection algorithm

??

So the Java heap can also be subdivided into: the new generation and the old age

??

A little more detailed, the Cenozoic can be divided into Eden space, fromSurvivor space, toSurvivor space.

??

However, regardless of the partition, is not related to the content, no matter what area, storage is still an object instance, further dividing its purpose is to better reclaim memory or allocate memory more quickly .

??

According to the Java Virtual machine specification, theJava Heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

??

Can be implemented into fixed-size, can also be implemented into dynamic expansion and contraction of the

??

However, the current mainstream virtual machines can be dynamically expanded and contracted ( through -xmx and -xms control ).

??

A OutOfMemory error will be thrown if there is not enough memory in the heap to complete the instance assignment and the heap can no longer be expanded.

??

5. Method area

??

The method area, like the Java heap, is an area of memory that is shared by each thread, and is used to store class information, constants, static variables that have been loaded by the virtual machine , Real-time compiler compiled code and other data. The method area is created when the virtual machine is started.

??

For HotSpot virtual machines, the method area is also known as the " Permanent Generation (Permanent Generation)"

??

In essence, the two are not equivalent, simply because the HotSpot virtual machine extends the GC collection to the method area, or permanently to the method area. For other virtual machines ( such as BEA JRockit, IMB J9) There is no concept of a permanent generation, that is, HotSpot now has to abandon the permanent generation and " move " to Native Memory to achieve the planning of the method area ( in JDK8 , the permanent generation has been removed, and theJDK7 begins to move some data that was originally stored in the method area to Java heap ).

??

Like the Java heap, the method area does not require contiguous memory and can choose a fixed size with extensible shrinkage, and optionally does not implement garbage collection because the garbage collection effect of the method area is unsatisfactory. The OutOfMemory exception is thrown when the method area does not meet the intrinsic allocation requirements.

??

6. Run a constant-rate pool

??

The runtime contant Pool is part of the method area. In addition to the class method in the description of the version, field, method, interface, and other descriptive information, there is also a constant table (Constant Pool table), used to store the compilation period generated by the various literal and symbolic references, This section is stored in the run-time pool of the method area after the class is loaded.

??

One of the key features of running a constant pool with respect to a regular pool of class files is that it is dynamic, and theJava language does not require constants to be generated only at compile time, that is, not pre-placed class The contents of the constant pool in the method can be entered into the run-time pool of the method area, and the new constants may be placed in a constant pool, such as the Intern () method of the String class, during the program's run.

??

Running a constant is part of the method area and is naturally constrained by the size of the method area memory, which throws a OutOfMemory exception when the const pool is no longer able to apply to memory.

??

7. Direct Memory

??

Direct Memory is not part of the data area when the virtual machine is running, nor is it a memory area defined in the Java VM specification, but this part of the memory is also used frequently, and may also lead to OutOfMemoryError error occurred.

??

Garbage collection, although the virtual machine will be the direct memory recycling, but not like the new generation and the old age, the discovery of insufficient space to notify the collector to garbage collection, it can only wait until the old age is full after FULLGC , and then " by the way" cleans up objects that are discarded in direct memory.

??

New NIOhas been introduced into JDK1.4 , which introduces an I/O method based on channel and buffer, which can be used The Native Library allocates out-of-heap memory directly and then operates through a directbytebuffer object stored in the Java Heap as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and Native heap.

??

In-depth understanding of Java Virtual Machines notes Chapter II Java Virtual machine memory area

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.