How Java works: JVM, memory recovery, and more

Source: Internet
Author: User

Java Virtual Machine series articles

Http://developer.51cto.com/art/201001/176550.htm

The Java language introduces Java virtual machines and features cross-platform functions that adapt well to a variety of Web applications. At the same time, in order to improve the performance and robustness of the Java language, but also introduced a garbage collection mechanism and other new features, through these improvements so that Java has its unique working principle.

1. Java Virtual machine

A Java Virtual machine (MACHINE,JVM) is a software-emulated computer that performs a secure and compatible execution of bytecode stored in a. class file on any processor, whether on a computer or in another electronic device. The "Machine code" of a Java virtual machine is stored in a. class file and can sometimes be called a bytecode file.

The cross-platform feature of Java programs is that bytecode files can be run on any computer or electronic device that has a Java virtual machine, and the Java interpreter in the Java Virtual Machine is responsible for interpreting the bytecode file as a specific machine code to run. Therefore, at run time, the Java source program needs to be compiled into a. class file by the compiler.

The establishment of the Java Virtual machine needs to be implemented for different hardware and software platforms, both the model of the processor and the type of operating system should be considered. This allows Java virtual machines to be implemented on UNIX, Linux, Windows, and some real-time operating systems on embedded processing chips such as SPARC architecture, X86 Architecture, MIPS, and PPC.

2. Useless memory automatic recovery mechanism

During the execution of the program, some memory is abandoned after use, and if it is not collected in time, it is likely to cause a memory leak, which can lead to a system crash. In the C + + language is the memory of the programmer to recycle, programmers need to write the program when the memory is no longer used to release the object, such a way to manage the memory release is often due to the programmer's negligence caused by the memory is not recycled, but also increased the workload of the programmer. In the Java runtime Environment, there is always a system-level thread, specifically tracking the use of memory, regular detection of unused memory, and automatic recycling, to avoid the leakage of memory, but also reduce the workload of the programmer.

3. code security Check Mechanism

Safety and convenience are always relatively contradictory. The advent of the Java programming language makes it easy for client computers to upload or download Java programs from the network to run on the local computer, but how do you ensure that the Java program does not carry viruses or other dangerous purposes? To ensure the security of Java program execution, the Java language controls the security of illegal programs through applet programs, which ensures the survival of the Java language.

Java bytecode execution takes the following 3 steps:

(1) The class loader is responsible for loading the class file (. class file) into the Java virtual machine. In this procedure, you need to verify that the class file conforms to the class file specification.

(2) The Bytecode Checker (bytecode verifier) checks the code of the class file for certain illegal operations, such as writing the local computer file system in an applet program.

(3) If the bytecode checker is passed, the Java interpreter is responsible for interpreting the class file as machine code for execution.

Attention:

Java Virtual machine in the "sandbox" run mode, that is, the Java program code and data are limited to a certain amount of memory space to execute, do not allow the program to access memory outside the memory space. In the case of applets, access to the file system of the client machine is not allowed.

Java's operating environment

No matter which language needs to have its specific operating environment, that is, the platform. The Java language is no exception. But how do you understand that Java programs are independent of the hardware environment?

Almost all languages need to be compiled or interpreted to be performed by a computer, but Java is a bit different and requires both processes. In fact, it is for this reason that the Java language has a platform-independent. When a Java source is completed, it is first compiled by a Java translator into an intermediate code called bytecode, which is then translated by the Java platform interpreter into machine language to execute, the core of which is the JVM.

The compilation process for Java differs from other languages. Languages such as C + + are inextricably linked to the computer's hardware platform information at compile time. The compiler will convert the operands and opcode of all instructions into the offset of memory, that is, the memory allocation mode when the program runs, in order to ensure the normal operation of the program. Java, however, transforms the instruction into a. class file that does not contain hardware information and needs to be interpreted as long as the machine with the JVM is installed, creating memory allocations and then determining the address of an instruction by checking the table. This effectively guarantees the portability and security of Java.

The Java platform has a feature that is related to its structure. Typically, a program runs on a hardware or software-run environment, and is now more prevalent in Windows XP, Linux, Solaris, and MacOS. The Java platform is not quite the same, it consists of two parts, the JVM and the application design interface.

1. Jvm

The JVM is the core of the Java platform, and in order to make the compiled bytecode better interpreted and executed, the JVM is divided into 6 parts: The JVM interpreter, the instruction system, the register, the stack, the storage area, and the fragment Reclaim area.

JVM Interpreter: This is the CPU on which the virtual machine processes the field code.

JVM Instruction System: The system is very similar to a computer, and an instruction consists of two parts: Operation code and operation number. OpCode is a 8-bit binary number, mainly to illustrate the function of an instruction, the operand can be based on the need, the JVM has up to 256 different instructions.

Register: The JVM has its own virtual register so that it can quickly exchange data with the JVM's interpreter. For functionality, the JVM has 4 commonly used 32-bit registers: PC (program counter), Optop (operand top pointer), frame (current execution environment pointer), and VARs (pointer to the first local variable in the current execution environment).

JVM Stacks: The location and control center where data and information are stored at instruction execution time, which provides the information needed for JVM interpreter operations.

Store: The JVM store is used to store information such as compiled byte codes.

Fragment Reclaim area: JVM Fragment recycling refers to the recycling of specific instances of used Java classes from memory, which frees developers from the hassle and danger of programming their own memory. As the JVM continues to escalate, its fragmentation recovery techniques and algorithms are more reasonable. After the JVM version 1.4.1, there is a technique called generational collection, which simply means that the time of the object's survival in the program is divided into generations, which is used as a standard for fragment recycling.

2. Java Application Design Interface

Java application programming interface is referred to as the Java API, where dissemble designs interfaces for Java applications. It is a collection of software that has many of the controls needed for development, which can be used to aid development.

Java APIs and JVMs form the basic environment in which Java is run, both of which are integrated on top of the computer, where the Java platform separates a Java application from the hardware system, thus ensuring the program's independence. To better adapt to the needs of development, Java designers have provided 3 versions of the Java platform: Java 2 Micro Edition (J2ME), Java 2 standard Edition (J2SE), and Java 2 Enterprise Edition (Java ee), each version provides a rich development tool to suit different development needs.

How Java works: JVM, memory recovery, and more

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.