In-depth exploration of Java working principles: JVM memory collection and other

Source: Internet
Author: User

Java introduces the Java virtual machine, which has the function of running across platforms and can adapt to various web applications. At the same time,

To improve the performance and robustness of the Java language, new features such as the garbage collection mechanism are introduced.

Java has its unique working principle.

1. Java Virtual Machine

Java Virtual Machine (JVM) is a computer simulated by software (

Secure and compatible execution of bytecode stored in the. Class file, whether on a computer or on other electronic devices.

The "machine code" of the Java Virtual Machine is stored in the. Class file, which is also called a bytecode file.

The cross-platform feature of Java programs mainly refers to the ability to enable bytecode files on any computer or electronics with a Java Virtual Machine

The Java interpreter in the Java Virtual Machine is responsible for interpreting the bytecode file as a specific machine code for running.

Therefore, at runtime, the Java source program must be compiled into a. Class file through the compiler.

The establishment of a Java virtual machine needs to be implemented based on different hardware and software platforms.

System type. Therefore, on the embedded processing chip such as the iSCSI structure, x86 structure, MIPS and PPC,

Java virtual machines can be implemented in Linux, windows, and some real-time operating systems.

2. Useless memory Automatic Recovery Mechanism

In the execution process of the program, some of them are discarded after being used. If they are not recycled in time, it is very feasible.

This can cause memory leakage and system crash. In C ++, memory is recycled by programmers.

When writing a program, you need to release the object memory that is no longer used. This method of manually managing the memory release is often caused by the program

The memory cannot be recycled due to employee negligence, and the workload of programmers is also increased. In the Java Runtime Environment

There is a system-level thread that specifically tracks memory usage, regularly detects memory that is no longer in use, and automatically

To avoid Memory leakage and reduce the workload of programmers.

3. Code Security Check Mechanism

Security and convenience are always in conflict. The emergence of the Java programming language allows the client computer to easily access the network

Upload or download the Java program to the local computer to run, but how to ensure that the Java program does not carry viruses or has no other

What about dangerous purposes? To ensure the security of Java program execution, Java uses Applet programs to control illegal programs.

Security, that is, it ensures the survival of the Java language.

 
Java bytecode execution requires the following three steps:

(1) The Class Loader loads class files (. class files) to the Java Virtual Machine. Here

The process needs to check whether such files comply with the class file specifications.
 

(2) bytecode verifier checks whether some illegal operations exist in the code of such files.

For example, write the file system of a local computer in an applet.

(3) If the bytecode validator passes the verification, the Java interpreter is responsible for interpreting the file as a machine code for execution.

Note:

The Java virtual machine adopts the "sandbox" running mode, that is, the Java program code and data are restricted to a certain amount of memory space.

Row. The program is not allowed to access memory other than the memory space. If it is an applet, access to the client machine is not allowed

File System.
  

Java Runtime Environment

Either language requires a specific runtime environment, that is, the platform. The Java language is no exception. But how to handle

Does the Java program have nothing to do with the hardware environment?

Almost all languages can be executed by computers only through compilation or interpretation, but Java is a little different.

These two processes are required. In fact, it is precisely for this reason that the Java language has platform independence. When finished

After a Java source program is compiled, it is first compiled into an intermediate code called bytecode through the Java translation program.

Then, the interpreter on the Java platform converts the interpreter into a machine language for execution. The core of this platform is JVM.

Java compilation process is different from other languages. When compiling a language like C ++, it is similar to the hardware platform of a computer.

Information is inseparable. The compiler converts the operands and operation codes of all commands into memory offsets through the Table query, that is

The memory allocation method when the program is running to ensure the normal operation of the program. While Java converts commands into one

Class file. This file does not contain hardware information. If you need to execute the file, you only need to perform the solution on a machine installed with JVM.

Release, create a memory allocation, and then use the table to determine the address of a command. This effectively ensures the availability of Java

Portability and security.

The Java platform has such features related to its structure. Generally, a running platform is a hardware or software operating system.

Currently, Windows XP, Linux, Solaris, and MACOs are popular. Java platforms are not the same,

It consists of two parts: the JVM and the application design interface.

1. JVM

JVM is the core of the Java platform. To better interpret and execute the bytecode generated by compilation, the JVM is divided into six

Part 1: JVM interpreter, command system, register, stack, storage zone, and fragment zone.

◆ JVM Interpreter: the CPU that the VM processes the field code.

◆ JVM Command System: similar to a computer, a command consists of an operation code and an operand. The operation code is

The 8-bit binary number is mainly used to describe the function of a command. The number of operations can be determined as needed. There are up to 256 JVM types.

Different operation commands.
 
◆ Register: The JVM has its own virtual register, so that it can quickly exchange data with the JVM interpreter. To

JVM has four common 32-bit registers: PC (program counter) and optop (top pointer of the operand stack ).

), Frame (pointer to the current execution environment), and vars (pointer to the first local variable in the current execution environment ).

◆ JVM Stack: the place and control center for storing data and information during command execution. It is provided to the JVM interpreter for calculation.

Information.

◆ Storage zone: The JVM storage zone is used to store compiled bytecode and other information.

◆ Fragment collection zone: JVM fragment Collection refers to the collection of specific instances of the used Java class from the memory, which enables

Developers are free from the trouble and danger of programming and controlling the memory. With the continuous upgrade of JVM, the technology and calculation of its fragmentation recovery

Method is more reasonable. After JVM version 1.4.1, a technology called generational collection is generated. in simple words, objects are used in programs.

The survival time is divided into generations, and fragments are recycled based on this standard.

2. Java application design interface

Java application programming interface is short for Java API. Its Chinese name is the interface designed for Java applications.

Port. It is a collection of software, which has many controls required during development and can be used to assist development.

Java API and JVM constitute the basic environment for Java running. These two types of software are integrated on the computer.

The Java platform separates a Java application from the hardware system, thus ensuring the independence of the program.

. To better adapt to development needs, Java designers provide three versions of Java platform: Java 2 Micro

Edition (j2se), Java 2 Standard Edition (j2se), and Java 2 Enterprise Edition (

J2EE), each version provides a variety of development tools to adapt to different development needs.

 

Related Article

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.