About Class loading and initialization when the JVM starts

Overview

The HotSpot Runtime Overview says:

The VM loads core classes such as java.lang.Object, java.lang.Thread, etc. at JVM startup. Loading a class requires loading all superclasses and superinterfaces. And classfile verification, which is part of the linking phase, can require loading additional classes.

So what kind of Class was loaded and when? Let's take a look at Unified JVM Logging.

Work environment

Ubuntu 18.04 as well as previous article And I am using OpenJDK 13, please refer to that for the specific construction method.

Take a look at startup time

First, let's see the rough startup time of the JVM with the variable -Xlog: startuptime.

$ $JAVA_HOME/bin/java -Xlog:startuptime -version
[0.168s][info][startuptime] StubRoutines generation 1, 0.0257810 secs
[0.229s][info][startuptime] Genesis, 0.0605841 secs
[0.232s][info][startuptime] TemplateTable initialization, 0.0010887 secs
[0.263s][info][startuptime] Interpreter generation, 0.0302811 secs
[0.693s][info][startuptime] StubRoutines generation 2, 0.0152926 secs
[0.697s][info][startuptime] MethodHandles adapters generation, 0.0017809 secs
[0.703s][info][startuptime] Start VMThread, 0.0010136 secs
[0.997s][info][startuptime] Initialize java.lang classes, 0.2923160 secs
[1.018s][info][startuptime] Initialize java.lang.invoke classes, 0.0159241 secs
[3.161s][info][startuptime] Initialize module system, 2.1431533 secs
[3.173s][info][startuptime] Create VM, 3.1052374 secs
openjdk version "13.0.3-internal" 2020-04-14
OpenJDK Runtime Environment (fastdebug build 13.0.3-internal+0-adhoc.root.jdk13u)
OpenJDK 64-Bit Server VM (fastdebug build 13.0.3-internal+0-adhoc.root.jdk13u, mixed mode)

What you can see here is how many classes and modules have been initialized in the Start VM Thread throat.

[0.997s][info][startuptime] Initialize java.lang classes, 0.2923160 secs
[1.018s][info][startuptime] Initialize java.lang.invoke classes, 0.0159241 secs
[3.161s][info][startuptime] Initialize module system, 2.1431533 secs

See Class load and ʻinit`

Next, set the variables -Xlog: startuptime, class + init, class + load and try again.

$ $JAVA_HOME/bin/java -Xlog:startuptime,class+init,class+load -version
[0.096s][info][class,load] path: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[0.159s][info][startuptime] StubRoutines generation 1, 0.0283598 secs
[0.220s][info][startuptime] Genesis, 0.0597463 secs
[0.223s][info][startuptime] TemplateTable initialization, 0.0012748 secs
[0.252s][info][startuptime] Interpreter generation, 0.0291667 secs
[0.284s][info][class,load ] path: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[0.285s][info][class,load ] path: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[0.314s][info][class,load ] java.lang.Object source: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[0.321s][info][class,load ] java.io.Serializable source: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
...
[0.553s][info][class,load ] java.util.Iterator source: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[0.578s][info][class,load ] java.lang.NullPointerException source: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[0.579s][info][class,load ] java.lang.ArithmeticException source: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[0.619s][info][startuptime] StubRoutines generation 2, 0.0132195 secs
[0.622s][info][startuptime] MethodHandles adapters generation, 0.0018546 secs
[0.627s][info][startuptime] Start VMThread, 0.0007446 secs
[0.629s][info][class,init ] 0 Initializing 'java/lang/Object' (0x0000000100001080)
[0.644s][info][class,init ] 1 Initializing 'java/lang/CharSequence'(no method) (0x00000001000016b8)
...
[0.898s][info][class,init ] 135 Initializing 'java/lang/IllegalArgumentException'(no method) (0x000000010002b0e8)
[0.898s][info][startuptime] Initialize java.lang classes, 0.2690178 secs
[0.902s][info][class,init ] 136 Initializing 'java/lang/invoke/MethodHandle' (0x000000010000c560)
...
[0.925s][info][class,init ] 144 Initializing 'java/lang/invoke/MethodHandleNatives' (0x000000010000d2e8)
[0.925s][info][startuptime] Initialize java.lang.invoke classes, 0.0232579 secs
[0.935s][info][class,load ] jdk.internal.module.ModuleBootstrap source: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
...
[3.112s][info][class,load ] jdk.internal.module.ModuleBootstrap$SafeModuleFinder source: /vagrant/jdk/jdk13u/build/linux-x86_64-server-fastdebug/jdk/modules/java.base
[3.113s][info][class,init ] 662 Initializing 'jdk/internal/module/ModuleBootstrap$SafeModuleFinder'(no method) (0x000000010008e930)
[3.113s][info][startuptime] Initialize module system, 2.1876125 secs
[3.124s][info][startuptime] Create VM, 3.0688743 secs
openjdk version "13.0.3-internal" 2020-04-14
OpenJDK Runtime Environment (fastdebug build 13.0.3-internal+0-adhoc.root.jdk13u)
OpenJDK 64-Bit Server VM (fastdebug build 13.0.3-internal+0-adhoc.root.jdk13u, mixed mode)

What I found here is actually two stages

Check with source code

If you search the source code for the above text, you'll find where to inspire these two steps.

Summary

Initializes these core Classes when the JVM boots

Recommended Posts

About Class loading and initialization when the JVM starts
About Class loading and initialization when the JVM starts
When log data accumulates in Rails and the environment stops working
About next () and nextLine () of the Scanner class
About the StringBuilder class
About the File :: Stat class
About the same and equivalent
About the equals () and hashcode () methods
A murmur about the utility class
About the relationship between the Java String equality operator (==) and initialization. Beginners
About the operation of next () and nextLine ()
About the difference between irb and pry
About the mechanism of the Web and HTTP
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
About the speed when fetching values from HashMap
Think about the combination of Servlet and Ajax
[Rails / ActiveRecord] About the difference between create and create!
When you get lost in the class name
[Rails] About collection when rendering partial (when loading partial template)
A note about the Rails and Vue process
[CentOS 8] About the work when rebuilding ConoHa VPS
Clone and duplicate the app from GitHub (initialization)
About the mechanism when displaying the GUI with Docker
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
Copy and paste the source in class development ...? That's NO! Let's know about "inheritance"!