Two GC-related Java memory model

Source: Internet
Author: User

Based on the problem described in the previous section, we know that the final cause is GC-led. In this section, we'll explore the Java memory model that is relevant to the GC in detail.

Noun interpretation: variable, understood as the basic type of java. object, which is understood as an instance of Java new.

The Java program runs in the JRE (Java Runtime Environment) and the JRE includes the Java API and the JVM (Java Virtual machine).

The Java original file is compiled with a Java byte code (. Class) file, and the JRE loads the Java byte code file into the JVM's Run-time data area via ClassLoader.

The Run-time data area of the JVM contains the PC Register, Java Virtual machine Stacks, Heap, Method area, Run-time Constant Pool, Native M Ethod Stacks.

The PC register: each thread has its own register. The main record is the method name, instruction address, returnaddress, current pointer, etc. that are being executed by each point-in-time thread. If you are currently executing Java native method (that is, Java's lowest-level approach), then the value of the PC Register for the front-thread is undefined.

Native method Stacks: This stack is used to execute methods that are not written in the Java language (that is, Native).

Heap: This space is shared by all the threads of the JVM. From this we can see that thread communication inside Java is done through shared memory. All of the instance objects and arrays are stored here. GC's collection of objects and the compression of space occur in this space.

Java Virtual Machine Stacks: When a thread is created, the JVM allocates a stack for this thread. The main storage thread is its own variables, partial results, references to shared memory, and so on. This space is allocated by the heap.

Method area: This space is allocated by the heap. is also shared by all the threads of all JVMs, primarily storing the code of the Run-time constant pool, method. This space is generally not a GC.

Run-time Constant Pool: This space is allocated by method area, which mainly stores constants, variables, and references.

Summary: 1. Diagram of the JVM run-time data area:

Examples of memory usage: When starting a class's main function (in fact, a process is started), the JVM's Run-time data area is allocated. The PC register records the name of this main function, the address of the instruction in the main function, etc., the global variables of this class are stored in the Run-time Constant Pool, the constructor of the class, the method body, and the variables in the method body. The JVM stack stores the local variables of this thread (main function body), the object's reference, the return result, the exception distribution, and so on; the instance of the main function body is stored in the heap.

Program execution process: the thread executes according to the instructions recorded by the PC register (for faster execution, these instructions are reordered, with CPU reordering and memory system reordering (Java Happen-before), which is supposed to be executed sequentially), This thread can read the local variables of the current JVM stack directly, read the global variables in the runtime constant pool, invoke the instance in the heap by reference to the object, and invoke method in the method area. Or Invoke method in the Native method area. it can be abstracted as the thread reads/writes the current thread's data and results through the heap.

Two GC-related Java memory model

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.