Java Virtual machine: A detailed description of the memory model

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, reproduced please indicate the source, Welcome to exchange Study!

We all know that when the virtual machine executes Java code, the first thing to load the bytecode file into memory, then the information of these classes are stored in the memory of which area? When we create an instance of an object, the virtual machine allocates memory for the object, and how does the Java VM match its own storage? These are all related to the memory partitioning mechanism of the Java Virtual machine, and today we will explore the memory model of the Java Virtual machine.

Java virtual machines in the course of executing a Java program will divide the memory it manages into several different data regions, each of which has its own purpose and time for creation and destruction, and some regions exist as the virtual machine process starts, and some regions depend on the user thread's start and end to build and destroy. According to the Java Virtual Machine specification, the memory managed by the Java Virtual machine includes the following data regions. As shown in the following:

The above is the partition of the data region of the Java Virtual runtime, each piece of memory area has its responsibility, and holds different runtime data.

Virtual Machine Stack

Java Virtual machine stack is thread-private, each thread in this area has a piece of memory area, its life cycle is the same as the thread, with the thread started, with the thread die out. The virtual machine stack describes the memory model of the Java method execution, each of which corresponds to a stack data structure in the virtual machine stack area, because the method call chain of a thread can be very long, each method will create a stack frame at execution time, and the stack frame is the stack element of the stack data structure of the thread corresponding. Stack frames are used to store information such as local variable tables, operand stacks, dynamic links, and so on. The local variable table holds the local variables defined within the method parameters and methods, including information such as the various basic data types and object reference types. Often hear some program ape rough to divide the virtual machine memory into heap memory and stack memory, this partition can only show that most of the program apes are concerned about, and the object memory allocation is most closely related to the two areas of memory, where the "stack memory" is referred to as the virtual machine stack, And in the virtual machine stack, our program Ape is most concerned about the local variable table part.

Local Method Stack

The local method stack is also thread-private, similar to what the virtual machine stack does, and the difference between them is that the virtual machine stack executes the Java method service for the virtual machine, while the local method stack serves the native method (local method) used by the virtual machine. In the Java Virtual Machine specification, the implementation of the local method stack is not mandatory, and some virtual machines even directly merge the virtual machine stack with the local method stack.

Heap

The Java heap is a chunk of memory shared by all threads and the largest in memory managed by a Java virtual machine. The only purpose of this memory is to hold object instances where almost all of the object instances are allocated memory. Java heap is the main area of garbage collector management, from the point of view of memory recycling, because the collector basically uses the Generational collection algorithm, so the Java heap can be subdivided into the new generation and the old age. The Cenozoic can also be subdivided into Eden, Fromsurvivor, and Tosurvivor regions. No matter how it is divided, it has nothing to do with what is stored, and the storage is still an object instance. The purpose of further partitioning is to better recycle or allocate memory more quickly.

Method Area

The method area, like the Java heap, is a thread-shared area of memory that stores data such as class information, constants, static variables, compiler-compiled code, and so on, that have been loaded by the virtual machine. A method area is a logical region of which physical memory depends on the implementation of a different virtual machine. In the implementation of the hotspot, the method area is logically isolated from heap memory, and the physical storage is part of the Java heap. Many people call the method area "the permanent generation", in fact, the hotspot uses the permanent generation to realize the method area. Other virtual machine implementations do not have a permanent generation of this concept. The Java Virtual Machine specification has a very loose limit on the method area, and you can choose not to implement garbage collection, except that you do not need contiguous memory and can choose a fixed size or extensible, as with the Java heap. In general, garbage collection is not done in the method area, and the effect of recycling in this area is difficult to satisfy. A memory overflow exception is thrown when the method area does not meet the memory allocation requirements.

The run-time-constant pool is part of the method area that holds the various literal and symbolic references generated during compilation, which are stored in the run-time pool of the class load backward into the method area.

Program Counter

A program counter is a small amount of memory space, which is also thread-private. It can be thought of as the line number indicator of the byte code executed by the current thread, and by changing the value of this counter to select the next line of bytecode instructions to execute. Because the multithreading of a Java Virtual machine is implemented in a way that threads take turns switching and allocating processor execution time, at any given moment, a processor core executes instructions in only one thread, so in order for the thread to switch back to the correct execution position, Each thread needs to have a separate program counter that is independent of each thread and stored independently.

The above is the memory model of the Java Virtual Machine Division, this is our program ape must master the principle of understanding the Java Virtual Machine memory model, is to understand the virtual machine memory allocation and garbage collection basis, as a summary.

Java Virtual machine: A detailed description of the memory model

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.