Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the JVM use system calls to seek OS functionalities?

Tags:

java

jvm

We know that the JVM calls on the underlying system to allocate memory and CPU time, access files, and many more. How does it work internally to achieve its activities?

Does the JVM use system calls?

like image 872
webiondev Avatar asked Oct 12 '25 16:10

webiondev


1 Answers

Does the JVM use system calls?

Yes.

How does it work internally to achieve its activities?

The typical pattern is that some of the methods in a Java class are labelled as native. When the JVM encounters a call to a native method it makes a call into C or C++ code that is part of the JVM executable. The native method implementation typically does the following:

  1. Check arguments from Java, and translates them into a C / C++ compatible form. For example, String arguments need to be converted to zero-terminated form.

  2. Call the standard C / C++ library function with the arguments it needs.

  3. The library function makes the syscall.

  4. The OS does its stuff and the syscall returns.

  5. The standard C / C++ library function returns.

  6. The native method implementation checks the 'errno'. If there was an error, it creates a Java exception object and throws it.

  7. Otherwise, the native method implementation converts results, etc into Java objects and returns them to the caller of the Java method.

The details vary, depending on what the native method does.

If you want to get a deeper understanding, I recommend that you checkout a copy of the OpenJDK source tree and start trawling. (You need to do the hard yards yourself ....)

like image 58
Stephen C Avatar answered Oct 14 '25 08:10

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!