Java Memory Area

Source: Internet
Author: User

---restore content starts---

Bibliography: In-depth understanding of Java Virtual Machine Zhou Zhiming

The Java Virtual machine Specification SE 7 release specifies that the memory managed by the Java Virtual machine will include the following runtime data regions.

program Counter Register is a small amount of memory space that can be used as a signal indicator of the byte code executed by the current thread. In the virtual machine concept mode, the bytecode interpreter works by changing the value of this counter to select the next byte code instruction to execute. In multi-threaded programs, in order to be able to switch between threads, you can return to the correct location to start execution, each thread is a separate program counter. If the method is a Java method, this counter records the address of the bytecode instruction that the JVM is executing, and if the method being executed is native, then the value of 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.

Java Virtual Machine Stack

As with program counters, the Java Virtual machine stack (Java VM stack) is also thread-private. It has the same life cycle as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each time a method is executed, a stack frame is created to store the local variable table, the operand stack, the dynamic link, the method exit, and so on. Each method is called until the completion process corresponds to a stack frame in the virtual machine stack from the stack to the stack. (c + +, when the function is called, the first stack is the address of the next instruction in the function (the next executable statement of the function call statement), and then the parameters of the function, most of the C compiler, the argument is from right to left into the stack, and then the function of the local variables. Static variables are not in the stack. The local variable table of the JVM virtual machine stack holds the various primitive types, object references, returnarress types (addresses that point to a bytecode directive) known to the compiler.

The JVM stack can be implemented as a fixed size, or it can be dynamically extended based on calculations. You can use the-XSS parameter to adjust the stack memory capacity.

JVM Stack Exception Condition:

Stackoverflowerror: The stack depth of a thread request is greater than the maximum allowable depth for the virtual machine. This is the kind of situation that is commonly encountered.

OutOfMemoryError: The virtual machine cannot request enough memory space on the scale stack.

If a memory overflow is caused by a multithreaded build-up, it can only be exchanged for more threads by reducing the maximum heap and reducing the stack capacity if the number of threads cannot be reduced or a 64-bit virtual machine is replaced.

Local method Stack

The local method stack is used by the virtual machine to support the Native method used by the Native. The language, usage, and data structures used by methods in the local method stack are not mandated in the virtual machine specification. Hotspot directly merged the local method stack with the virtual machine stack.

Exception: Stackoverflowerror and OutOfMemoryError exceptions are thrown as well as virtual machine stacks.

Java Heap

For most applications, the Java heap (heap) is a chunk of memory that is shared by all threads. In general, the heap is the primary area where Java places object instances. However, with the development of JIT compilation technology, stack allocation and scalar substitution optimization techniques (which require the support of escape analysis technology) also make this allocation way less absolute. The Java heap is the primary area of garbage collection (the method area also reclaims some unused constants, etc.). The Java heap can be subdivided into: the new generation, the old age, the permanent generation. The objects stored in the heap are managed by the automatic management of the memory system (Automatic Storage Management system, also known as the "garbage Collector (garbage collector)"), so that Java programmers do not have to, and cannot display, destroy objects. The capacity of the Java heap can be a fixed size or can be dynamically scaled with demand (via-XMX and-xms control). The memory used by the Java heap does not need to be guaranteed to be physically contiguous, as long as it is logically contiguous.

Java Heap Exception:

OutOfMemoryError: Throws an exception if there is no memory in the heap to complete the instance assignment, and the heap can no longer be expanded.

Method area

The method area, like the Java heap, is an area of memory shared by each thread that is used to store data such as class information, constants, static variables, and code compiled by the instant compiler that have been loaded by the class-loading subsystem. The capacity of a method area can be a fixed size, or it can be scaled dynamically as the requirements of the program execute, and shrink automatically when too much space is not required. The method area can be discontinuous in the actual memory space.

Java Method Area Exception:

OutOfMemoryError: The memory space of the method area does not meet the memory allocation request.

Method-area Overflow is also a common memory overflow exception. In applications where a large number of classes are generated on a regular basis, special attention needs to be paid to the class recycling status. Common scenarios are: a large number of JSP or dynamically generated JSP file applications, OSGi-based applications, and so on.

run a constant-rate pool ( Runtime Constant Pool )

Running a constant pool is part of the method area. The run-time pool is used to hold various literal and symbolic references generated by the compiler, which is stored in the class load backward into the run-time pool.

Exceptions that you may encounter when you are running a constant pool of classes and interfaces:

OutOfMemoryError: As with the method area, a OutOfMemoryError exception is thrown.

Direct Memory ( Direct Memory )

NIO introduces channel-and buffer-based I/O, which can be used to allocate out-of-heap memory using the native function, and then operate through a Directbytebuffer object stored in the Java heap as a reference to this memory.

Java Memory Area

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.