Java Virtual machine Memory area detailed

Source: Internet
Author: User

It is well known that Java programs run on the Java Virtual Machine (JVM), so how is the memory allocated when the JVM is running? Each part of the program is stored in the memory of which part, and how to access, below, let me explain the Java Virtual Machine memory area.


Why you need to know the Java Virtual Machine memory area

In contrast to C + + programmers, because of the existence of automatic memory management mechanisms for virtual machines, Java programmers often do not need to worry about memory leaks and memory overflow issues. But it is because of the memory control to the JVM, in the event of memory leaks and overflow problems, if you do not understand how the virtual machine uses memory, it is difficult to troubleshoot errors.

memory leaks: After some objects are used, the system does not reclaim the memory occupied by the object or is not recycled in time, resulting in waste.

Memory Overflow: When you want to allocate memory to an object or variable, there is not enough memory


Java Virtual machine Memory region resolution

As shown, the memory that the JVM manages includes the following run-time data areas


Program counter: It is a thread-private memory . It has a smaller memory footprint and can be seen as the line number indicator of the bytecode that is being executed by the current thread. The bytecode parser works by changing the value of this program counter to select the next byte-code instruction to execute. (that is, to ensure that threads are executed in the order the programmer wants)

If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed, and if a native method is being executed, this counter is empty. This memory area is the only area of memory that is not specified in the Java Virtual Machine specification for any OUTOFMEMORYERROR exception conditions.

Why is this part of the memory private? Because at any given moment, a processor will only execute instructions in one thread. Therefore, in order to be able to return to the correct execution location after the thread switch, each thread needs to have a separate program counter, each thread sees the counter does not affect each other, independent storage.


virtual machine stack: It is also a thread-private memory , which is the "stack memory" in our usual general parlance. The virtual machine stack describes the memory model that is executed by the Java method: When each method is executed, a stack frame is created to store the local memory table, the Operation Stack, the dynamic link, the method exit, and so on. Each method is called until the completion of the process, corresponding to a stack frame in the virtual machine stack from the stack to the process of the stack. but when a method executes, the stack frame is reclaimed by memory, and the object defined in this method, as well as some final variables, becomes useless, so the memory space they occupy must be reclaimed, or memory leaks.

The local variable table holds the various basic data types, object references (reference types, which are not equivalent to the object itself, depending on the virtual machine implementation, which may be a reference pointer to the starting address of the object. It may also point to a handle representing the object or other location associated with the object) and the Retrunaddress type (the address that points to a bytecode directive)

There are two exceptions to the memory area of the virtual machine stack:

1. Stackoverflowerror exception will be thrown if the thread requests a stack depth (which can be understood as the length of the stack) greater than the depth allowed by the virtual machine

2, if the virtual machine stack can be dynamically extended, but when the extension cannot request enough memory, will throw OutOfMemoryError exception


Local method Stack: It is also a thread-private memory. It is similar to the function and function of the virtual machine stack, only the method service that the virtual machine stack performs for the virtual machine, and the local method stack is the native method service used by the virtual machine.


java heap: It's a thread-sharing, and it's a "heap of memory" in our general parlance. ". It is the largest piece of memory managed by a virtual machine and is created when the virtual machine is started. the only purpose of this memory area is to hold object instances, where almost all object instances and arrays are allocated memory

The Java heap is the main area of garbage collector management because almost all object instances allocate memory here, and when the object is used, the garbage collector manager should reclaim this part of the memory

The Java heap can be in a physically discontinuous memory space, but logically contiguous. A OutOfMemoryError exception is thrown if there is no memory in the heap to complete the allocation of the object instance, and the heap cannot be extended.


Method Area: It is thread-shared . It is used to store data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine.

Compared to the Java heap, which is the main area of garbage collector management, the Java Virtual machine has a very loose limit on the method area, and it can choose not to implement garbage collection. The memory recovery target for this area is primarily for the collection of constant pools and for unloading types .

Chang is a part of the method area that holds the various literal and symbolic references generated during the compilation period, which is stored in the method area running a constant pool after the class is loaded.

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


Direct Memory: Direct Memory is not a JVM-managed memory, so to understand, direct memory, is outside the JVM machine memory, for example, you have 4G of memory, the JVM occupies 1G, then the rest of 3G is the direct memory, In the JDK, there is a way to allocate memory based on channels (channel) and buffers (buffer), and the native libraries implemented by the C language are allocated in direct memory and referenced by the directbytebuffer stored in the JVM heap. OutOfMemoryError exceptions can also occur because direct memory is limited to the memory of this machine.


At this point, we already know what part of the Java program variables are stored in the JVM memory, and the role of each memory area, is not an enlightened feeling

We will introduce in the next Article blog postHow the JVM implements object access

Java Virtual machine Memory area detailed

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.