Partitioning and exceptions for Java memory regions

Source: Internet
Author: User

Partitioning of Java memory regions and exception run-time data regions

The JVM divides the memory into a number of different data regions when running Java programs.

Program counter

Thread Private. Can be thought of as the line number indicator of the byte code executed by the current thread , and the bytecode interpreter's work is to read the next byte-code instruction to be executed by changing the value of the meter.

Multithreading is done by rotating threads and allocating processor execution time, and at any one time, a kernel can only execute instructions in one thread. in order for the thread to switch back to the correct execution location, each thread needs a separate program counter . This is the beginning of the "thread-private". If the method that the thread is executing is a Java method, the counter records the instruction address of the virtual machine bytecode, and if it is the native method, the counter value is empty. The program counter is the only area in the Java Virtual Machine specification that does not specify an oom (outofmemoryerror) condition .

Java Virtual Machine stack

Thread private, life cycle and thread are the same. The Java Virtual machine stack describes the memory model of the Java method: Each method creates a stack frame at execution time, stores the local variable table, the operand stack, the dynamic link, the method exit information , each method from the call to the end, corresponding to this stack frame in the virtual machine stack in the stack and out of the stack process. The local variable table holds a variety of basic data types (int, double, char, Byte, and so on)
), the object reference (not the object itself), and the ReturnAddress type (pointing to a bytecode address).

There are two types of anomalies that can occur in this part of the area:

    • The stack depth of a thread request is greater than the depth allowed by the virtual machine, throwing stackoverflowerror;
    • When the virtual machine stack expands, it cannot request enough memory to throw OutOfMemoryError.
Local method Stack

The above virtual machine stack executes the Java method service for the JVM, and the local method executes the native service. Other and virtual machine stacks are similar and will also throw Stackoverflowerror, OutOfMemoryError.

Java heap

Often referred to as "stack Memory", "Heap memory", the former refers to the virtual machine stack, the latter is said Java heap. the Java heap is shared by threads . Created when the virtual machine is started.

the role of the Java heap is to hold object instances , and the Java heap can be in a physically discontinuous memory space, requiring only logical succession.

Method area

The zone to which the thread is shared. Stores data such as class information, constants, static variables, even compiler-compiled code, that have been loaded by the virtual machine. The OutOfMemoryError is thrown when the method area fails to meet the memory allocation requirements.

Run a constant-rate pool

Running a constant pool is part of the method area . C is used to hold the various literal constants and symbolic references generated during the compilation period, and will be stored in the run-time pool of the class load backward into the method area. The Java language does not require constants to be generated at compile time only, in other words, new constants can be placed during runtime .

Direct Memory

Direct memory is not part of the data area of the virtual runtime, nor is it a memory area. The allocation of native direct memory is not limited by the size of the Java heap, but eventually it is memory, and if the sum of each memory region is greater than the physical memory limit, OutOfMemoryError will appear.

The creation process of an object

Virtual opportunity to a "new" directive:

    1. First, check whether the parameters of this directive can be used to locate the symbolic reference of a class in a constant pool;
    2. Check whether the class represented by this symbol reference has been loaded, parsed, initialized, or, if not, the class must be loaded first
    3. allocating memory to the new object in the Java heap, the desired size is determined after the class is loaded;
    4. Initialize the allocated memory space to 0 (excluding object headers)
    5. Class initialization, which is the Init method, the object is initialized to the desired value according to the programmer's wishes.
Memory layout of the object

The layout of objects stored in memory can be divided into 3 areas:

    • Object Header
    • Instance data
    • Align to add

Object Header: Stores the runtime data of the object itself, such as hash code, GC generational age, lock status flag, thread-held lock, biased thread ID, and so on. Another part is the type pointer, the pointer to its class metadata, through which the virtual machine determines which instance of the class the object belongs to.

Instance data: Object really valid information, defined in the program of various types of field content;

Alignment Supplement: Non-mandatory, the role of the occupier.

Access positioning of objects

Java programs manipulate instance objects on the heap through references on the stack. Like what

newPerson();

Here p is the reference, and the new person object is an instance.

This reference does not specify how you want to locate or access the object's location in the heap. There are two ways to access the mainstream:

    • Handle. The Java heap divides a chunk of memory as a handle pool, referencing the address of the handle that stores the object, and the handle contains the object instance data and type data. The advantage is that when an object is moved, it simply changes the address in the handle, and the reference itself does not have to be modified.
    • Direct pointer. The object address is stored directly in the reference . The benefit is faster because the reference directly represents the address of the instance object, saving a pointer-to-position operation. This is the way Sun hotspot uses it.

by @sunhaiyu

2018.5.28

Partitioning and exceptions for Java memory regions

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.