Millions of people, thank you for this second you see here. I hope my series of interview questions can be helpful to you! ‘!
Wish you in the future days, keep love, go to the mountains and seas!
Three interview questions a day to make you a better self
Well, since you talked about string constant pools yesterday, let’s see
1. Can you talk about the runtime data area or memory structure of the JVM?
There are two cases where threads are private and threads are shared
Thread private: program counters, local method stack, virtual stack
Thread sharing: Heap and method area
- Program counter: This takes up a small chunk of memory space and records the number of rows of an execution of our current thread. Because the thread may be constantly switching, how to ensure that the current thread, where it is executed, is to rely on the program counter to achieve. This memory area is the only one where the Java Virtual Machine specification does not specify any OOM conditions.
- Virtual Stack: When a method is executed by the JVM, a stack frame is created in this area to store information about the method such as local variables, operand stacks, dynamic connections, and method placement addresses.
- Local Method Stack: It is similar to the Virtual Stack, but it mainly serves native methods, such as when Java needs to use C interfaces to serve.
- Heap: Also known as the Java heap or GC heap, it is the area of memory shared by threads. It is also the largest area of memory in the JVM, where almost all objects are stored for allocated memory, and the main management area for garbage collection.
- Method area: Stores class information loaded by the virtual machine, constants, static variables, compiler compiled code and other data.
Good good, JVM all know, then ask you a little more.
2. Class loading process
The main steps of system loading Class type file include loading -> connection -> initialization, and connection can be divided into verification -> preparation -> parsing
- Load: Gets the binary byte stream of the Class based on its fully qualified name, and generates a Class object representing the Class in memory
- Validation: Primary validation checks the correctness of class files, such as file format, metadata, bytecode, and symbol reference validation.
- Preparation: It is the stage of allocating memory for class variables and setting the class variable initial.
- Resolution: The process by which a virtual machine replaces a symbolic reference in a constant pool with a direct reference.
- Initialization: This is the last step in class loading, the process of actually executing the Java program code defined in the class.
Ok, let me ask you one last question:
3. What are class loaders and what are they?
For any class, the uniqueness of the JVM needs to be established by the class loader that loads it and by the class itself, each of which has a separate class namespace. The classloader loads the class file into the JVM memory with the specified fully qualified name and then converts it into a Class object.
There are four main types of loaders:
- The bootstrapClassLoader is used to load the Java core class libraries and cannot be directly referenced by Java programs.
- ExtensionClassLoader: This is used to load Java extension libraries. An implementation of the Java Virtual Machine provides a directory of extension libraries. The class loader finds and loads the Java classes in this directory.
- ApplicationClassLoader: It loads Java classes according to the CLASSPATH of the Java application. In general, it is used to load classes for Java applications. Through this. GetSystemClassLoader () to get it. In general, if we don’t have a custom classloader, the default is to use this loader.
- A user-defined ClassLoader can be implemented by inheriting the java.lang.ClassLoader class.
For a class in the process of loading, if a class loader receives the request of the class loading, it won’t go to the load the first class, it is delegate the request to their parent class loader to do, until the start of the top class loader, only when the parent was unable to complete the load request, will be one layer down an attempt to load classes. This pattern is known as the parental delegate pattern, and the benefits of this pattern are class hierarchies and security.
Nice guy! So much for today, looking forward to your arrival tomorrow, I hope I can continue to keep surprise!
Note: if the article has any mistakes and suggestions, please leave a message! If this article is also helpful to you, I hope you can pay close attention to it. Thank you very much! You can also search for Prince Nezha’s official account on WeChat and chat about me in private. Thank you guys!