On the running mechanism and error analysis of Java program

JVM (Java virtual machine) a specification for computing devices that can be implemented in different ways (software or hardware). Compiling the instruction set of a virtual machine is very similar to compiling the instruction set of a microprocessor. The Java virtual machine includes a set of bytecode instruction sets, a set of registers, a stack, a garbage collection heap, and a storage method field.

A 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 the system.

1. Why use Java virtual machine

A very important feature of Java language is platform independence. Using java virtual machine is the key to realize this feature. If a general high-level language wants to run on different platforms, it needs to be compiled into different object code at least. After the introduction of Java language virtual machine, Java language does not need to be recompiled when running on different platforms. Java language usage pattern java virtual machine shields the information related to the specific platform, so that the Java language compiler can run on a variety of platforms without modification only by generating the object code (bytecode) running on the Java virtual machine. When executing bytecode, Java virtual machine interprets bytecode into machine instruction execution on specific platform.

Java running mechanism

The running of Java program must go through three steps: writing, compiling and running.

Writing refers to inputting program code in java development environment, and finally forming the suffix Java source file for Java.

Compilation refers to the process of using the java compiler to troubleshoot the source file. After compilation, the suffix will be generated Class, which is not the final executable file generated like C language.

Run refers to using the Java interpreter to translate the bytecode file into machine code, execute and display the results

Bytecode file is an intermediate code independent of any specific machine environment and operating system environment. It is a binary file and an object code file generated by java source file compiled by java compiler. Neither programmers nor computers can directly read the bytecode file. It must be interpreted and executed by a special Java interpreter. Therefore, Java is a language that interprets and runs on the basis of compilation.

The Java interpreter is responsible for translating bytecode files into machine code under specific hardware environment and operating system platform for execution. Therefore, Java program can not run directly on the existing operating system platform, it must run on the software platform called Java virtual machine.

Java virtual machine (JVM) is a software environment for running Java programs, and the Java interpreter is a part of the Java virtual machine. When running Java programs, the JVM will first start, 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 separate Java bytecode programs from specific hardware platforms and operating system environments, 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, or the platform on which the bytecode file is generated. JVM hides the specific differences between different software and hardware platforms, so as to realize the real binary code level cross platform transplantation. JVM is the basis of Java platform independence. Java's cross platform feature is realized by running Java programs in JVM.

The way of "write once, run anywhere" of Java language effectively solves the problem that most high-level programming languages need to compile different machine codes for different systems, that is, the heterogeneity of hardware environment and operating platform, and 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 does 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.

Analysis of Java program running error

Generally speaking, a large-scale project that has been put into operation may have problems in the following situations at most:

1. Abnormal CPU usage

1) Check the CPU usage, the usage of the target process, and then check the usage of each kernel. The secondary location can be a single thread problem or a thread pool problem.

2) Sometimes, after a normal 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, try CAS + yield to realize spin lock)

3) Deadlock, directly export the call stack to find a solution to the problem.

2. Abnormal memory

1) Memory leak. There's nothing to say. Dump has a stack search problem

2) Frequent GC will also lead to insufficient performance. When GC often occurs in programs, you should pay attention to increasing the size of the new generation. If it still cannot be solved, you need to locate a large number of codes that create temporary objects (you can use object pool technology to avoid repeated memory applications)

3. Unexpected termination of a worker thread

4. Abnormal IO

1) View the open file, IO operation occupation, and disk utilization. You can use the command DF, iostat, and so on

2) Check whether there are programs occupying listening and network utilization. You can use the command netstat, etc

Use tools to analyze faults

1.jmap

Jmap PID views the memory usage information in the java process by default

Jmap - histo PID view the number of active instances in memory

Jmap - dump: format = B, file = (file name) PID completely exports Java program memory. The complete analysis is divided into three processes. Jmap - dump: format = B, file = a.bin is executed once after program initialization, and then it is executed once when the memory occupation begins to rise. Finally, it is executed again after reaching the upper limit, and the dump file is opened with jhhat or a third-party tool.

3. Jstack or jconsole

By default, you can view the process call stack information to analyze IO timeout, deadlock, or other situations. The information that must be analyzed when an exception occurs in the program can assist in the positioning and troubleshooting of the problem.

Jstat - GC PID game view GC related information

Jstat - compiler PID view real-time compilation information

4.kill -3

As above, it is applicable to servers without developer tools installed. It can output call stack information and some GC information

5. Iftop can check the network port to check the traffic between the network in and out traffic and the target server, which can help check whether it is caused by the attack

Pstack virtual machine stack

GDB needs to be installed, which is generally used to check C / C + + programs in case of some virtual machine level errors.

summary

The execution of a java program must go through two steps: Compilation and interpretation. That's all for the introduction of Java program operation mechanism and error analysis. I hope it will be helpful to you. Interested friends can refer to other topics on this site. I hope you can support this site more.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>