SoFunction
Updated on 2025-04-14

Detailed explanation of the relationship between JVM and operating system

The relationship between JVM and operating system

The relationship between JVM (Java Virtual Machine) and the operating system isDependence and being dependedThe relationship between JVM also plays a roleAbstract and IsolationThe role of

1. Dependence and being depended

JVM depends on the operating system:

  • The JVM itself is a program (usually written in C/C++) that needs to run on the operating system.
  • The JVM requires the use of the operating system resources, such as:
    • Memory management:JVM applies for memory from the operating system, which is used for heap, stack, method area, etc.
    • Thread management:JVM uses the operating system's threading model to create and manage Java threads.
    • File system:JVM uses the operating system's file system to load class files, read configuration files, etc.
    • network:The JVM uses the operating system's network API to communicate networks.
    • Process Management:The JVM runs on the operating system as a process.
    • I/O Operation:JVM relies on I/O capabilities provided by the operating system.
  • Different operating systems have different JVM implementations (for example, Windows, Linux, and macOS have their own HotSpot JVM implementations).

Java programs rely on JVM:

  • The bytecode (.class file) generated after Java program is compiled needs to be run on the JVM.
  • JVM provides a runtime environment for Java programs, including class loading, memory management, garbage collection, security management, etc.
  • Java programs interact indirectly with the operating system through the JVM.

2. Abstraction and isolation

JVM abstracts the underlying operating system:

  • JVM provides a unified, platform-independent runtime environment for Java programs.
  • Java programs only need to interact with the JVM, and do not need to care about the specific details of the underlying operating system.
  • This gives Java programs the feature of "Write Once, Run Anywhere, WORA".

JVM isolates Java programs and operating systems:

  • Java programs run in the JVM sandbox and cannot directly access the operating system resources (unless through special methods such as JNI).
  • This improves the security of Java programs and prevents malicious code from corruption of the operating system.
  • JVM can also prevent mutual interference between Java programs.

Illustration:

+------------------------+
|      Java program         |   (.java, .class)
+------------------------+
         ↑  rely
         |  (Bytecode, API Call)
         ↓
+------------------------+
|          JVM           |   (Java Virtual Machine)
+------------------------+
         ↑  rely
         |  (系统Call, Resource Request)
         ↓
+------------------------+
|      operating system          |   (Windows, Linux, macOS, etc.)
+------------------------+
         ↑  rely
         |  (Hardware commands)
         ↓
+------------------------+
|        hardware            |   (CPU, Memory, harddisk, Internet, etc.)
+------------------------+

More detailed explanation

  1. JVM as a process:The JVM itself is a process on the operating system. When you runjavaWhen commanded, the operating system creates a new process to start the JVM.
  2. System calls:The JVM needs to interact with the operating system, such as allocating memory, creating threads, performing I/O operations, etc. These interactions are achieved through system calls. System calls are interfaces provided by the operating system to applications.
  3. JNI (Java Native Interface):If a Java program needs to directly access the operating system's resources or call a local library (for example, a library written in C/C++), you can use JNI. JNI allows Java code to interact with local code.
  4. Different JVM implementations:Different operating systems and hardware platforms have different JVM implementations. For example, Oracle's HotSpot JVM has versions for different platforms such as Windows, Linux, and macOS. These JVM implementations all follow the Java virtual machine specification, but the underlying implementation details may vary.
  5. portability:Since JVM blocks differences between the underlying operating system and hardware, Java programs can be run on different platforms as long as there is a corresponding JVM implementation.

Summarize

The relationship between the JVM and the operating system is:

  • The JVM relies on the resources and services provided by the operating system.
  • Java programs rely on the runtime environment provided by the JVM.
  • JVM abstracts the underlying operating system and provides a unified and platform-independent running platform for Java programs.
  • JVM isolates Java programs and operating systems, improving the security of Java programs.

JVM is like a "translator", translating Java bytecode into instructions that a specific operating system can understand and managing the operation of Java programs.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.