Java memory area and memory overflow exception

Source: Internet
Author: User

Run-time data region

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.

1. Program counter

A program counter is a small amount of memory space that can be thought of as the line number indicator of the byte code executed by the current thread. In order for the thread to switch back to the correct execution location, each thread needs to be represented by a separate program counter. 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.

2.Java Virtual Machine Stack

The Java Virtual machine stack is also thread-private, with the same life cycle as the thread. The virtual machine stack 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 call until the completion of the process, corresponding to a stack frame in the virtual machine stack into the stack of the process.

The local variable table holds the various basic data types, object references, and ReturnAddress types (which point to the address of a bytecode directive) that are known at compile time. Long and double 64-bit lengths occupy two local variable spaces (slots), and the remainder occupy 1.

A Stackoverflowerror exception is thrown if the thread requests a stack depth greater than the virtual machine allows, and throws a OutOfMemoryError exception if the virtual machine stack can be dynamically extended and cannot request enough memory for expansion.

3. Local Method Stack

The role of the virtual machine stack is very similar, but the virtual machine stack executes Java method services for the virtual machine, while the local method stack is the native method service used by the virtual machine. The exception that is thrown is the same.

4.Java Heap

The Java heap is the largest piece of memory managed by a Java virtual machine and is a chunk of memory shared by all threads, created when the virtual machine is started. The only purpose of this memory area is to hold object instances where almost all object instances allocate memory (not absolute). Java heap is the main area of garbage collector management, in order to better reclaim memory or allocate memory more quickly, can be further divided, Eden Space, from Survivor space, to survivor space and so on.

The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous. It can be either fixed size or extensible. The OutOfMemoryError exception is thrown when there is no memory in the heap to complete the instance assignment, and the heap cannot be extended.

5. Method area

Like the Java heap, it is a region 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 design team of the hotspot virtual machine extends the GC generational collection to the method area, or uses the permanent generation to implement the method area. However, the use of the permanent generation of the implementation method area, more prone to memory overflow problems (permanent generation of-xx:maxpermsize), so in the JDK1.7 hotspot, has been placed in the permanent generation of the string constant pool out. Java Virtual Machine Specification heap the limits of the method area are very loose, in addition to the Java heap without the need for continuous memory and can choose a fixed size or extensible outside, you can also choose not to implement garbage collection. In contrast, the garbage collection behavior in this area is less than the occurrence, and is difficult to be satisfactory, but this part of the area of recovery is still necessary, the recovery target is mainly for the recovery of the constant pool and the type of unloading.

The OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

6. Run a constant pool (runtime Constant)

Part of the method area. class file In addition to the class version, fields, methods, interfaces, and other descriptive information, there is a constant pool, for the compilation period generated by a variety of literal and symbolic references, which will be loaded in the class load into the method area of the run constant pool, in addition to symbolic references, A direct reference to the translation is also stored in the run-time constant pool. The running constant pool is dynamic, and new constants may be put into the pool during run time, which is the intern () method of the string class that is used by the developer for more than one edge.

Since the run-time constant pool is part of the method area, you naturally receive a limit on the method area memory, which throws a OutOfMemoryError exception when the constant pool is no longer able to request memory.

7. Direct Memory

Direct memory is not an area of memory defined in the Java Virtual Machine specification. However, this part of memory is also used frequently, and may cause outofmemoryerror exceptions.

In JDK1.4, a new NiO class is introduced, which introduces a channel-and buffer-based I/O approach, which can be used to directly allocate out-of-heap memory using the native function 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. When configuring virtual machine parameters, parameters such as-XMX are set according to the actual memory, but direct memory is often ignored, causing the sum of each memory region to be greater than the physical memory limit, resulting in a OutOfMemoryError exception when dynamic scaling occurs.

Java memory area and memory overflow exception

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.