Interview -jvm class load than face questions Detailed class loading surface more than -jvm interview questions Detailed

Excerpt: https://www.cnblogs.com/bailiyi/p/11887470.html

Interview -jvm class load than face questions Comments

 

Interview questions

The problem with learning is the most efficient, this time we will try to answer the following questions:

  1. What is the load classes?
  2. What circumstances will trigger class loading?
  3. JVM loads a class to talk about the process
  4. When will allocate memory for variables?
  5. What JVM class loading mechanism?
  6. Parents delegate mechanism can break it? why

The answer on the end of the article, too late to see the principle can also jump directly to the final look directly answer.

In-depth principle

Class life cycle

Lifecycle like to believe we have been familiar, like this:

file

But this stuff is always back to forget, forget and back, like a horse, like what plum, right?

In fact, after understanding, basically I will not forget.

load

Loaded mainly to do three things:

  1. Find the class file (to obtain such a binary byte stream defined by the fully qualified class name)
  2. Into the method area (byte stream that represents the static storage structure into a run-time data structure area method)
  3. Open inlet (java.lang.Class generate a class object represents, as the access method entry region of these data structures)

In general, this step by the class loader is the class read into memory. Note that, although the third step to generate the object, but not in the heap, but in the method of the district.

connection

Connection is divided into three steps, generally are more like an interview and asked to prepare this step.

check

As the name suggests, check the Class file byte information contained in the stream meets the requirements of the current virtual machine.

ready

This step will static variables and constants allocated memory, and assignment.

It should be noted that only a static variable to a default value. Such as the following:

public static int value = 123;

At this time, values ​​are assigned to the value 0, not 123.

Static constant (static final modification) will direct assignment. Such as the following:

public static final int value = 123;

At this time, values ​​are assigned 123 value.

Resolve

Jvm parsing stage is the symbolic constant pool references replaced by a direct reference.

Well ...... what is the constant pool? What is the symbolic reference? What is a direct quote?

We put constant pool jvm memory structure he says. First is what is under the direct symbolic references and references.

Symbolic references and direct reference

Assuming a Worker class contains a class Car run () method, as follows:

class Worker{
    ......
    public void gotoWork(){ car.run(); //这段代码在Worker类中的二进制表示为符号引用 } ...... } 

Before parsing stage, Worker class does not know car.run () method where this memory, so only use a string representation of this method. This string contains sufficient information, such as information, the method name, class and other parameters, can be found in the appropriate position for practical use.

This string is called a symbolic reference .

In the resolution phase, found JVM memory area corresponding to the address according to the content of the string, and then replace the symbolic references directly to the destination pointers, handles, offset, etc., after which it can be used directly.

These direct pointer to the target, the handle, is offset to be referenced directly .

initialization

The main work of the class initialization of static variables are set for the initial value assigned program.

Remember the above static variables do:

public static int value = 123;

After this step, the value of value is finally a 123.

Summarized below:

file

Class initialization conditions

Java Virtual Machine Specification stringent requirements and only five cases must be initialized categories:

  1. Create a new instance of a class using the bytecode instructions, or using getstatic, putstatic reading or setting a value of the static field (except the constant into the constant pool) or when calling a static method, the corresponding class must be initialized or .
  2. When reflected by the methods of a class calls java.lang.reflect packet, if the class is not been initialized, first have to be initialized.
  3. When initializing a class, if you find not been initialized its parent, the parent class initialization is first triggered.
  4. When the virtual machine starts, the user needs to specify a main class (contains main () class method), the virtual first initialize the class.
  5. When using a dynamic language support jdk1.7, if a method handle java.lang.invoke.MethodHandle instance final analysis result REF_getStatic, REF_putStatic, RE_invokeStatic, and this method handle corresponding to the class is not initialized, it is necessary to trigger initialized .

In addition to these five cases, any other circumstances would not trigger initialization class.

For example, the following situations will not trigger this class initialization:

  1. Call the parent class's static field by subclasses. At this time, a parent compliance, and subclass does not meet any situation. Therefore, only the parent class is initialized.
  2. To refer to initialize the class, the class does not trigger through the array. Because new is an array, rather than classes.
  3. Static const call the class does not trigger initialization class, because the static constants at compile time will be stored in the constant pool class is called, does not refer to the definition of the constants class.

Class loading mechanism

Class loader

In the above we have said, the loading phase needed "to get the description of such binary byte stream through a fully qualified name of the class." This thing is a class loader is doing.

jvm loader carrying three types, namely:

  1. Start class loader.
  2. Extension class loader.
  3. Application class loader

Their inheritance as shown below:

file

Delegate parents

Parents delegate mechanism works as follows:

  1. The current ClassLoader first queries from his already loaded class if such has been loaded, if already loaded directly back to the original already loaded class. Each class has its own loader loads the cache, when a class is loaded into the cache will be later, so next time you can load directly returned.

  2.  Current classLoader cache did not find the class that is loaded when the delegation parent class loader to load, the parent class loader using the same strategy, first check its cache, then delegate the parent class of the parent class to load, until bootstrp ClassLoader.

  3.  When all of the parent class loader are not loaded, and then loaded by the current class loader, and placed in its own cache, so next time there is a load request return directly.

Why engage in so complicated? Deal with their own do not you?

Delegated advantage parent follows:

  1. Avoid repeated loading. When the father has loaded the class, when there is no need child ClassLoader loaded once again.
  2. for safety. Avoid core classes, such as String to be replaced.

Delegate parents break

"Parents delegation" mechanism is only recommended Java mechanism, the mechanism is not mandatory.

Such as JDBC broke the parents delegate mechanism. It is through Thread.currentThread). GetContextClassLoader () get (thread context loader to load the Driver implementation class, thus breaking the parents delegate mechanism.

As for why talk about it later.

answer

Now, we can answer the questions raised by the beginning of the article. Try to understand the basis on the answers, you do not need to memorize.

  1. What is the load classes?

    After the JVM class obtained by the class name of the binary stream, the class into the method area, and creates an object entry process called loaded classes. After loading, the class was put in the memory.

  2. Initialization what circumstances would trigger class?

    In five cases the class will be initialized:

    First, if this class is class entry, he is initialized.

    Second, use new to create an object, or call the class static variables, classes will be initialized. But not a static constant.

    Third, by obtaining the reflection type, the class will be initialized

    Fourth, if the sub-class is initialized, his parent class will be initialized.

    Fifth, the use of dynamic language support jdk1.7, call the static handler, it will be initialized.

  3. JVM loads a class to talk about the process

    With Question 1. But here you can also ask the interviewer is not to ask the life cycle of the class. If the life cycle of the class asked, you can answer the "loaded, connect, initialize, use, uninstall" five stages, the connection can be divided into "check, preparation, analysis" in three stages.

  4. When will allocate memory for variables?

    Allocate memory for static variables in the preparation phase.

  5. What JVM class loading mechanism?

    Parents delegate mechanism, will let the class loader to load their parent, the parent class can not be loaded, it will load themselves.

  6. Parents delegate mechanism can break it? why

    You can break, such as JDBC using thread context loader to break the parents delegate mechanism. The reason is that JDBC interface provides only, and does not provide an implementation. This problem can look at the contents of documents cited.

Citation

When will trigger Java class initialization and principles

Java symbolic references and direct reference

Symbolic references and direct reference

Direct references and symbolic references in the JVM

Interview questions: JVM class loading mechanisms explain (a) the JVM class loading procedure

Depth understanding of parents entrust mechanism

Parents delegate mode destruction -JDBC

 
Tags:  the Java the JVM gc

Interview questions

The problem with learning is the most efficient, this time we will try to answer the following questions:

  1. What is the load classes?
  2. What circumstances will trigger class loading?
  3. JVM loads a class to talk about the process
  4. When will allocate memory for variables?
  5. What JVM class loading mechanism?
  6. Parents delegate mechanism can break it? why

The answer on the end of the article, too late to see the principle can also jump directly to the final look directly answer.

In-depth principle

Class life cycle

Lifecycle like to believe we have been familiar, like this:

file

But this stuff is always back to forget, forget and back, like a horse, like what plum, right?

In fact, after understanding, basically I will not forget.

load

Loaded mainly to do three things:

  1. Find the class file (to obtain such a binary byte stream defined by the fully qualified class name)
  2. Into the method area (byte stream that represents the static storage structure into a run-time data structure area method)
  3. Open inlet (java.lang.Class generate a class object represents, as the access method entry region of these data structures)

In general, this step by the class loader is the class read into memory. Note that, although the third step to generate the object, but not in the heap, but in the method of the district.

connection

Connection is divided into three steps, generally are more like an interview and asked to prepare this step.

check

As the name suggests, check the Class file byte information contained in the stream meets the requirements of the current virtual machine.

ready

This step will static variables and constants allocated memory, and assignment.

It should be noted that only a static variable to a default value. Such as the following:

public static int value = 123;

At this time, values ​​are assigned to the value 0, not 123.

Static constant (static final modification) will direct assignment. Such as the following:

public static final int value = 123;

At this time, values ​​are assigned 123 value.

Resolve

Jvm parsing stage is the symbolic constant pool references replaced by a direct reference.

Well ...... what is the constant pool? What is the symbolic reference? What is a direct quote?

We put constant pool jvm memory structure he says. First is what is under the direct symbolic references and references.

Symbolic references and direct reference

Assuming a Worker class contains a class Car run () method, as follows:

class Worker{
    ......
    public void gotoWork(){ car.run(); //这段代码在Worker类中的二进制表示为符号引用 } ...... } 

Before parsing stage, Worker class does not know car.run () method where this memory, so only use a string representation of this method. This string contains sufficient information, such as information, the method name, class and other parameters, can be found in the appropriate position for practical use.

This string is called a symbolic reference .

In the resolution phase, found JVM memory area corresponding to the address according to the content of the string, and then replace the symbolic references directly to the destination pointers, handles, offset, etc., after which it can be used directly.

These direct pointer to the target, the handle, is offset to be referenced directly .

initialization

The main work of the class initialization of static variables are set for the initial value assigned program.

Remember the above static variables do:

public static int value = 123;

After this step, the value of value is finally a 123.

Summarized below:

file

Class initialization conditions

Java Virtual Machine Specification stringent requirements and only five cases must be initialized categories:

  1. Create a new instance of a class using the bytecode instructions, or using getstatic, putstatic reading or setting a value of the static field (except the constant into the constant pool) or when calling a static method, the corresponding class must be initialized or .
  2. When reflected by the methods of a class calls java.lang.reflect packet, if the class is not been initialized, first have to be initialized.
  3. When initializing a class, if you find not been initialized its parent, the parent class initialization is first triggered.
  4. When the virtual machine starts, the user needs to specify a main class (contains main () class method), the virtual first initialize the class.
  5. When using a dynamic language support jdk1.7, if a method handle java.lang.invoke.MethodHandle instance final analysis result REF_getStatic, REF_putStatic, RE_invokeStatic, and this method handle corresponding to the class is not initialized, it is necessary to trigger initialized .

In addition to these five cases, any other circumstances would not trigger initialization class.

For example, the following situations will not trigger this class initialization:

  1. Call the parent class's static field by subclasses. At this time, a parent compliance, and subclass does not meet any situation. Therefore, only the parent class is initialized.
  2. To refer to initialize the class, the class does not trigger through the array. Because new is an array, rather than classes.
  3. Static const call the class does not trigger initialization class, because the static constants at compile time will be stored in the constant pool class is called, does not refer to the definition of the constants class.

Class loading mechanism

Class loader

In the above we have said, the loading phase needed "to get the description of such binary byte stream through a fully qualified name of the class." This thing is a class loader is doing.

jvm loader carrying three types, namely:

  1. Start class loader.
  2. Extension class loader.
  3. Application class loader

Their inheritance as shown below:

file

Delegate parents

Parents delegate mechanism works as follows:

  1. The current ClassLoader first queries from his already loaded class if such has been loaded, if already loaded directly back to the original already loaded class. Each class has its own loader loads the cache, when a class is loaded into the cache will be later, so next time you can load directly returned.

  2.  Current classLoader cache did not find the class that is loaded when the delegation parent class loader to load, the parent class loader using the same strategy, first check its cache, then delegate the parent class of the parent class to load, until bootstrp ClassLoader.

  3.  When all of the parent class loader are not loaded, and then loaded by the current class loader, and placed in its own cache, so next time there is a load request return directly.

Why engage in so complicated? Deal with their own do not you?

Delegated advantage parent follows:

  1. Avoid repeated loading. When the father has loaded the class, when there is no need child ClassLoader loaded once again.
  2. for safety. Avoid core classes, such as String to be replaced.

Delegate parents break

"Parents delegation" mechanism is only recommended Java mechanism, the mechanism is not mandatory.

Such as JDBC broke the parents delegate mechanism. It is through Thread.currentThread). GetContextClassLoader () get (thread context loader to load the Driver implementation class, thus breaking the parents delegate mechanism.

As for why talk about it later.

answer

Now, we can answer the questions raised by the beginning of the article. Try to understand the basis on the answers, you do not need to memorize.

  1. What is the load classes?

    After the JVM class obtained by the class name of the binary stream, the class into the method area, and creates an object entry process called loaded classes. After loading, the class was put in the memory.

  2. Initialization what circumstances would trigger class?

    In five cases the class will be initialized:

    First, if this class is class entry, he is initialized.

    Second, use new to create an object, or call the class static variables, classes will be initialized. But not a static constant.

    Third, by obtaining the reflection type, the class will be initialized

    Fourth, if the sub-class is initialized, his parent class will be initialized.

    Fifth, the use of dynamic language support jdk1.7, call the static handler, it will be initialized.

  3. JVM loads a class to talk about the process

    With Question 1. But here you can also ask the interviewer is not to ask the life cycle of the class. If the life cycle of the class asked, you can answer the "loaded, connect, initialize, use, uninstall" five stages, the connection can be divided into "check, preparation, analysis" in three stages.

  4. When will allocate memory for variables?

    Allocate memory for static variables in the preparation phase.

  5. What JVM class loading mechanism?

    Parents delegate mechanism, will let the class loader to load their parent, the parent class can not be loaded, it will load themselves.

  6. Parents delegate mechanism can break it? why

    You can break, such as JDBC using thread context loader to break the parents delegate mechanism. The reason is that JDBC interface provides only, and does not provide an implementation. This problem can look at the contents of documents cited.

Citation

When will trigger Java class initialization and principles

Java symbolic references and direct reference

Symbolic references and direct reference

Direct references and symbolic references in the JVM

Interview questions: JVM class loading mechanisms explain (a) the JVM class loading procedure

Depth understanding of parents entrust mechanism

Parents delegate mode destruction -JDBC

Guess you like

Origin www.cnblogs.com/xichji/p/11909561.html