Java core: Class loading and JVM memory allocation

Source: Internet
Author: User
Tags field table

Classes are loaded:

  Refers to reading the binary data of a class file into the runtime data area (the JVM is partitioned in memory)

and creates a class object within the method area.

Class Loader:

Responsible for loading the compiled class file (bytecode file) into the JVM (Java Virtual machine).

The class loader is mainly divided into the following types:

1.Bootstrap class loader (boot class loader)

Responsible for loading the Java Core Class library. In the Jre\lib directory, including the Rt.jar (Java base Class library), these

Are the core class libraries of Java. And this loader is written in C, so in Java programs it gets

Less than that.

2.Extension class loader (extension ClassLoader)

The jar package that is responsible for loading the extended functionality under the Java platform, which is in the Jre\lib\ext directory. This load

Written by the Java language.

3.System class Loader (System loader)

Responsible for loading all class libraries under the Classpath directory, the class file under the Classpath directory is generally

We wrote the Java file after compiling it. This loader is written in the Java language.

These classloader work together to complete the loading process for the entire class, so the load patterns of these classes are based on

"Parental delegation Model".

" Parent-commissioned model":

After the program is run, the compiler compiles the Java file into a class file, and the first thing to load is the

The System class loader, but it does not load immediately, but instead transfers this task to its parent ClassLoader extension

The class loader loads, and the extension class loader also transfers this task to the boot ClassLoader for loading.

class file to the boot ClassLoader, it first determines if it can load the class, and if so,

Load, no, transfer to its child loader, and so on. Ultimately, the class we write will be configured in

Classpath environment, the class load task is still done by the system ClassLoader. If the system

The ClassLoader cannot be loaded, and the classnotfoundexception is thrown.

When a class is loaded into the JVM, the class loading phase is complete. The JVM then allocates memory to the entire

class file (inside the file is a binary assembly command) for content parsing (JVM-to-binary life

To perform line-by-row parsing to the CPU).

Memory allocation:

When the JVM is running, it divides the memory space, which is called the runtime data area.

The runtime data area is divided into the following pieces of content:

1. Stack:

  Each thread is run with a stack (line stacks), and the data stored in the stack is currently

Threads are exclusive (no resource sharing occurs, so threads are secure). And the stack is stored in the stack frame,

When a thread calls a method, it forms a stack frame and pushes the stack frame. When the method finishes executing,

Perform a stack operation. This stack frame includes (local variables, operand stacks, which refer to the current method's corresponding class

The method returns information such as an address).

2. Local method Stack:

The mechanism of the local method stack is similar to the stack, except that the stack is running the Java implementation method, while the local

The method stack runs on a local method. Local methods refer to methods implemented by the JVM to invoke a non-Java language.

For example, C language. In the JVM specification, there is no hardening requirement that the implementation party must partition the local method stack (for example:

The hotspot virtual machine will combine the local method stack with the stack) and implement the specific (different operating systems, the JVM

Specific implementations of the specification are different).

3. Program Counter:

Program counters can also be referred to as PC registers (popularly speaking is the instruction cache). It is primarily used to cache the current

The instruction address of the next instruction of the program, the CPU will find the command to be executed according to this address. This register is the JVM

The internal implementation is not a physical concept counter, but is the same as the JVM's implementation logic.

4. Heap:

Heap memory primarily holds objects and arrays created. The heap is unique within the JVM and can be shared by multiple threads.

Each object in the heap holds an instance variable of the instance.

When a local variable is defined in a method, if the variable is a basic data type, then the value of the variable is directly

stored in the stack, and if the variable is a reference data type, the value of the variable is stored in the heap memory, and the stack is stored in the

Point to the reference address in the heap.

5. Method Area:

The method area is also a very important piece of memory in the JVM, which, like a heap, can be shared more than one thread.

It mainly stores information for each load class. The class information mainly contains the magic number (determines whether it is a class file), the constant

Pool, Access flag (whether the current class is a normal class or an interface, if it is an abstract class, whether it is decorated with public, whether the final

Description information such as decoration ... ), the Field table collection information (using what access modifier, is an instance variable or a static variable, whether

Use descriptive information such as final decoration ..... ), Method table collection information (what access modifiers are used, whether static methods, whether

The final decoration was used, whether the synchronized decoration was used, whether it was a native method ... ) and other content. When a class adds

When the loader loads a class, it will create a class object based on the class file, and the class object will contain the above information.

The subsequent creation of instances of this class is created based on the class object.

6. Constant pool:

Chang is a part of the method area that holds the most important resources in a class object. The JVM maintains one for each class object

Constant pool. It mainly stores two types of constants:

1. Literal constants:

Literal constants are usually literal values defined in Java. For example: int = 1, in the 1,string s = "Hello", this

Hello is the literal amount. Or use the final decorated constant value.

2. Symbol References:

  Symbolic references mainly include the full class name of the class and interface, the name and descriptor of the property, the method name and the descriptor, and so on.

-----------------------------------------------------------

Java core: Class loading and JVM memory allocation

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.