In-depth understanding of automatic memory management for Java virtual machines

Source: Internet
Author: User

Java memory area and memory overflow exception 1. Run-time data region

Java virtual machines in the course of executing a Java program, HPE divides the memory it manages into several different data regions. As shown in the following:

(1). Program counter: A small memory space that can be viewed as the line number indicator of the bytecode that is executed when the front thread executes. (is the only area that will not be oom)

(2). Java Virtual stack: Store local variables, operand stacks, dynamic links, method exits and other information. Each method from the call to the completion of execution, corresponding to a stack frame in the virtual machine stack into the stack and the process of the stack.

(3). Local method Stack: similar to the virtual machine stack, except that the services performed by the virtual machine stack perform Java methods (that is, bytecode) service for the virtual machine, which is the native method service used by the virtual machine.

(4). Java Heap (also called GC heap): The largest piece of memory managed by a virtual machine, with the sole purpose of storing instance objects.

(5). Method Area: Store constants, static variables, and so on.

(6). Run a constant pool: part of the method area

(7). Direct Memory: A memory area that is neither part of the virtual machine nor defined in the Java Virtual Machine specification. It's going to be used when NIO.

2. HotSpot Virtual Machine Object

Learn more about the other details of the data in these virtual machine memory. Includes creating----layouts----access

(1). Creation of objects

At the language level, creating objects (for example, cloning, deserialization) is usually just a new keyword, whereas object creation in a virtual machine is much more complex, as shown in:

(2). Memory layout of object: three block area---- Object header, instance data, and alignment fill

    • Object Header

    ① is used to store the runtime data of the object itself: hash code, GC generational age, lock status flag, thread-held lock, biased thread ID, biased timestamp, and so on;

The ② object points to the type pointer of its class metadata (but not necessarily every object), and if the object is an array then the object header also includes a piece of data to record the length of the array;

    • instance Data : is the valid information that the object actually stores, and also is the field content of the type of the Gothic defined in the program code;
    • align fills : not necessarily present, nor have special meanings, just play the role of placeholder;

(3). Object Access positioning: Principle: Stack of reference data to manipulate concrete objects on the heap, there are two ways----handle and Direct pointer

    • Handle: A chunk of memory is partitioned in the Java heap as a handle pool, where the handle address of the object is stored in the reference, and the handle contains the specific address information of the object instance data and the type data, as shown in

    • Direct pointer: The layout of the Java heap object must consider how to place information about the access type data, and the reference is stored directly in the object address, as shown in

   

    • The difference between the two access: The handle: Reference record is the handle address, the object changes only need to change the handle of the instance data pointer, does not affect the reference point, more stable; direct pointer: direct access to the object, relative to the handle access to the middle of a lower handle pool, so faster, But overhead can add up and increase execution costs.
3. OutOfMemoryError exception

(1). Java heap overflow: Using Eclipse Memory Analyzer to analyze heap dump snapshot files, see Eclipse Memory Analyzer

(2). The virtual machine stack and the local method stack overflow:

① throws a Stackoverflowerror exception (memory leak) If the thread requests a stack depth that is greater than the maximum depth that the virtual machine is running

② throws a OutOfMemoryError exception (memory overflow) if the virtual machine cannot request enough memory space when it is extended

(3). Method area and run-time-constant pool overflow

(4). Native Direct Memory Overflow

4. Summary

1. How the memory in the virtual machine is divided

2. Causes of anomalies

  

In-depth understanding of automatic memory management for Java virtual machines

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.