当前位置:网站首页>Class loading process of JVM

Class loading process of JVM

2022-04-23 19:26:00 yltrcc

One sentence per day

When people come to love, love turns thin , Now it's really not affectionate .

One sentence per day

The frog in the well knows nothing of the great ocean.
able only to see the little patch of sky above , I don't know the sea .

JVM Class loading phase of

JVM  Class loading is divided into five stages :

1.  load : Read into memory by virtual machine

2.  verification : verification  Class  Whether the data of byte stream complies with JVM The provisions of the

3.  Get ready : Officially a class variable ( Static variables ) Allocate memory and set initial values , Not the value set in the code

4.  analysis : Resolve the symbolic reference in constant pool to direct reference

5.  initialization : Actually execute the... Defined in the class java Code

load

finger JVM Read class file , And according to Class File description creation java.lang.Class Object procedure .

The class loading process mainly includes the following steps Class The file is read into the method area of the runtime area , Create... In the heap java.lang.Class object , And encapsulates the data structure of the class in the method area .

In the reading Class Files can be read in the form of files , It can also be done through jar package 、war Package read , It can also be generated automatically by proxy Class Or other ways to read

verification

It is mainly used to ensure that  Class  The file meets the requirements of the current virtual machine , Protect the security of the virtual machine itself , Only those that have been verified  CLass  Documents can only be  JVM  load

Get ready

The main work is to allocate memory space for class variables in method area and set initial values of variables in class .
Initial values refer to the default values for different data types , Here we need to pay attention to  final  Variables of type and not final The data initialization process for variables of type is different in the preparation phase

For example, a member variable is defined as follows :
public static long value = 1000;
In the above code , Static variables  value  The initial value in the preparation phase is 0, take  value  Set to 1000  This is done when the object is initialized , because  JVM  In the compilation phase, the initialization of static variables is defined in the constructor .
public static final int value = 1000;

be JVM After the compile phase, it will be final Variable of type value Generate its corresponding ConstantValue attribute , The virtual machine will be based on ConstantValue Property will be value The assignment is 1000.

summary : Static variables will be given initial values twice , Assign zero value in the preparation stage , Assign user setting value during initialization , and final The variable will be assigned at one time in the preparation stage

analysis

JVM  The symbolic reference in the constant pool is replaced by a direct reference .

initialization

Mainly through the execution of class constructors <client>  Method to initialize the class .
<client>  The method is composed of the compiler automatically collecting static statements and variable assignment operations in the class in the compilation stage .JVM Regulations , Only in the parent class <client>  After successful execution of the methods , A subclass of <client>  Methods can be executed .
When there is neither static variable assignment nor static statement block in a class , The compiler does not generate for this class <client>  Method .

In the following situations ,JVM Class initialization process will not be executed :

1.  Constant values are stored in the constant pool of the class that uses the constant at compile time , The procedure does not need to call the class where the constant is located , therefore , The initialization of this constant class will not be triggered .

2.  When a subclass refers to a static field of a parent class , Does not trigger subclass initialization , Only the initialization of the parent class will be triggered .

3.  Defining an array of objects , The initialization of the parent class will not be triggered

4.  Using the class name to get  Class  Object does not trigger class initialization

5.  In the use of  Class.forName  When loading the specified class , Can pass  initialize  Parameter sets whether the class needs to be initialized

6.  In the use of ClassLoader default loadClass Method does not trigger the initialization of the class when it loads the class .

Beautiful sentences

A lot of times , The dilemma of things , It's often because we've drilled our own horns , here , All you need to do is change .

Perfectionists can let go of obsession , Allow yourself to have the little confusion that ordinary people will make ; Working mothers can face reality , One can never achieve double percent of family and career ; Employees who take on too many tasks , You can try to reflect to your superiors , Seek resources or adjust goals …… these , These are changes that we should and can make .

As the saying goes : There have never been so-called miracles in the world , Fate is always in our own hands . Want to change your destiny , The most important thing is to change yourself . When you start to change yourself , A lot of things changed .

The next time , When trouble comes , You might as well try to find problems from yourself . Adjust the direction and rhythm of efforts , Learn to untie the mind , You'll find that : A lot of things , It's no big deal .

Interview questions

SpringMVC What's the use of frames ?

Spring Web MVC Framework provide ” Model - View - controller ”( Model-View-Controller ) Architecture and ready to use components , For developing flexible and loosely coupled Web Applications .
MVC Patterns help separate different aspects of an application , For example, input logic , Business logic and UI Logic , At the same time, it provides loose coupling among all these elements .

@RestController and @Controller What's the difference? ?

@RestController annotation , stay @Controller On the basis of , Added @ResponseBody annotation , It is more suitable for the current architecture of separation of front end and back end , Provide Restful API , Go back, for example JSON data format . Of course , What kind of data format to return , According to the client's "ACCEPT" Ask the head to decide .

SpringMVC working principle ?

1.  The client sends the request to the front controller  DispatcherServlet
2. DispatcherServlet  Upon receipt of the request , call  HandlerMapping  Processor mapper , The request for  handler
3.  Processor mapper according to  url  Find the specific processor , Generate processor objects and processor interceptors ( Generate if any ) Return to  DispatcherServlet
4. DispatcherServlet  call  HandlerAdapter  Processor adapter 
5. HandlerAdapter  Called by the adapter   Specific processor (Handler, Also called back end controller )
6. Handler  Execution complete return  ModelAndView
7. HandlerAdaper  take  Handler  Execution results  ModelAndView  Return to  DispatcherServlet
8. DispatcherServlet  take  ModelAndView  Pass to  ViewResolver  view resolver   To analyze 
9. ViewResolver  Return details after parsing  View
10. DispatcherServlet  Yes  view  Conduct   Render view ( Fill model data into view )
11. DispatcherServlet  Responding to users 

Hello , I am a yltrcc, Daily sharing technology drops , Welcome to follow me : ylcoder

版权声明
本文为[yltrcc]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231859351005.html