preface

For a Java engineer, the JVM is a knowledge point that must be asked in the interview, and most of them may not have actual experience in the development and use of JVM, after all, they rarely use it in their work, or even have no contact with it. But as we move up the technical ladder, we must understand the JVM and know what it is. What should I do if I encounter performance problems on my project? This article will give you an in-depth look at everything you need to know about the JVM. This will help you make the transition from zero exposure to the JVM to an in-depth understanding of the JVM. Here is a detailed mind map for JVM performance optimization.

Java memory model

1. How does the Java code we developers write make it known to computers

  • First of all, the computer is a binary system. He only knows 01010101
  • For example, we often write HelloWord. Java how does the computer know how to run
  • HelloWord. Java is written by our programmers, we can know, but the computer does not know

Java file compilation process

1. Java files prepared by programmers

2. Compile to bytecode files from javac. Class :(why compile to class files because the JVM only knows.class files)

3. When compiled by the JVM into a computer-aware file (files are everything to a computer system)

Why is Java a cross-platform language

  • This kua platform is the kua platform for intermediate language (JVM) implementations
  • Java has the JVM to mask the underlying hardware from the software level, and the details of the instruction level make it compatible with various systems

C and C++ need to be compatible with different operating systems at the compiler level. Those who have written C and C++ know that some of the code in different operating systems is different

3. Differences between Jdk and Jre and JVM

  • The Jdk includes both the Jre and the Jvm, and the Jre includes the Jvm
  • The Jdk is the development kit we use to write code
  • Jre is the Java runtime environment, which is mostly written in C and C++. It is the basic class library that we need when compiling Java
  • The Jvm, commonly known as the Java Virtual Machine, is part of the Java runtime environment. It is a fictitious computer that implements Java applications by emulating various computer functions on the actual computer

Look at the official picture of Java, Jdk includes Jre, Jre includes JVM

4. Tell me what parts the JVM consists of and what the running process is.

  • The JVM consists of two subsystems and two components: the Class loader and the Execution engine. The two components are Runtime data area and Native Interface.
  • Class loader: Loads a Class file into a Class based on the given fully qualified Class name (e.g. Java.lang.object)
  • Method area in Runtime Data Area.
  • Execution Engine: Executes instructions in classes.
  • Native Interfaces: Interact with Native libraries. Native interfaces are used for interaction with other programming languages.
  • Runtime data area: This is what we call the memory of the JVM.
  • Process: The compiler converts Java code into bytecode, and the ClassLoader loads the bytecode into memory and places it in the methods section of the Runtime data area. Bytecode files are just instructions set specifications for the JVM. It cannot be delivered directly to the underlying operating system, so a specific command parser Execution Engine is required to translate bytecode into the underlying system instructions for Execution by the CPU. In this process, it is necessary to call the Native Interface of other languages to realize the function of the whole program.

5. Talk about the JVM runtime data area

  • During the execution of Java programs, the Java VIRTUAL machine divides the memory area managed by the virtual machine into several data areas. Each of these areas has its own purpose and time of creation and destruction. Some areas exist with the start of a virtual machine process, while others are created and destroyed depending on the start and end of a thread. The memory managed by the Java VIRTUAL machine is divided into the following areas:

Simply put, that’s where our Java runtime stuff is

  • Program Counter Register: The bytecode parser’s job is to select the next bytecode instruction to be executed by changing the value of this counter. Branch, loop, jump, exception handling, thread recovery and other basic functions need to be completed by this counter.

Why thread counters? Because threads don’t have memory

  • Java Virtual Machine Stacks: When each method is executed, a Stack Frame is created in the Java Virtual Machine Stack to store information about local variables, operand Stacks, dynamic links, method exits, etc.

A stack frame is the next unit in the Java virtual machine stack

  • Native Method Stack: Serves the same function as virtual machine Stack, except that the virtual machine Stack serves Java methods, while the local Method Stack serves Native methods for virtual machines.

The Native keyword modification method is not seen, the source code of Native method is mostly C and C++ code

  • The Java Heap is the largest area of memory in the Java virtual machine, which is shared by all threads and where almost all object instances are allocated memory.
  • Methed Area: Used to store data such as class information, constants, static variables, and just-in-time compiled code that have been loaded by the virtual machine.

The JVM runtime data area is explained in more detail later

6. Describe the program counter in detail? (Key understanding)

1. A program counter is a small memory space that can be thought of as: the address (line number) of the bytecode instructions being executed by the current thread.

2. Because Java virtual machine multithreading is implemented by switching threads and allocating processor execution time in turn, each processor will execute instructions in only one thread. Therefore, in order to restore the correct execution position after the thread switch, each thread has an independent program counter, counters between each thread do not affect each other, independent storage. This is called “thread private” memory. The program counter memory area is the only area in the virtual machine where OutOfMemoryError cases are not specified.

Summary: You can also call it a thread counter

** Example: ** The smallest unit of execution in Java is a thread, which is responsible for executing instructions that ultimately operate on our computer, the CPU. Running on the CPU, there is a very unstable factor called the scheduling policy, and the scheduling policy is based on the time slice, that is, the current nanosecond is allocated to that instruction.

If:

Thread A is watching live

7. Introduce the Java virtual machine stack in detail.

Does one method call another method create a lot of stack frames?

What does stack to heap mean?

Does the recursive call itself create a lot of stack frames?

8. Can you give me a detailed introduction to the Java heap? (Key understanding)

9. Can you explain the local method stack?

10. Could you explain the method area?

11. What is JVM bytecode execution engine

12. Have you heard of direct memory?

13. Do you know the garbage collection system?

14. What is the difference between stacks?

15. Deep and shallow copies

  • ShallowCopy simply adds a pointer to an existing memory address,
  • A deepCopy adds a pointer to a new memory and makes the pointer point to the new memory.
  • Shallow copy: refers only to the memory address to be copied. If the original address changes, the shallow-copied object will change accordingly.
  • Deep copy: The creation of a new memory location in the computer to store the copied object.

16. Does Java have memory leaks? Please explain why?

  • A memory leak is when objects or variables that are no longer in use remain in memory. In theory, Java has a GC garbage collection mechanism, which means that objects that are no longer in use are automatically reclaimed by the GC and erased from memory.
  • However, even then, Java still has memory leaks, and the causes of Java memory leaks are clear: A memory leak occurs when a long-lived object holds a reference to a short-lived object. Although a short-lived object is no longer needed, it cannot be reclaimed because a long-lived object holds a reference to it.

conclusion

Finally, from the small series of benefits

Java Virtual Machine (JVM) interview questions 51 +Java virtual machine The world of programming is always open to all who love programming. It is a world of freedom, equality, and sharing. I have always believed that.