Home Java javaTutorial Analysis of program running mechanism and errors in Java

Analysis of program running mechanism and errors in Java

Sep 26, 2017 am 10:05 AM
java Operating mechanism mistake

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles