Deep understanding of the Java Virtual Machine family (i): Java memory area and memory overflow exception

Source: Internet
Author: User

The main article is read the deep understanding of Java Virtual Machine: JVM advanced Features and Best Practices Chapter II: Java memory area and memory overflow exception

Some of the notes and summaries.


Okay, here we go. If there are any errors or omissions, please point out.


I. Overview

First, a picture.


This diagram mainly lists several areas of memory managed by the Java Virtual machine.

Java memory is often divided into heap memory (heap) and stack memory (stack), this division is rough, the division of Java memory area is actually far more complex than this, can be seen from. The "stack" referred to in the stack is really just a virtual machine stack, or a local variable table part of a virtual machine stack. The next section focuses on the sections of the area.


Second, run-time data area


1. Program counter

    • The program Counter Register is a small memory space that acts as a line number indicator of the bytecode that is being executed by the current thread. In the virtual machine conceptual model (only the conceptual model, a variety of virtual machines may be implemented in some more efficient way), the bytecode interpreter works by changing the value of this counter to select the next byte code command to execute
    • The multithreading of a Java Virtual machine is implemented in a way that threads rotate and allocate processor execution time, and at any given moment, a processor (a kernel for a multicore processor) executes only the instructions in one thread. Therefore, in order to recover the thread after switching to the correct execution location, each thread needs to have a separate program counter, the counters between the threads do not affect each other, isolated storage, we call this type of memory area is "thread-private" memory.
    • The thread is executing a Java method, and if the Natvie method is being executed, the counter value is empty (Undefined). This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.


2.Java Virtual Machine Stack

Stack frame is the base data structure stack capacity of the method run time can be set by the-XSS parameter

    • Like the program counter, the Java Virtual machine stack (Java Stacks) is also thread-private, with the same life cycle as the thread. 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.
    • Java memory is often divided into heap memory (heap) and stack memory (stack), this division is rough, the division of Java memory area is actually far more complex than this. The "heap" referred to in this section is described later, and the "stack" refers to the current virtual machine stack, or the local variable table portion of the virtual machine stack.
    • 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. It mainly contains various basic data types, object references (reference types), ReturnAddress types, which are known in the compilation period.


3. Local Method Stack

Stack capacity can be set by-XSS parameter

The virtual machine stack executes Java methods (that is, bytecode) services for the virtual machine, while the local method stack is the native method service used by the virtual machine. Some virtual machines, such as sun hotspot virtual machines, directly combine the local method stack with the virtual machine stack.


4.Java Heap

parameter-xms and-XMX settings available .

The Java heap (Java heap) is the largest piece of memory managed by a Java virtual machine. The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. The only purpose of this area of memory is to hold object instances where almost all of the object instances are allocated memory.


5. Method area

Parameter-xx:maxpermsize can be set.

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. Although the Java Virtual Machine specification describes the method area as a logical part of the heap, it has an alias called Non-heap (Not a heap), which should be distinguished from the Java heap.


6. Run a constant-rate pool

Can be set via-xx:permsize and-xx:maxpermsize

    • The runtime Constant pool is part of the method area. In addition to information such as the version of the class, fields, methods, interfaces, and so on, the class file contains a constant pool (Constant pool Table) that holds the various literal and symbolic references generated during the compilation period, which are stored in the run-time pool of the method area after the class is loaded.
    • 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 at compile time, and new constants may be put into the pool during runtime, a feature that is used by developers more than intern () of the String class. Method.


7. Direct Memory

Can be specified by-xx:maxdirectmemorysize, if not specified, the default is the same as the maximum value of the Java heap (-xmx specified)

Direct memory is not part of the data area when the virtual machine is running, nor is it a memory area defined in the Java VM specification, but this part of memory is also used frequently and can cause outofmemoryerror anomalies to occur.


Third, hotspot virtual machine Object Quest

This paper focuses on the whole process of object assignment, layout and access in the Java heap of the hotspot virtual machine.


1. Creation of objects

Virtual opportunity to the new directive,

    • First check to see if the parameter of this directive can be used to locate a symbol reference for a class in a constant pool, and to check that the class referenced by the reference has been loaded, parsed, and initialized. If not, the class loading process is performed (chapter 7th).
    • After the load check passes, allocate memory (in the presence of class loading complete can be fully determined).
    • After the memory allocation is complete, the virtual machine makes the necessary settings for the object, such as an instance of which class the object is, how to find the metadata information for the class, and so on (all in the object header of the object).
    • From the virtual machine perspective, a new object has been created, but from the Java program perspective, object creation has just begun, because the <init> method has not been executed, and all fields are zero. After executing the new instruction, the <init> method (construction method) is followed and initialized so that a truly usable object is completed.


2. Memory layout of objects

Divided into the object header, the instance data, the alignment fills three parts.

1) Object Header

Contains two parts

  • Stores the runtime data of the object itself, such as hash code, GC generational age, and so on. 32-bit and 64-bit virtual machines, 32bit, 64bit, respectively, officially called "Mark Word"
  • A pointer to the type of the object that points to its class metadata, which the virtual machine uses to determine which class the object is an instance of.

Note: If the object is a Java array, the object header must also have a piece of data that records the length of the data.

2) Instance data

Objects are really useful information to store, and are also the various types of field content defined in the program.

3) Align the fill

Since the hotspot virtual machine requires that the starting address of the object must be an integer multiple of 8 bytes, in layman's words, the object size must be an integer multiple of 8 bytes. The object header is exactly a multiple of 8 bytes. When the instance data part is not aligned, you need to complement it by aligning the fill.


3. object access and positioning

Object access is ubiquitous in the Java language and is the most common program behavior, but even the simplest accesses involve the association between the three most important memory areas of the Java stack, the Java heap, and the method area, as in the following code:
Object obj = new Object ();
Assuming that this code appears in the method body, the semantics of the part "Object obj" will be reflected in the local variable table of the Java stack as a reference type data.

The semantics of the section "New Object ()" will be reflected in the Java heap, forming a piece of structured memory that stores all instance data values of type object (Instance data, each instance field in the object)
Because the reference type only specifies a reference to the object in the Java Virtual Machine specification, and does not define the way in which the reference should be located, and the specific location of the objects in the Java heap, the object access methods implemented by different virtual machines will vary. There are two ways to access the mainstream: using handles and direct pointers.



Iv. Summary

The last one is the schematic of this chapter:



In-depth understanding of the Java Virtual Machine family (i): Java memory area and memory overflow exception

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.