Deep understanding of Java Virtual Machine notes (i)-java memory area and memory overflow

Source: Internet
Author: User

1. Preface

This is a deep understanding of the Java Virtual Machine book, from the second chapter of the note. Because this book is more esoteric, this is the second time to look, need to record notes.

2. Run-time data region

The memory managed by the Java Virtual machine is divided into the following areas.

PS: Pictures from the web

2.1 Program Counters

The 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. The bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, and the basic functions such as the fraction, loop, jump, exception handling, and thread recovery need to rely on this counter to complete.
This piece of memory is thread-private memory.
If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed, and if it is the native method, the counter is empty, and this memory area is the area of the Java Virtual Machine specification where oom is not present.

2.2 Java Virtual Machine stack

is also thread-private, the virtual machine stack describes the Java method execution of the memory model, each method executes at the same time will create a stack frame to store the local variable table, operand stack, dynamic link, method exit and other information, not a method from the call to execute the direct knife to complete the process, Corresponds to a stack frame in the virtual machine stack into the stack and out of the process.
Local variable tables are stored to the compiler to know the various basic data types, object reference types, different from the object itself, may be a reference pointer to the start address of the object, or it may point to a handle representing the object or other objects related to the location.

2.3 Local Method Stack

The local method stack is the native method service used by the virtual machine.

2.4 Java Heap

is the largest chunk of memory in a virtual machine, shared by threads, where almost all object instances are allocated memory, and all object instances and arrays are allocated on the heap.

2.5 Method Area

Thread sharing, which stores data such as the class information that has been loaded by the virtual machine, constants, static variables, even after the compiler compiles the code.

2.6 Running a constant-rate pool

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

2.7 Direct Memory

is not part of the virtual machine, such as NIO and so on.

3. Creation of objects

Virtual opportunity to a new directive, to check whether the parameters of this directive can be located in the constant pool of a class symbol reference, and check whether the symbol refers to the class is loaded, parsed and initialized, if not, the process of executing class loading, after passing, the virtual machine to allocate memory for the new object, The required memory is fully determined after the class has been loaded. After the memory allocation is complete, the virtual machine makes the necessary set values for the object.

Pointer collisions:

Assume that the memory is absolutely complete, the middle place a pointer to distinguish between use and free memory, when allocating objects, the pointer moves and the object size of the same piece of area, called the pointer collision.

Free list:

If it is incomplete, the virtual machine needs to record the memory available, the block of memory is unavailable, allocate memory from the available list to find a large enough memory allocation.

4. Access to the object is determined as
    • Handle
    • Direct pointer
5. Memory Overflow
    • Java Heap Overflow
    • Virtual machine stack and local method stack Overflow
    • Method area run constant pool overflow
    • Native Direct Memory Overflow

Deep understanding of Java Virtual Machine notes (i)-java memory area and memory overflow

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.