Java class loading mechanism

Source: Internet
Author: User

Recently in the review of the Java Foundation, the Java runtime of the entire memory operation mechanism is curious, read a bit in-depth understanding of Java Virtual Machine advanced features and best practices, the entire JVM operation mechanism is still relatively complex. If you have time to get to know it later, briefly describe it at the level of understanding the program.


First, the memory area of Java at runtime



Tell me a little bit about each area.

Program counter: The line number indicator of the byte code executed by the current thread, loop, thread recovery and other basic functions rely on this counter to complete (thread private, each thread will have its own separate counter).

Local method Stack: similar to the Java Virtual machine stack, but the native method is executed.

Method Area (contains a run-time-constant pool): the area that each thread shares, which is used for data such as class information, constants, static variables, and instant compiler-compiled code that have been loaded by the virtual machine.

Java Virtual machine stack: Also thread-private, with the same life cycle as the thread each method executes will create a stack frame (which will be described in detail) structure is the stack structure, advanced and out.

Java heap: The largest chunk of memory managed by a thread-shared Java Virtual machine, where almost all object instances allocate memory and are the main areas of Java garbage collector management.

Run a constant pool: part of the method area mainly holds the class's version, fields, methods, interfaces, and other descriptive information and constants pools. Constant dominant in the class load in the run-time runtime, which holds the compilation period generated by a variety of literal and symbolic references.



Then there is the loading process of the class

The class loading time is usually

1.new when creating an instance

2. Call the static method of the class

3.class.forname ("");

..... (When the class is initialized, his parent class is initialized together)

The loading of a class is divided into three parts 1. Load

The ClassLoader reads the bytecode file into memory and creates a Java.lang.Class object

2. Connect (1) Verify (2) Prepare

Allocates memory for static field of class, sets system default initial value

(3) Analysis

To replace a symbol reference in a constant pool with a direct reference

3. Initialization (mainly for Staticfield initialization)

1 2

1 The last I value of the graph is 3

2 The last I value of the graph is 2

The assignment is based on the order, but the static code block in the 1 diagram can manipulate the I variable, not in the 2 figure, and the static code block assigns only I.






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.