(iii) Java Virtual machine memory management and thread exclusive and thread sharing areas

Source: Internet
Author: User

First, memory management

TwoThread Exclusive ZoneProgram Counter Register)

A program counter is a small amount of memory space, which can be seen as the line number indicator of the bytecode executed by the current thread. In the virtual machine conceptual model, 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.

If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null (Undefined) If the native method is being executed. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions

Threethe thread exclusive zoneJava VM Stack (Java Virtual machine Stacks)
    • The Java Virtual machine stack is the same as the thread life cycle. It describes the memory model that the Java method executes: Each method creates a stack frame to store information such as local variable tables , operand stacks , dynamic links, method exits, and so on. Each method from the invocation until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack.
    • The local variable table holds the various basic data types known to the compiler (Boolean, Byte, char, short, int, float, long, double), Object references (reference types) and ReturnAddress types (addresses that point to a bytecode directive).
    • In this region, the Java Virtual Machine specification specifies two exceptions:

      1. Throws a stackoverflowerror exception if the thread requests a stack depth that is greater than the virtual machine's allowable depth;
      2. If the virtual machine stack can be dynamically extended (most of the current Java virtual machines can be dynamically extended, but the Java Virtual Machine specification also allows a fixed-length virtual machine stack), and the extension cannot request enough memory, it will throw OutOfMemoryError exception.

Fourthe thread exclusive zoneLocal methods Stack (Native method Stacks)

The local method stack is very similar to the virtual machine stack, except that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, and the local method stack serves the native method used by the virtual machine.

Because the virtual machine specification does not enforce the language, usage, and data structure of the local method stack, some virtual machines (such as sun hotspot virtual machines) directly combine the local method stack with the virtual machine stack.

The local method stack also throws Stackoverflowerror and outofmemoryerror exceptions.

Java heap of thread sharing area (Java heap)

The Java heap is the largest piece of memory managed by a Java virtual machine and is created at the time the virtual machine is started, with the sole purpose of storing object instances: All object instances and arrays are allocated on the heap (but with the development of the JIT compiler and the escape analysis technology gradually mature, The allocation of all objects on the heap is gradually not so absolute.

Java heap is the main area of garbage collector management, and now the collector basically uses the generational collection algorithm , so the Java heap can be subdivided into: The new generation and the old age ; From survivor Space , tosurvivor space , etc.

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 .

A OutOfMemoryError exception is thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.

Vi. method area of thread sharing area

Used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine.

The so-called method area is the term for the permanent generation (Permanent Generation) , simply because the hotspot virtual machine extends the GC generation collection to the method area, or uses the permanent generation to implement the method area. For other virtual machines there is no permanent generation of claims.

6.1 Running a constant-rate pool (runtime Constant)

A run-time constant pool is part of a method area that holds the various literal and symbolic references generated by the compiler, and typically contains direct references that are translated. This section is stored in the run-time pool of the class load backward into the method area.

The OutOfMemoryError exception is thrown when the constant pool is no longer able to request memory.

6.2 Cases
 Public class Testjconsole {    publicstaticvoid  main (string[] args) {        = "abc" ;         New String ("abc");         = = str2)        ; = = Str2.intern ());}    }

Results:

    • For example,string str1 = "abc" str1 points to a constant pool, while string str2 = new String ("abc") , str2 points to the heap memory object, The address is different so str1 = = str2 The result is false, but Str2.intern () moves the string value from the heap memory to the constant pool (if the constant pool exists, returns the address of the value). This way both str2 and STR1 are the ABC that points to the constant pool.
Vii. Direct Memory

Direct memory is not part of the data area when the virtual machine is running, nor is it an area of memory defined in the VM specification, but it can also cause a outofmemoryerror exception to occur.

For example, the newly added NiO (new Input/output) class in JDK1.4 introduces a method of I/O based on channel and buffer, which he can use to directly allocate out-of-heap memory using the native library. It then operates as a reference to this memory through a Directbytebuffer object stored in the Java heap. This can significantly improve performance in some scenarios because it avoids copying data back and forth in the Java heap and native heap.

Native Direct memory is not limited by the size of the Java heap, but is limited by the size of the native total memory (including RAM and swap or paging files) and the processor addressing space. A outofmemoryerror exception occurred while dynamically expanding.

(iii) Java Virtual machine memory management and thread exclusive and thread sharing areas

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.