CVM启动和初始化过程
来源:互联网 发布:js date对象getmi 编辑:程序博客网 时间:2024/05/16 08:53
CVM Bootstrap and Initialization Process
You probably already know that CVM is written in C. So what happens from the VM's launched (invoking the C main()
function) all the way till the first line of Java code in your main()
method is executed? There is a lot going on during that period, and we usually refer to this as the VM bootstrap. This article will explain the details of the CVM bootstrapping and initialization process.
The CVM bootstrapping code is implemented in ansiJavaMain()
(ANSI compliant) that is called by the C main()
. The first thing ansiJavaMain()
does is convert and prepare the arguments for the VM. Then, it calls JNI_CreateJavaVM()
(defined in the JNI Invocation API) to create and initialize CVM. During that process, it initializes all the VM global states, including system mutexes, Java heap, threads, classloaders, etc. After the VM is created successfully, it loads the main class of the Java application and invokes the main()
Java method.
In the rest of the article, I am going to discuss the details of the CVM initialization process step by step.
Preloader Initialization
CVM supports class ROMization (at build time), which loads and links class data with the VM. We sometimes also call it class preloading. One of the first few things the VM needs to do is to initialize the ROMized classes. For each ROMized class blocks (the CVMClassBlock is the main data structure for storing meta-data about a Java class), it finds their corresponding Java instances and initializes them by filling in their class block pointer, classloader pointer, etc. It also iterates over the compressed string data and constructs the matching Java instances by filling in the string value, offset and length, etc. Note these ROMized classes do not live in the Java heap.
In CVM the method data structure (CVMMethodBlock) consists of immutable and mutable parts. The immutable part includes method name and type ID, methodtable index, etc. Once initialized by JCC (the ROMizer) or the classloader, these fields are never written into. The mutable part includes the invoker pointer, compiled code start PC, etc. As part of the preloader initialization, the VM initializes the method data for each ROMized class by copying the immutable part and filling in the mutable part.
System Mutexes Initialization
As described in one of Mark Lam's blog, The Big Picture (a Map of CVM), CVM has global mutexes that control synchronization throughout the VM subsystems. These include the jit lock, heap lock, thread-list lock, class table lock, loader cache lock, global roots lock, weak global roots lock, typeid lock, etc. During the bootstrapping process, CVM creates and initializes these global locks.
Initialization GC Global Root Stacks
Then CVM initializes GC root stacks, including global roots stack (for allocating JNI global references and CVM global roots), weak global roots stack (for allocating JNI weak global references), class global roots stack, classloader global roots stack, protection domain global roots stack, and class table root stack(for all dynamically loaded classes). The global root stacks are used by GC for scanning live objects.
Typeid System Initialization
The next thing the VM does is to initialize the typeid system and registering some commonly used typeids, such as <init>
, <clinit>
, and finalize
in the CVMglobals. That prevents having to repeatedly lookup these typeid later at runtime.
Classes System Initialization
Next is the class loader cache initialization. The loader cache is used to cache all <Class, ClassLoader>
pairs that are loaded in the system.
Initializing the Java Heap
CVM uses -Xms
, -Xmn
and -Xmx
options to specify the start, minimum and maximum heap size. It extracts those size information from the VM arguments, and allocates a heap of the start size. The heap can grow up to the maximum size at runtime.
Preallocating Object Monitors and Desperation Exception Objects
The VM creates a initial number of object monitors. This is a minimum number that the VM knows that it needs. The desperation exception objects include OutOfMemoryError, StackOverflowError, etc. They come in handy in circumstances where there is no memory availabe to create the exception object.
JVMTI Initialization
If debugger or profiler are supported, CVM also does JVMTI related initialization.
JIT Initialization
If runtime compiler (JIT) is supported, the VM needs to do JIT related initialization, including initializing the compilation policy, compiler back end, code cache, etc.
Initializing Some System Classes
Some system classes that are ROMized need to be initialized explicitly by the VM. First, it initializes those system classes that don't require thread support by executing their static initializers. These include java.lang.Class
, java.lang.String
, java.lang.Shutdown
, java.lang.ClassLoader
, etc. Then it creates the system ThreadGroup
, main ThreadGroup
, and main Thread
objects. After the thread initialization, the VM calls System.initializeSystemClass() to set up system properties, stdin
, stdout
, and stderr
. During that, it also creates:
- A Reference handler thread, which enqueues pending References.
- A Finalizer thread that runs the finalizer.
Parse Arguments
The VM calls sun.misc.CVM.parseCommandLineOptions()
to parse the rest of the arguments. During which it adds user defined properties, and finds the main class name.
Now CVM is ready. To execute the main()
method, it needs to find the main Java class first. To do that it creates the system classloader (sun.misc.Launcher$AppClassLoader
) and uses the classloader to search and load the main class. After the class is loaded successfully, it invokes the main()
method and starts executing its Java code.
JNI文档:http://www.iplab.cs.tsukuba.ac.jp/liuxj/jdk1.2/zh/docs/
- CVM启动和初始化过程
- PhoneMe Advanced 代码浅读一----CVM启动和初始化过程
- CVM虚拟机的启动过程
- flex启动过程和初始化
- 多核初始化和启动过程
- Linux 启动过程, 初始化和关闭
- Nginx 启动初始化过程
- μC/OS-II的初始化和启动过程
- Bootloader初始化启动过程分析
- 数据库启动初始化过程探讨
- nginx启动初始化过程(二)
- CVM栈和代码的执行
- PostgreSQL启动过程中的那些事七:初始化共享内存和信号一:初始化shmemIndex和信号
- PostgreSQL启动过程中的那些事七:初始化共享内存和信号二:shmem中初始化xlog
- PostgreSQL启动过程中的那些事七:初始化共享内存和信号三:shmem中初始化clog
- PostgreSQL启动过程中的那些事七:初始化共享内存和信号:四 shmem中初始化subtrans
- PostgreSQL启动过程中的那些事七:初始化共享内存和信号五:shmem中初始化multixact 编辑
- PostgreSQL启动过程中的那些事七:初始化共享内存和信号六:shmem中初始化twophase
- 模板设计模式(Template Method)和窄接口 的应用1例
- 使用openssl库实现des&amp;&amp;3des加密
- 软件存在的价值
- Lesson1: A Private Conversation
- delphi 7下安装dspack
- CVM启动和初始化过程
- 当个适任主管的关键
- Java中Timer,TimerTask的使用
- vb.net里如何使用SQL存储过程- -
- 通过sql*plus,使用sys登录数据库
- 一个电脑白痴和黑客的对话
- Linux进程编程介绍(一)
- 梅花雪Web Calendar ver 3.0 网页日历在asp.net 2.0的应用
- 工作情况