The vast sea of millions, thank you for this second you see here. I hope my series of interview questions can help you! ‘!

May you keep your love and go to the mountains and seas in the coming days!

Three interview questions a day, achievement better self

So, yesterday, since you talked about the string constant pool, I’ll tell you what

1. Can you talk about the runtime data area or memory structure of the JVM?

We can divide the two cases into thread private and thread shared

Thread private: program counters, local method stacks, virtual machine stacks

Thread sharing: heap and method area

  • Program counter: This occupies a small memory space and records the number of lines of execution in our current thread. Because the thread can be constantly switching, how to ensure that when it reaches the current thread, it is implemented by the program counter. This memory area is the only one where the Java Virtual Machine specification does not specify any OOM condition.

  • Virtual machine stack: When the JVM executes a method, it creates a stack frame in this area, which stores various information about the method such as local variables, operand stacks, dynamic concatenation, and method return addresses.

  • Native method stack: This is similar to the virtual machine stack, but it mainly serves native methods, such as when Java needs to use C interface services.

  • Heap: Also known as the Java heap or GC heap, it is a thread-shared area of memory and the largest memory footprint in the JVM. It is where almost all objects are stored and allocated memory. It is also the main area of garbage collection management.

  • Method area: stores some virtual machine loaded class information, constants, static variables, compiler compiled code and other data.

Good good, the JVM knows, then ask you a little more.

2. Class loading process

The main steps of the system to load a Class file are load -> connect -> initialize, and connect can be divided into verification -> prepare -> parse

  • Load: Gets the binary byte stream of a Class based on its fully qualified name and generates a Class object in memory that represents the Class
  • Validation: Main validation checks the correctness of class files, such as file format, metadata, bytecode, and symbol references.
  • Preparation: The initial stage of allocating memory and setting up class variables.
  • Resolution: The process by which the virtual machine replaces symbolic references in the constant pool with direct references.
  • Initialization: This is the final step in class loading, the actual execution of the Java program code defined in the class.

Okay, one last thing for you:

3. What are classloaders, and what are they?

For any class, uniqueness within the JVM needs to be established both by the classloader that loads it and by the class itself. Each classloader has a separate class namespace. The classloader loads a class file into 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 (BootstrapClassLoader) is used to load Java core class libraries and cannot be referenced directly by Java programs.
  • ExtensionClassLoader: It is used to load Java extension libraries. The Implementation of the Java Virtual machine provides an extension library directory. The class loader finds and loads Java classes in this directory.
  • ApplicationClassLoader: it loads Java classes based on the CLASSPATH of the Java application. In general, Java application classes are loaded by it. Through this. GetSystemClassLoader () to get it. In general, if we don’t have a custom class loader this is the default.
  • Custom class loaders can be implemented by inheriting java.lang.ClassLoader classes.

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 the parent delegate pattern, which has the benefit of hierarchical classes and security.

Nice guy! Today is over here, looking forward to your arrival tomorrow, I hope to continue to keep a surprise!

Note: If there are any mistakes and suggestions, please leave a message! If this article is helpful to you, I hope you will pay attention to it for three times. Thank you very much! You can also search the wechat prince Nezha public number private chat me, thank you guys!