Class files and JVM exact revenge

Load opportunity class

Now we are two examples generated .class file will be loaded directly into the JVM it? ?

Virtual Machine specification are strict rules and only five cases classes must be carried out immediately, "initialize" (load class file into the JVM):

  • Create an instance of the class (new way). Access static variables of a class or interface, or assignment of the static variables, static method call class
  • Reflectively
  • Initialize the subclass of a class, the parent class will be initialized
  • Java virtual machine when the class is marked as the start classes that directly use the java.exe command to run a master class (the class that contains the main method)
  • When using the dynamic language support JDK1.7 (....)

so:

  • Loaded Java class is dynamic, and it will not be a one-time class will all fully loaded before running, but to ensure the base class running (such as the base class) fully loaded into the jvm, the other class, need when it is loaded. This of course, is to save memory overhead.

How class loading to jvm

class file is loaded into the jvm by loading a class!

By default there are three types of Java loader:

Class files and JVM exact revenge

 

Each loader work responsibilities:

  • 1) Bootstrap ClassLoader: $ JAVA_HOME responsible for loading in jre / lib / rt.jar in every class, implemented by C ++, not ClassLoader subclass
  • 2) Extension ClassLoader: responsible for loading the java platform expansion pack features some of the jar, the jar package including / * under $ JAVA_HOME in jre / lib jar or -Djava.ext.dirs specified directory.
  • 3) App ClassLoader: in charge of records specified in the classpath jar package and the directory class

work process:

  • 1, when AppClassLoader load a class, it first does not own to try to load this class, but the class loader delegates to the parent class loader ExtClassLoader to complete.
  • 2, when ExtClassLoader load a class, it first does not own to try to load this class, but the class loader delegates to BootStrapClassLoader to complete.
  • 3, if BootStrapClassLoader fails to load (eg $ JAVA_HOME / jre / lib is not found in this class), will be used to try to load ExtClassLoader;
  • 4, if ExtClassLoader also fails to load, will be used to load AppClassLoader
  • 5, if AppClassLoader also fails to load, it will report the abnormal ClassNotFoundException

In fact, this is called the parent delegation model. In simple terms: If a class loader loads the class received a request, it first does not own to try to load this class, but the request is delegated to the parent loader to complete, turn up.

benefit:

  • Multiple copies of the same byte code (security point) prevent memory

Special Note:

  • After a successful class loader to load a class, instance of class java.lang.Class will get cached. Next time a request to load the class when the class loader will be used directly cached instance of the class, but do not try to load again.

Detailed class loading process

Loader loaded into the jvm, then in fact divided into several steps:

  • Load, find and load the binary data type, but also create an object of class java.lang.Class in the Java heap.
  • Connection, the connection also contains three elements: verification, preparation, initialization.
  • 1) Verify file format metadata, byte code, verification of symbolic references;
  • 2) Preparation, allocate memory for the class's static variable, and initialized to default values;
  • 3) parsing the class is converted into direct reference symbol references
  • Initialization, given the correct initial value of a static variable class.
Class files and JVM exact revenge

 

JIT immediate Editor

Generally, we might think: JVM loaded these files after class for these byte codes, one by one out, executed one by one -> parser.

But if that is the case, it is too slow!

Our JVM is implemented as follows:

  • These Java byte code is to recompile optimized machine code generated, so that CPU executed directly. This series out code will be more efficient.
  • Compiler also takes time, and we generally do compile the code on a hot, non-hot code directly resolved just fine.

Hot Code explanation: a method called multiple times. Second, the body of the loop is executed many times

Hotspots probe to detect whether the code is hot, hot spot detection in two ways:

  • sampling
  • counter

HotSpot is currently used in counter mode, it prepared two types of counters for each method:

  • Method calls counter (Invocation Counter)
  • Back side counter (Back EdgeCounter).
  • In determining the parameters of the virtual machines running under the premise of these two counters have a determined threshold, when the counter exceeds the threshold value overflows, it will trigger the JIT compiler.
Class files and JVM exact revenge

 

Back to the example

According to our program to go, our Java3yTest.class file will be AppClassLoader loader (because ExtClassLoader and BootStrap loader will not load it [parent delegation model]) is loaded into the JVM.

Subsequently found to be used Java3y this class, we will be AppClassLoader Java3y.class file loader (because ExtClassLoader and BootStrap loader will not load it [parent delegation model]) is loaded into the JVM

 

 

 

At last

If java micro-services, distributed, high concurrency, high availability, large-scale Internet infrastructure technology, exchange of experience in the interview. Interested can follow me oh!

Xiao Bian also share some information to you, to learn useful to enhance the role, there are about distributed micro-service, performance optimization, Spring, recorded video as well as spring MyBatis and other source of knowledge, jvm and so face questions, We hope to be able to help you!

 

 

 

 Needs can add my JAVA exchange group to receive Oh!

772 300 343 main group will take the initiative to give you da!

I was a small frame, we see the next article!

Guess you like

Origin www.cnblogs.com/sevencutekk/p/11527937.html