The memory model of the Java Virtual machine

Source: Internet
Author: User
Tags garbage collection

1. Overview

For those engaged in C, C + + program development, in the memory management field, they both have the highest power of the "emperor" is also engaged in the basic work of the "working people"---have each object of "ownership", but also assume the life of each object to the end of the maintenance responsibility.

But for Java programmers, with the help of the virtual machine automatic memory management mechanism, it is not necessary to write paired Delete/free code for each new operation, it is not easy to be in memory leak and memory overflow problem, and the virtual machine manages the memory. It all looks good. However, it is because Java programmers put the right to memory control to the Java Virtual machine, in the event of memory leaks and overflow problems, if you do not understand how the virtual machine uses memory, then troubleshooting will be a very difficult task. Let's look at the memory model of the Java Virtual machine.

2. Run-time data region

Virtual opportunities in running Java programs divide memory into different data regions that have their own purpose and time to create and destroy. As a rule, the Java Virtual machine includes the following runtime memory areas.

2.1 Program Counters

A program counter is a small amount of memory space that can be seen as the line number indicator of the byte code executed by the current thread. In other words, in the current thread, the next statement that needs to be executed, such as branching, looping, jumping, and exception handling, is guided by it.

Because Java supports multi-threading, in order to ensure that after the thread switch to run smoothly, each thread will have a separate program counter, the counters between the various threads do not affect each other, isolated storage, we call this type of memory area is "thread-Private" memory. It is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.

2.2 Java Virtual Machine stack

Like program counters, the Java Virtual machine stack is also "thread-Private" , which describes the memory model that the Java method executes : Each method creates a stack frame to store the variable table, the operand stack, the dynamic link, Method exports and other information. Each method from the call to the completion of the process, corresponding to a stack frame in the virtual machine into the stack to the process of the stack.

The local variable table holds the various data types that are known at compile time (Boolean, Byte, char, short, int, float, long, double), object reference, and ReturnAddress type. The amount of memory space required for a local variable table is allocated during compilation, and when entering a method, this method needs to allocate how much local variable space in the frame is fully deterministic and will not change during operation.

For this region, a Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the stack depth allowed by the virtual machine. Most virtual machines support the dynamic expansion of the virtual machine stack, and if the extension cannot request enough memory, it throws a OutOfMemoryError exception.

2.3 Local Method Stack

The role of the local method stack and the virtual machine stack is very similar, but the difference between them is that the virtual machine stack executes the Java method service for the virtual machine, while the local method stack uses the Native method service for the virtual machine. Some virtual machines even merge the local method stack directly with the virtual machine stack. As with virtual machine stacks, the local method stack area throws Stackoverflowerror and OutOfMemoryError exceptions.

2.4 Java Heap

Java pair (Java Heap) is the largest piece of memory managed by a Java virtual machine, a piece of memory shared by all threads, created at the time of virtual machine startup, and the only purpose of this memory area is to hold objects .

The Java heap is the main area of garbage collector management, so many times it is called the "gc heap. Java heap Memory is further subdivided, either to better reclaim memory or to allocate memory more quickly.

According to the Java Virtual Machine specification, the Java heap can be a physically discontinuous memory space, as long as it is logically contiguous, just like our disk space. An OutOfMemoryError exception is thrown when there is no memory in the pair to complete the instance assignment, and the heap cannot be expanded.

2.5 Method Area

The method area, like the Java heap, is an area 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 Java Virtual Machine specification has a very loose limit on the method area, it does not require contiguous memory and can choose to be fixed size or extensible, and it can choose not to implement garbage collection , because the garbage collection behavior in this area is relatively small, and the "performance" of garbage recycling is also difficult to be satisfactory.
Note: In later versions of JDK1.7, the string constant pool that was originally placed in the method area has been removed.

2.6 Running a constant-rate pool

Running a constant pool is part of the method area. In addition to the descriptions of the various versions, fields, methods, interfaces, and other descriptive information in the class file, there is also a constant pool (Constant pool Table) that holds the various literal and symbolic references generated during the compilation period, which will be loaded into the run-time pool of the approach area when the class loads.

Another important feature of running a constant pool relative to a Class file's const pool is its dynamic nature, where the Java language does not require constants to be generated only at compile time, and new constants may be put into the pool during runtime, a feature that is used by developers to intern methods of the String class.

The above is the Java Virtual Machine memory model, the main content from the "deep understanding of Java Virtual Machine" this book.

Memory model of the Java Virtual machine

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.