Java memory area and memory overflow exception

Source: Internet
Author: User
Tags xms

To understand this knowledge is due to a OutOfMemoryError exception, then the solution is to add the-xmx1024m parameter to expand the Java heap, but also add another parameter-xx:-usegcoverheadlimit. Although the problem solved, but still feel can understand a little more, so buy a book to see.

Java Memory Area:

The memory managed by the Java Virtual Machine includes the following runtime data regions:

Both: Program counter, Java Virtual machine stack, local method Stack, Java heap, and method area.

Program counter

is a small memory space that can be seen as the line number indicator of the bytecode that is being executed by the current thread.
Because the multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, at any given time, a processor (a kernel for a multicore processor) will only execute instructions in one thread. Therefore, in order to recover the thread after switching to the correct execution location, each thread has a separate program counter, the counters between each other, independent storage, called the memory area as "thread-private" memory (virtual machine stack, the local method stack is also).
If the thread executes a Java method, this counter records the address of the virtual machine bytecode instruction being executed, and if the native method is executed, the counter value is empty (Undefined). The secondary memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.

Java Virtual Machine stack

is also thread-private, and its life cycle is the same as the thread. The virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame to store local variable tables, operand stacks, dynamic links, method exits, and so on. Each method from the call until the only thought of the process is corresponding to a stack frame from the path into the stack of the process.

Local method Stack

The role of the virtual machine stack is very similar, but the difference is that the Java Virtual machine stack is fine and executes the Java method service, and the local method stack serves the native method used by the virtual machine.

The virtual machine specification heap local method stack and Java Virtual Machine stack stipulate two exceptions: if the thread requests a stack with a depth greater than the virtual machine allows, the STACKOVERFLOWERROR exception will be thrown, and if the virtual machine stack can be dynamically extended, if the extension cannot request enough memory, Throws a OutOfMemoryError exception.

Java heap

For most applications, the Java heap is the largest piece of the memory that is managed by the virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of the secondary memory area is to hold the object instance, where almost all of the object instances are allocated memory. The Java heap garbage collector manages the main areas, so many times it is also referred to as the "GC" heap. 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. Implementations can be either fixed-sized or extensible, and the current mainstream virtual machines are implemented in a scalable way (via-XMX and-xms control). A OutOfMemoryError exception is thrown when there is no memory in the heap to complete the instance assignment and cannot be extended.

Method area

Like the Java heap, the memory area shared by each thread is used to store data such as the class information that has been loaded by the virtual machine, solid, static variables, and code compiled by the instant compiler. Java Virtual Machine The method area is described as a logical part of the heap, but it has a name non-heap (not a heap), which is intended to be distinguished from the Java heap.

Run a constant-rate pool

(Rintime Constant Pool) is part of the method area. In addition to descriptive information such as the class's version, field, method, and a few other descriptions, the class file also contains a constant pool (Constant pool Table) that holds the various literal and symbolic references generated during the compilation period, which are stored in the run-time pool of the class load backward into the method area. One of the important features of running a constant pool is dynamic, and the Java language does not require constants to be generated only at compile time, that is, the contents of a constant pool that is not pre-placed into a class file can be entered into the method to run the regular pool, and the new constants may be put into the pool during the run. This feature is used more than the Intern () method of the String class.

Memory Overflow Exception: Java Heap overflow:

Java.lang.OutOfMemoryError:Java Heap space. The Java heap is used to store object instances, as long as the objects are constantly created, and the GC ROOTSD to the object before it has an attainable path to prevent the recycling mechanism from being clear about these objects, a memory exception is generated when the number of objects reaches the maximum heap capacity limit. Can be set by-XMS minimum and-xmx maximum values.

Virtual machine stack and local method stack overflow:

If the thread requests a stack depth greater than the maximum allowed depth for the virtual machine, the Stackoverflowerror exception is thrown, and if the virtual machine cannot request enough memory on the scale stack, the OutOfMemoryError exception is thrown. Set by the-XSS parameter.

Method area and run constant pool overflow:

Java.lang.OutOfMemoryError:PermGen space.
Set by-xx:permsize and-xx:maxpermsize.

Direct Memory Overflow:

Due to insufficient system memory.
Set by-xx:maxdirectmemorysize.

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.