Java Memory Architecture

Source: Internet
Author: User

a) implementation.

Main () serves as the starting point for the program's initial line. Whether it is started by a thread on another thread.

The JVM has two internal threads: a daemon thread and a non-daemon thread, and main () is a non-daemon thread. Often used by the JVM itself. Java programs can also indicate that the thread they created is a daemon thread

b) extinction. The JVM exits when all non-daemon threads in the program terminate, and the program can use the runtime class or System.exit () to exit if the security Manager agrees

2. The JVM execution engine instance is corresponding to the thread level of the user executing the program.

First, the architecture of the JVM

1. Class loader (ClassLoader) (Used to load. class files)

2. Run the engine (run the bytecode, or run the local method)

3. Execution-Time Data area (method area, Heap, Java stack, PC register, local method stack)

Second, JVM class loader

Steps for the entire class loading process of the JVM:

1. Loading

The loading process is responsible for locating the binary bytecode and loading it into the JVM, which is the same as when the class is loaded through the ClassLoader class name, the package name where the class resides. The above three elements are also used to identify a loaded class: class name +

Package name +classloader Instance ID.

2. Links

The link process is responsible for verifying the format of the binary bytecode, initializing the static variables in the load class, and parsing the interfaces and classes called in the class.

After the checksum is complete, the JVM initializes the static variables in the class. and assigns its value to the default value.

Finally, all the properties and methods in the class are validated to ensure that they need to invoke the properties, methods exist, and have the permissions (such as public, private domain permissions, etc.). Will cause nosuchmethoderror, Nosuchfielderror and other error messages.

3. Initialization

The initialization process is the initialization of static initialization code, constructor code, and static properties in the running class, and in four cases the initialization process is triggered:

New was called;

Reflection invokes a method in a class.

The subclass invokes initialization;

The initialization class specified during the JVM startup process.

JVM Class Load Order:

The JVM two kinds of loaders contain: start class loaders and user-defined class loaders.

The Startup class loader is part of the JVM implementation;

A user-defined class loader is part of a Java program and must be a subclass of the ClassLoader class.

JVM Load Order:

When the JVM starts, the class is loaded into the user-defined direction by Bootstrap;

When the application is ClassLoader, the class is found and loaded by user-defined to bootstrap direction;

1. Bootstrap ClassLoader

This is the root classloader of the JVM. It is implemented in C + +. This classloader is initialized when the JVM starts. This classloader the loading of all class files in the $java_home Jre/lib/rt.jar (Implementation of the Sun JDK), which includes all the interfaces and implementations of the JAVA specification definition.

2. Extension ClassLoader

The JVM uses this classloader to load some jar packages that extend the functionality.

3. System ClassLoader

The JVM uses this classloader to load the jar packages and folders in the Classpath specified in the startup parameters, ClassLoader the corresponding class name Appclassloader in the Sun jdk.

4. user-defined ClassLoader

User-definedclassloader is a Java developer's own implementation of the ClassLoader abstract class ClassLoader, which can be used to load jars and folders in non-classpath based on their own defined ClassLoader.

Several key methods of ClassLoader abstract classes:

(1) LoadClass

This method is responsible for loading the class of the specified name, and ClassLoader is implemented by first looking from the already loaded class, and then continuing to look for it from the parent ClassLoader if none. If still not found. It is searched from the Systemclassloader, and then the Findclass method is called to look for, if you want to change the load order of the class, you can override this method

(2) Findloadedclass

This method is responsible for finding the loaded class from the cache of the current ClassLoader instance object. The method that is called for native.

(3) Findclass

This method throws classnotfoundexception directly, so you need to load the corresponding class in your own way by overwriting loadclass or this method.

(4) Findsystemclass

This method is responsible for finding the class from System ClassLoader, if it is not found, continues to look from the Bootstrapclassloader, and returns null if it is still found.

(5) DefineClass

This method is responsible for converting the binary bytecode to the class object

(6) Resolveclass

This method is responsible for completing the link to the class object. If it has been linked. will be returned directly.

Third, JVM running engine

The JVM provides four commands to run when the method is run:

(1) Invokestatic: Calling the static method of a class

(2) Invokevirtual: Method of invoking an object instance

(3) Invokeinterface: Defines a property as an interface for invocation

(4) INVOKESPECIAL:JVM for initializing objects (the Java constructor's method is:<init>) and for invoking private methods in the object instance.

The basic operating techniques are:

Explain. Instant compilation, adaptive optimization, chip-level direct operation

(1) explanation belongs to the first generation JVM,

(2) JIT compilation is a second generation JVM,

(3) Adaptive optimization (currently used by Sun's HOTSPOTJVM) draws on the first generation of JVMs and second generation

JVM experience, in a way that combines the two

Start by taking an explanation of how the entire code is running, and monitoring the operation of the code. A background thread is then started for those methods that are often called. Compile it into local code and optimize it. If the method is no longer used frequently, the compiled code is canceled, and it is still interpreted to run.

Iv. JVM Execution Time data area

First Block: PC Register

The PC register is the JVM instruction that is used to store each thread next to run, and if the method is native, no information is stored in the PC register.

Second block: JVM stack

The JVM stack is thread-private. The JVM stack is created at the same time each thread is created, and the JVM stack is stored as a variable of the local primitive type in the current thread (eight basic types defined in Java: Boolean, Char, Byte, short, int, long, float, double), Part of the return result as well as the stack Frame. Objects of non-basic types hold only one point on the JVM stack to the address on the heap

Third Block: Heap

It is JVM the area used to store object instances and array values. You can feel the memory of all objects in Java that are created through new are allocated here. The memory of objects in the Heap needs to wait for GC to be reclaimed.

(1) The heap is shared by all threads in the JVM, so the allocation of object memory on it requires locking. This also causes the new object's overhead to be relatively large.

(2) The Sun Hotspot JVM is designed to increase the efficiency of object memory allocation. A separate space Tlab (thread Local allocationbuffer) is allocated for the created thread, and the size is calculated by the JVM on the basis of execution, and no lock is required when allocating objects on Tlab. Therefore, the JVM allocates memory to the thread's objects as much as possible on the Tlab. In such cases, the performance of allocating object memory in the JVM is essentially as efficient as C. But assuming that the object is too large, it still uses the heap space allocation directly.

(3) Tlab only works on the new generation of Eden Space, so when writing Java programs, it is often more efficient to allocate smaller objects than large objects.

Block Four: Method area

(1) in the Sun JDK, this area corresponds to Permanetgeneration, also known as the persistent generation.

(2) The method area holds information about the class that is loaded (name, modifier, and so on), the static variable in the class, the constants defined in the class as the final type, the field information in the class, and the method information in the class. When the developer passes the class in the program

Objects such as GetName, Isinterface and other methods to obtain information, the data are derived from the method area, the same time the method area is also globally shared, under certain conditions it will also be GC. The OutOfMemory error message is thrown when the method area is required to use more memory than its agreed size.

Block fifth: Execution of constant-time pools (Runtime Constant Pool)

stored as fixed constant information, methods, and field reference information for the class. Its space is allocated from the method area.

Block Sixth: Local method Stack (Native Stacks)

The JVM uses the local method stack to support the operation of the native method, which is used to store the state of each native method call.

V. JVM Garbage Collection

GC Rationale: reclaims objects that are no longer in use in memory. The method used for recycling in GC is called a collector because the GC consumes some resources and time. Java after analyzing the life cycle characteristics of an object. Collection of objects according to the new generation and generation of generations to minimize the pauses caused by GC corresponding use

(1) The collection of new generation objects is called minor GC;

(2) The collection of objects of the old generation is called full GC;

(3) The GC for active invocation of System.GC () in the program is full GC.

Different object reference types, the GC will be recycled in different ways. A reference to a JVM object is divided into four types:

(1) Strong references: by default. objects are strongly referenced (instances of this object have no other object references, and GC is recycled)

(2) Soft reference: Soft reference is an application available in Java that is suitable for caching scenarios (only if the memory is not enough to be GC)

(3) Weak reference: GC recovery is guaranteed during GC

(4) Virtual reference: Is it because the virtual reference object is just to learn the GC

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Java Memory Architecture

Related Article

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.