Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the operation principle of Java program?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how the Java program works, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Class file content

The class file contains the bytecode executed by the Java program

The data is arranged in a compact format in the binary stream of the class file, with no separator in the middle.

The file begins with a 0xcafebabe (hexadecimal) special flag

JVM Runtime data area

Thread monopoly: each thread has its own independent space, which is created and destroyed with the life cycle of the thread.

Thread sharing: all threads can access this piece of memory data, which is created and destroyed with the virtual machine or GC

Method area

The method area is the memory area shared by each thread

Used to store class information, constants, static variables, instantly compiled code and other data that have been loaded by the virtual machine

Although the Java virtual machine specification describes the method area as a logical part of the heap, it has an alias called Non-Heap, which is supposed to distinguish it from the Java heap.

Oracle's Hotspot virtual machine is placed in the "permanent generation" (Permanent Generation) in the legal area of Java7, and Java8 is placed in the metadata space, and this area is managed through the GC mechanism.

The run-time pool is part of the method area

Java reactor

The Java heap is a memory area that is shared by all and is created when the virtual machine starts

Store an instance of an object

The main management area of the garbage collector

Java heap can also be subdivided into Cenozoic and old age, and Cenozoic can be subdivided into Eden space, From Survivor space and To Survivor space.

OutOfMemoryError will be thrown when the space is full.

Java virtual machine stack

The Java virtual machine stack is thread private and its life cycle is the same as that of threads.

Java virtual machine stack describes the memory model of Java method execution: when each method is executed, a stack frame (stack frame is the basic data structure of method runtime) is created at the same time to store local variables, operation stack, dynamic link, method exit and other information.

The default maximum stack memory is 1m. If the stack memory is exceeded, StackOverFlowError is thrown.

Local method stack

The function of the local method stack is similar to that of the virtual machine stack. The virtual machine stack is prepared for the virtual machine to execute the Java method, and the local method stack is prepared for the virtual machine to use the Native local method.

The virtual machine stack in the Hotspot virtual machine is implemented in the same way as the local method stack, and StackOverFlowError is thrown when the size is exceeded.

Program counter

A program counter is a small memory space owned by a thread.

Records the bytecode location executed by the current thread, stores the bytecode instruction address, and if the Native method is executed, the counter is empty

CPU executes only one thread's instructions at a time. JVM multithreading takes turns switching and allocating CPU execution time. After the thread switch, you need to use the program counter to restore the correct execution position.

View the contents of the class file

Test with Demo.Java, run javac Demo.java to compile into a class file, and then run javap-v Demo.class > Demo.txt to view the contents of the class file

Demo.Java

Public class Demo {

Public static void main (String [] args) {

Int x = 500

Int y = 100

Int a = x / y

Int b = 50

System.out.println (a + b)

}

}

Demo.txt

Classfile / E:/*/Demo.class

Last modified 2019-6-30; size 412 bytes

MD5 checksum efd785af33e58aa9fc9834110b74b87b

Compiled from "Demo.java"

Public class Demo

Minor version: 0 / / minor version number major version: 52 / / Major version number rules: JDK5,6,7,8 corresponds to 49, 50, 51, and 52 respectively.

Flags: ACC_PUBLIC, ACC_SUPER / / access flag Constant pool: / / static constants contained in constant pool class information. After compilation, you can confirm # 1 = Methodref # 5.room14 / / java/lang/Object. "": () V

# 2 = Fieldref # 15.16 / / java/lang/System.out:Ljava/io/PrintStream

# 3 = Methodref # 17.018 / / java/io/PrintStream.println: (I) V

# 4 = Class # 19 / / Demo

# 5 = Class # 20 / / java/lang/Object

# 6 = Utf8

# 7 = Utf8 () V

# 8 = Utf8 Code

# 9 = Utf8 LineNumberTable

# 10 = Utf8 main

# 11 = Utf8 ([Ljava/lang/String;) V

# 12 = Utf8 SourceFile

# 13 = Utf8 Demo.java

# 14 = NameAndType # 6 7 / "": () V

# 15 = Class # 21 / / java/lang/System

# 16 = NameAndType # 22 23 / / out:Ljava/io/PrintStream

# 17 = Class # 24 / / java/io/PrintStream

# 18 = NameAndType # 25 26 / / println: (I) V

# 19 = Utf8 Demo

# 20 = Utf8 java/lang/Object

# 21 = Utf8 java/lang/System

# 22 = Utf8 out

# 23 = Utf8 Ljava/io/PrintStream

# 24 = Utf8 java/io/PrintStream

# 25 = Utf8 println

# 26 = Utf8 (I) V

{

Public Demo (); / / default implicit no-parameter constructor descriptor: () V

Flags: ACC_PUBLIC

Code:

Stack=1, locals=1, args_size=1

0: aload_0

1: invokespecial # 1 / / Method java/lang/Object. "() V

4: return

LineNumberTable:

Line 1: 0

Public static void main (java.lang.String []); / / entry of the program main method descriptor: ([Ljava/lang/String;) V

Flags: ACC_PUBLIC, ACC_STATIC / / access control Code:

Stack=3, locals=5, args_size=1 / / depth of Operand stack, number of local variables, number of parameters 0: sipush 500 / / Jvm execution engine executes these source compiled instructions, 3: istore_1 / / translated by javap is an operator, instruction code is stored in the class file, and the previous data is offset

4: bipush 100 / / Jvm distinguishes between different instructions according to this. For more information, please see 'JVM instruction Code Table'.

6: istore_2

7: iload_1

8: iload_2

9: idiv

10: istore_3

11: bipush 50

13: istore 4

15: getstatic # 2 / / Field java/lang/System.out:Ljava/io/PrintStream

18: iload_3

19: iload 4

21: iadd

22: invokevirtual # 3 / / Method java/io/PrintStream.println: (I) V

25: return

LineNumberTable:

Line 3: 0

Line 4: 4

Line 5: 7

Line 6: 11

Line 7: 15

Line 8: 25

}

SourceFile: analysis of the complete Operation of "Demo.java" Program

What is the operating principle of the Java program is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report