Home >Java >javaTutorial >Analysis of program running mechanism and errors in Java
This article mainly introduces the relevant content of the Java Virtual Machine (JVM) as well as the running mechanism and error analysis of Java programs. Friends who need it can learn more.
JVM (Java Virtual Machine) A specification for computing devices that can be implemented in different ways (software or hardware). Compiling a virtual machine's instruction set is very similar to compiling a microprocessor's instruction set. The Java virtual machine includes a set of bytecode instructions, a set of registers, a stack, a garbage collection heap and a storage method field.
The Java Virtual Machine (JVM) is an imaginary computer that can run Java code. As long as the interpreter is ported to a specific computer according to the JVM specification, any compiled Java code can be guaranteed to run on that system.
1. Why use the Java Virtual Machine
A very important feature of the Java language is its independence from the platform. The use of Java virtual machine is the key to achieving this feature. If general high-level languages want to run on different platforms, they at least need to be compiled into different target codes. After the introduction of the Java language virtual machine, the Java language does not need to be recompiled when running on different platforms. Java language usage model The Java virtual machine shields information related to specific platforms, so that the Java language compiler only needs to generate the target code (bytecode) that runs on the Java virtual machine, and can run on multiple platforms without modification. run. When the Java virtual machine executes bytecode, it interprets the bytecode into machine instructions for execution on the specific platform.
Java running mechanism
The running of a Java program must go through three steps: writing, compiling, and running.
Writing refers to inputting program code in the Java development environment, and finally forming a Java source file with the suffix .java.
Compilation refers to the process of using the Java compiler to troubleshoot source files. After compilation, a bytecode file with the suffix .class will be generated. This is not like the C language that ultimately generates an executable file. document.
Running refers to using a Java interpreter to translate a bytecode file into machine code, execute it and display the results
A bytecode file is a kind of file that is compatible with any specific machine The intermediate code is independent of the environment and operating system environment. It is a binary file and an object code file generated after the Java source file is compiled by the Java compiler. Neither programmers nor computers can directly read bytecode files. It must be interpreted and executed by a dedicated Java interpreter. Therefore, Java is a language that is interpreted and run on the basis of compilation.
The Java interpreter is responsible for translating bytecode files into machine code under specific hardware environments and operating system platforms for execution. Therefore, Java programs cannot run directly on existing operating system platforms. They must run on a software platform called the Java virtual machine.
The Java Virtual Machine (JVM) is a software environment for running Java programs. The Java interpreter is part of the Java Virtual Machine. When running a Java program, the JVM will first be started, and then it will be responsible for interpreting and executing Java bytecode, and Java bytecode can only run on the JVM. In this way, the JVM can be used to separate the Java bytecode program from the specific hardware platform and operating system environment. As long as the JVM for a specific platform is installed on different computers, the Java program can run without considering The current specific hardware platform and operating system environment do not need to consider the platform on which the bytecode file was generated. The JVM hides the specific differences between different software and hardware platforms, thereby achieving true binary code-level cross-platform transplantation. JVM is the platform-independent foundation of Java. Java's cross-platform features are realized by running Java programs in JVM.
Java language's "write once, run anywhere" approach effectively solves the need for most high-level programming languages to target different The system compiles and generates different machine codes, that is, the heterogeneous problem of hardware environment and operating platform, which greatly reduces the overhead of program development, maintenance and management.
It should be noted that Java programs can achieve cross-platform features through the JVM, but the JVM is not cross-platform. In other words, JVMs on different operating systems are different. JVMs on Windows platforms cannot be used on Linux, and vice versa.
JAVA program operation error analysis
Generally speaking, a large-scale project that has been put into operation may have problems in the following situations:
1. Abnormal CPU usage
1) Check the CPU usage, the usage of the target process, and then check the usage of each core. It can assist in locating whether it is a single thread problem or a thread pool problem.
2) Sometimes, after the program has been running normally for a period of time, the CPU suddenly rises vertically, which may be related to the lock in the program (if the lock is held for a short time, it is better to try cas+yield To implement spin lock)
3) deadlock, directly export the call stack to find a solution to the problem.
2. Abnormal memory
1) Memory leak, nothing to say, dump the stack to find the problem
2) Frequent GC will also lead to insufficient performance. If GC occurs frequently in the program, you should pay attention. If increasing the size of the new generation still does not solve the problem, you need to locate a large number of codes that create temporary objects (you can use objects Pool technology to avoid repeated application of memory)
3. Unexpected termination of a worker thread
4. Abnormal IO
1) View open files, IO operation usage, and disk usage. You can use the command df iostat, etc.
2) To check whether there are programs occupying monitoring and network usage, you can use the command netstat, etc.
Use tools to analyze faults
1.jmap
jmap pid View memory usage related information in the JAVA process by default
jmap -histo pid View memory Number of active instances
jmap -dump:format=b,file=(file name) pid Completely exports the java program memory. The complete analysis is divided into three processes. Execute jmap -dump:format=b,file=a.bin once after the program is initialized, then execute it once when the memory usage starts to increase, and finally execute it again after reaching the upper limit. Use JHat Or a third-party tool to open the dump file.
3.jstack or JCONSOLE
Default You can view process call stack information to analyze IO timeouts, deadlocks, or other situations. Information that must be analyzed when an abnormality occurs in a program can assist in locating and troubleshooting problems.
jstat -gc pid gaptime View GC related information
jstat -compiler pid View real-time compilation information
4.kill -3
Same as above, suitable for servers that do not have the develop tool installed, and can output call stack information and part of GC information
5.iftop View network port
View network incoming and outgoing traffic The traffic between the target server and the target server can assist in troubleshooting whether it is caused by an attack.
pstack virtual machine stack
needs to install gdb, which is generally used to check c/c++ programs , when certain virtual machine level errors occur.
Summarize
The above is the detailed content of Analysis of program running mechanism and errors in Java. For more information, please follow other related articles on the PHP Chinese website!