Java memory overflow and java Overflow

Source: Internet
Author: User
Tags xms

Java memory overflow and java Overflow

I want to know this because of an OutOfMemoryError exception. The solution was to add the-Xmx1024m parameter to expand the Java heap and add another parameter-XX:-UseGCOverheadLimit. Although the problem has been solved, I still feel that I can learn more, so I want to buy a book.

Java memory region:

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

Program counters, Java Virtual Machine stacks, local method stacks, Java stacks, and method zones.

Program counters

Is a small memory space, which can be seen as the row number indicator of the bytecode executed by the current thread.
Java Virtual Machine multithreading is implemented by switching threads in turn and allocating the processor execution time. At any specific time, a single processor (for a multi-core processor, It is a kernel) only the commands in one thread are executed. Therefore, in order to restore the thread to the correct execution position after switching, each thread has an independent program counter. The counters between threads do not affect each other and are stored independently, this memory zone is called "thread-private" memory (Virtual Machine stack, local method stack ).
If the thread executes a Java method, the counter records the address of the Virtual Machine bytecode instruction being executed. If the native method is executed, the counter value is Undefined ). The sub-memory region is the only region in which the Java Virtual Machine specification does not specify any OutofMemoryError conditions.

Java Virtual Machine Stack

It is also private to the thread, and its lifecycle is the same as that of the thread. The Virtual Machine Stack describes the Memory Model of java method execution. Each method creates a Stack Frame during execution to store the local variable table, operand Stack, and dynamic link, method exit and other information. The process of each method from the call to the end is the process of a stack frame coming out of the stack from the ingress.

Local method Stack

The role of the VM stack is very similar. The difference is that the java VM stack serves the delicate and executed java method, and the local method stack serves the native method used by the VM.

The local method stack and Java Virtual Machine stack of the virtual machine specification stack stipulate two types of exceptions: If the depth of the stack requested by the thread is greater than the depth allowed by the virtual machine, an StackOverflowError exception will be thrown; if the Virtual Machine stack can be dynamically expanded and the memory cannot be applied for during expansion, an OutOfMemoryError error is thrown.

Java heap

For most applications, java heap is the largest piece of memory managed by virtual machines. Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of the sub-memory area is to store object instances, where almost all object instances are allocated memory. The main area of java heap Garbage Collector management, so it is often called the "GC" heap. From the perspective of memory collection, the current recycler basically uses the generational collection algorithm, so Java heap can also be divided into: New Generation and old generation, the new generation can be divided into two types: Eden space and javascor space. The Eden space stores new objects. After garbage collection, the new objects will be stored in the same vor space, when the ratio of the surviving objects in the same vor space to the Eden space exceeds a certain number, some objects will be placed in the old age area, and the method area will be permanently replaced. According to the java Virtual Machine specification, the java heap can be in physically discontinuous memory space, as long as the logic is continuous. It can be fixed or scalable during implementation. Currently, mainstream virtual machines are implemented according to the Scalability (controlled by-Xmx and-Xms ). If the heap does not have memory for instance allocation and cannot be expanded, an OutOfMemoryError error will be thrown.

Method Area

Like the java heap, It is the memory area shared by various threads. It is used to store data such as class information, always bright, static variables, and Code Compiled by the real-time compiler. The Java Virtual Machine Method Area is described as a logical part of the Heap, but it has a specific name called Non-Heap (Non-Heap) to distinguish it from the Java Heap.

Runtime constant pool

(Rintime Constant Pool) is part of the method area. In addition to the Class version, fields, methods, and descriptions, the Class file also contains a Constant Pool Table ), it is used to store various types of literal and symbolic references generated during the compilation period. This part of content will be stored in the runtime pool of the method area after the class is loaded. One of the important features of the runtime constant pool is its dynamic nature. Java does not require constants to be generated only during compilation, that is to say, it is not the content of the constant pool preset into the class file that can enter the method to run the frequent volume pool. During the running period, new constants may also be placed in the pool, the intern () method of the String class is widely used.

Memory overflow exception: Java heap overflow:

Java. lang. OutOfMemoryError: Java heap space. Java heap is used to store object instances. As long as you constantly create objects and ensure that there is an accessible path before GC Rootsd reaches the object, the collection mechanism should be clear about these objects, the Memory exception occurs when the number of objects reaches the maximum heap capacity limit. You can set the value by-Xms minimum and-Xmx maximum.

Virtual Machine stack and local method stack overflow:

If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, an StackOverflowError exception is thrown. If the virtual machine cannot apply for sufficient memory when extending the stack, an OutOfMemoryError exception is thrown. Use the-Xss parameter settings.

Method Area and runtime constant pool overflow:

Java. lang. OutOfMemoryError: PermGen space.
Set the value through-XX: PermSize and-XX: MaxPermSize.

Direct memory overflow:

This error is caused by insufficient system memory.
Set the value through-XX: MaxDirectMemorySize.

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.