2nd Java memory area and memory overflow exception

Source: Internet
Author: User

2.1 Run-Time data regions

The Java Virtual machine divides the memory it manages into several different data regions during the execution of a Java program. These zones have their own purpose, as well as the creation and destruction of time, and some regions exist as the virtual machine process starts, and some regions depend on the user thread's start and end to build and destroy. As shown in the following:

2.1.1 Program counter

A program counter is a small amount of memory space, which is the thread's private memory, which can be seen as the line number indicator of the bytecode that is executed when the front thread executes. In the virtual machine concept model (only the conceptual model, the various virtual machines may be implemented in some more efficient way), the bytecode interpreter works by changing the value of this counter to select the next need to execute the bytecode instruction, branch, loop, jump, exception handling, Basic functions such as thread recovery need to rely on this counter to complete.

Because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given moment, a processor (a kernel for a multicore processor) executes only the instructions in one thread. Therefore, in order for the thread to switch back to the correct execution location, each thread needs to have a separate program counter, the counters between the various threads do not affect each other, isolated storage, known as "Thread-private" memory.

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

2.1.2 Java Virtual Machine stack

The Java Virtual machine stack is thread-private. It describes the memory model that the Java method executes: Each method creates a stack frame at the same time, for storing local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.

Local Variables Table : Contains the various basic data types (boolean,byte,char,short,int,float,long,double), object references (reference types, which are not equivalent to the object itself, It could be a reference pointer to an object's address, or it could be a handle to a representative object or other location associated with the object, and the ReturnAddress type (the address of a bytecode directive).

Note: The 64-bit length of long and double data takes up 2 local variable space (slots), and the remaining data types occupy only 1. The memory space required for a local variable table is allocated during compilation, and when entering a method, it is entirely deterministic how much of the local variable space is allocated in the stack frame, and does not change the size of the local variable table while the method is running.

In the Java Virtual Machine specification, two exceptions are specified for the Java Virtual Machine stack: (1) throws a Stackoverflowerror exception if the thread requests a depth greater than the virtual machine allows ; (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), if the extension cannot request enough memory, the OutOfMemoryError exception will be thrown.

2.1.3 Local Method Stack

The role of the local method stack and the virtual machine stack is very similar, but the difference between them is that the virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, while the local method stack is the native method service used by the virtual machine. In the virtual machine specification, the language used in the methods used in the local method stack is not mandated by the method and data structure, so the specific virtual machine can be implemented freely. Even some virtual machines, such as Sun hotspot virtual machines, merge the local method stack and the virtual machine stack directly. As with virtual machine stacks, the local method stack area throws Stackoverflowerror and OutOfMemoryError exceptions.

2.1.4 Java Heap

2nd Java memory area and memory overflow exception

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.