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

Source: Internet
Author: User

??

This phrase feels like the nature of the GC .

??

??

??

1. Procedure Counter (program Counter Register)

??

A program counter is a small amount of memory space that acts as a line number indicator of the bytecode that is being 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, and the 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. Therefore, in order to return to the correct execution position after each switch, each thread needs a separate program counter, which 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), The calculator must be able to accommodate the return address of the method or a local pointer to a specific platform. 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.

??

If viewed from a memory allocation perspective, the thread-shared Java heap may divide multiple thread-private allocation buffers (thread Local Allocation buffer, Tlab).

??

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 shared by each thread that stores data such as class information, constants, static variables , and code compiled by the immediate compiler that have been loaded by the virtual machine. 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 pool ( not understood )

??

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 ( also not understand )

?

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.