Java Memory detailed

Source: Internet
Author: User

The Java programmer, with the help of the automatic memory management mechanism of the virtual machine, does not need to write the paired Delete/free code for each new operation. It is also because Java programmers put the right to memory control to the Java Virtual machine, in the event of memory leaks and overflow problems, if you do not understand how the virtual machine uses memory, then troubleshooting errors is extremely difficult.

The Java virtual machine, in the course of executing a Java program, divides the memory he manages into a number of different data regions. Each zone has its own purpose, as well as creation time and destruction time, and some regions exist as virtual machine processes start, while others are created and destroyed depending on the start and end of the user thread.

The following describes the runtime data regions of a Java Virtual machine, respectively:1. Program counter,2.Java virtual machine stack,3. Local method stack;4.Java heap;5. Method area

The first three are thread-isolated data areas, and the last two are data areas shared by all threads

1. Program Counter Register Thread Private

is a small memory space that acts as a line number indicator of the bytecode that is being 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.

Because the multithreading of a Java Virtual machine is implemented in a way that threads take turns switching and allocating processor execution time, at any given moment, a 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 threads do not affect each other, independent storage, so the thread is private.

If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null 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.

2.Java Virtual machine stack (VM stack) thread private

The life cycle is the same as the thread. The virtual machine describes the memory model that is executed by the Java method: Each method executes at the same time creates a stack frame to store the local variable table, the operand stack, the dynamic link, the method exit and other information. Each method is called 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, object references, and returnadress types known to the compiler (addresses that point to a bytecode directive).

Two exceptions are specified for this area: if the thread requests a stack depth greater than the virtual machine allows, the Stackoverflowerror exception is thrown, and if the virtual machine stack can be dynamically extended, An exception OutOfMemoryError is thrown when an extension fails to request enough memory.

3. Local methods Stack (Native method stack) thread private

Similar to a virtual machine stack, it is simply a virtual machine stack that executes Java method services for a virtual machine, while the local method stack uses the native method service for the virtual machine.

4.Java Heap (Java heap) thread sharing

is the largest piece of memory managed by a Java virtual machine. Created when the virtual machine is started, with the sole purpose of storing object instances and arrays.

is the main place where the garbage collector is managed, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous.

Method area Thread-shared

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. Garbage collection in this area is primarily for constant pools and for unloading types.

The run-time-constant pool, which stores the various literal and symbolic references generated by the compiler, is stored in the run-time pool of the method area after the class is loaded.

A feature that runs a constant pool relative to the class file constants pool is dynamic, and the runtime may also put new constants into the pool.

Java Memory detailed

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.