Java Memory Area Detailed

Source: Internet
Author: User

Introduction

Java has been learning for some time, always feel that some things learned is not very proficient. Like what exactly is the Java memory area? How did the program run? How is the object stored? All of this has affected my familiarity with my own code.

A run-time data region

In the course of executing a Java program, the Java Virtual machine divides the memory it manages into several different data regions (a virtual machine is started whenever a Java program is run). One book, called the Java Virtual Machine specification, describes Sun's specification for Java Virtual machine implementations, in which the virtual machine divides the memory that is managed into the following sections:

    • Program counter

    • Virtual Machine Stack

    • Local Method Area

    • Heap

    • Method area

Where the method area and heap are shared by all threads, such as when multiple threads are created with Threadpoolexecutor, the heap and the method area can be read by multiple threads.

Program counter Learn the principle of computer principles of the people will know that in the CPU registers have a PC register, the next instruction address, here, the virtual machine does not use the CPU program counters, the memory of their own set up an area to simulate the CPU program counter. Only one program counter is not enough, when multiple threads switch execution, there is no way for a single program counter, the virtual machine specification indicates that each thread has a separate program counter. Note that the program counter in the Java Virtual machine points to the byte-code address that is being executed, not the next one.

is thread-private, and its life cycle is the same as the thread. The virtual machine stack describes the memory model that is executed by the Java method: Each method executes with a stack frame (which I think can be viewed as a snapshot, recording some of the parameters before entering the method, actually the underlying data structure of the method runtime), for storing the local variable table, the operand stack, the dynamic link, Method exports and other information. Each method from the invocation until the completion of the process corresponds to a stack frame in the virtual machine in the stack into the stack process. We usually divide the memory into heap memory and stack memory, where the stack memory refers to the local variable table portion of the virtual machine stack. The local variable table holds the basic data types that can be known at compile time, the object reference, and the address of the bytecode that is pointed to after the return.

The local method area is similar to the virtual machine stack, but note that the methods in the local method area are not mandatory in the virtual machine specification, and the virtual machine can be freely implemented, that is, it can be not a byte code. But it can also be bytecode, so that the virtual machine stack and the local method area can be combined, in fact, the OPENJDK and SUNJDK brought the hotspot virtual machine directly into the virtual machine stack and the Local method zone.

Heap This concept should be familiar to a lot of people, such as the beginning of the C language, the teacher will say the malloc method will allocate space in the heap, here is the same. This area is used to hold object instances, where almost all object instances allocate memory, and the virtual machine specification says that all instances of objects and arrays are allocated on the heap. But with the development of JIT (JUST-IN-TIME) compilation period, sometimes it is possible to allocate on the stack (I do not quite understand the truth here). Heaps are the main areas of Java garbage collector management (often referred to as GC heaps, not garbage dumps), and the garbage collector implements automatic destruction of objects.

The method area is also the area shared by each thread, which is used to store data such as class information, constants, static variables, and compiled code (class methods) that have been loaded by the virtual machine. Let's talk about the run-time constant pool, which is part of the method area that holds the various literal and symbolic references generated during the compilation period (in fact, eight basic types of wrapper types and string type data).

Finally, there is a direct memory, which is included in the JDK1.4 version of the NIO class, which introduces the I/O mode based on channel and buffer, which means that in this way, memory is not allocated in the run-time data region, thus avoiding The run-time data region replicates data back and forth, calling external memory directly.


Creation of two objects

In the case of an object-oriented language, we do not create objects through the new keyword at all, so what is the process?

When a virtual opportunity comes to a new command, it first checks to see if the new class has been loaded, and where is it checked? Of course, in the method area, the method area stores the loaded class information. If it is not loaded, then the class load is performed first.

After a class load check, the virtual machine begins to allocate memory for the new object, and the memory size required by the object is determined after the class is loaded, as long as the space is allocated in the heap. There are two ways to allocate memory, the first, we assume that the memory is absolutely regular, so long as the memory and unused memory to place a pointer, each time you allocate space when the pointer to the free space to move the corresponding distance. Second, we assume that free memory and non-free memory are mixed together, which is actually the case, then a list is needed to record the usage of the heap memory, which is how the operating system manages memory.

So, we also have to consider a problem, that is, in the case of multithreading, only one pointer how to ensure that a thread allocated memory pointer is not modified when another thread allocated memory will not overwrite the previous memory? Here's a way for each thread to pre-allocate a small chunk of memory (tlab local thread allocation buffers) in the heap, and each thread allocates memory only in its own memory.

Finally, the object is successfully allocated memory. We know through an object, we can get the class by the GetClass () method, the default comparison of two objects actually compares the object memory hash value, how is this implemented? In fact, after allocating memory, the virtual opportunity to make the necessary settings for the object, the object's class, the object's hash code and other information are stored in the object's object header, so the allocated memory size is never the sum of attributes.


Memory layout for three objects

The layout of the object in the heap is divided into three regions: object header, instance data, and alignment padding.

The object header consists of two parts, the first part is used to store the data of its own runtime such as hash code, lock state, which thread can have. The second part holds the class metadata that points to the method area.

The instance data holds the property information for the class, including the parent class's property information.

Align padding This is the virtual machine requires that the object start address must be an integer multiple of 8 bytes, which can be said to have no special meaning for the aligned padding.


Access positioning for four objects

We know that a variable reference is a variable reference, and the specific object is a concrete object. The variable reference is stored in the virtual machine stack, the data type is reference, and the object is stored in the heap. So how does a reference point to an object instance?

There are two main ways to access it, the first is through a handle pool, and if you use a handle pool, the Java heap will divide a portion of memory as a handle pool, with the handle containing the class metadata of the object type pointer to the method area, as well as the object instance pointer, pointing to the instance address in the heap.

The second is that the reference reference directly points to an object instance in the heap, and the object header of the object instance holds the object type pointer.

There are advantages to both approaches, the first of which is to change the object instance pointer in the handle pool only when the object instance is moved, without changing the reference reference itself. The second method is fast, reducing the time overhead of a pointer positioning. The second way the hotspot virtual machine is currently in use.


Summarize

Understanding the Java memory Area is an in-depth study of Java, previously only known to have heaps and stacks of distinction, and now we understand the role of concrete stacks. How memory is divided, how the object is stored, and how the methods and attributes are stored. through the understanding of these content, we will write Java programs more skilled, targeted.

Java Memory Area Detailed

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.