Java memory area and memory overflow exception

Source: Internet
Author: User

I. Overview

Java as a high-level language, not a completely independent system, usually rely on the underlying operating system, but managed to maintain the Java virtual memory, "Java and C + + has a memory dynamic allocation and garbage collection technology surrounded by the ' high wall ', the wall outside the people want to come in, the wall inside people want to come out."

With the help of the virtual machine automatic memory management mechanism, the Java programmer does not need to write the paired Delete/free code for each new operation, but it is not prone to memory leaks and memory leaks: After the memory has been allocated, the memory is occupied by the user regardless of whether they have finished their work. , cannot be reclaimed, memory overflow: The actual memory at run time is insufficient to support the required memory. )

While the virtual machine manages memory, it makes us feel that using Java programming looks very automated, but in our actual development process, there is not absolutely no memory leak or memory overflow problem.

Second, run-time data area

Java virtual machines in the process of executing Java programs, the managed memory is divided into several different areas: program counters, virtual machine stacks, local method Stacks, Java heap, method area.

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.

We know that multiple threads of a virtual machine are implemented by thread switching and allocating processor execution time in a rotational fashion. A processor core that executes only one thread instruction at a time, each thread requires a separate program counter, and the counters between the threads do not affect each other, isolated storage (thread-private).

If the thread is executing a Java method, the counter logs the executing virtual machine bytecode instruction address, and if the thread executes the native method (the local method, which is a non-Java code interface called by Java, such as C), the counter value is null (Undefined).

The program counter area is the only one in the Java Virtual Machine specification that does not specify any OutOfMemoryError (memory overflow) conditions.

2. Virtual Machine stack

Thread, is also a thread-private, virtual machine stack for the implementation of Java methods, each method executes at the same time, will create a stack frame, used to store local variables table, operand stack, dynamic link, method exit and other information, from the method call to execution completion, corresponding to a stack frame in the virtual machine into the stack to the stack process. The amount of memory space required for a local variable table is allocated during compilation, and when entering a method, the method needs to allocate much of the local variable space in the frame is fully deterministic and does not change the size of the local variable table while the method is running.

A Stackoverflowerror exception (stack overflow) 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 scaled and cannot be applied to enough memory for expansion.

3. Local Method Stack

The local method stack is very similar to the virtual machine stack, and the difference between them is that 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 (Native). As with virtual machines, local method stack regions also throw stackoverflowerror and OutOfMemoryError exceptions.

4. Java Heap

The Java heap is an area of memory that is shared by all threads.

For the most part, the Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine and is created when the virtual machine is started. The only purpose of this memory area is to hold object instances, but objects are not absolutely allocated on the Java heap.

Java heap is the main area of garbage collector management, so often referred to as the "GC Heap", from the perspective of memory recycling, since the current collector is basically a generational collection algorithm, so the Java heap can also be subdivided into several areas, but regardless of how, regardless of the area, the storage is still an object instance , the purpose of further partitioning is to better reclaim memory, or to allocate memory more quickly.

The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous, can be either fixed-sized or extensible, controlled by-XMX and-XMS. A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.

5. Method area

A method area is a region of memory shared by each thread that stores data such as class information that has been loaded by the virtual machine, solid, static variables, and code compiled by the immediate compiler.

The Java Virtual machine specification describes a method area as a logical part of a heap, but it has an alias, non-heap (not a heap), intended to be separate from the Java heap. The hotspot garbage collector manages the method area like the Java heap, but the virtual machine specification limits the method area very loosely, with the exception of the Java heap, which does not require physically contiguous memory and can choose a fixed size or cocoa extension, and optionally does not implement garbage collection. In contrast, the garbage collection behavior in this area is less than the occurrence, the memory recovery target of this area is mainly for the recovery of constant pool and the type of unloading, but the recovery performance is more difficult to be satisfactory, especially the type of unloading, the conditions are quite harsh.

According to the Java Virtual Machine specification, a OutOfMemoryError exception is thrown when the method area does not meet the memory allocation requirements.

Java memory area and memory overflow exception

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.