java之Compiler control

本文探讨了Java虚拟机(JVM)的C1和C2编译模式,以及如何通过方法依赖的编译器标志来控制它们。文章详细介绍了快速启动、优化增强的客户端侧C1模式,以及适用于长时间运行、服务器侧应用的C2模式。同时,介绍了分层编译的概念,并展示了如何在Java 11中使用指令文件来动态调整编译器操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Controlling Java Virtual Machine (JVM) compilers might seem like an unnecessary task,
but for many developers, this is an important aspect of testing. This is accomplished with
method-dependent compiler flags.
In this section, we will start with a look at JVM compilation modes and then look at
the compiler that can be controlled using the Java platform.

Compilation modes


The changes in the modern Java platform include granular control of both the C1 and C2
JVM compilers. As you can see in the following illustration, the Java HotSpot JVM has two
JIT compilation modes C1 and C2:

 

The C1 and C2 compilation modes use different compilation techniques and, if used on
the same code base, can produce different sets of machine code.

 

The C1 compilation mode


The C1 compilation mode inside the Java HotSpot VM is typically used for applications
that have the following characteristics:

  • Quick startup
  • Increased optimization
  • Client-side

The C2 compilation mode


The second compilation mode, C2, is used by applications with the following
listed characteristics:

  • Long runtimes
  • Server-side

 

Tiered compilation


Tiered compilation allows us to use both C1 and C2 compilation modes. Starting with Java
8, tiered compilation is the default process. As illustrated here, C1 mode is used at
startup to help provide greater optimization. Then, once the app has sufficiently warmed
up, C2 mode is employed:

 

Compiler control in Java 11

 

Java comes with the promise of the ability to have finite control over JVM compilers and to
make changes at runtime. These additional abilities do not degrade performance.
This permits greater fidelity of testing and testing optimization as we can run small
compiler tests without having to relaunch the entire JVM.
To control compiler operations, we need to create a directives file. These files
contain compiler directives that consist of a set of options with values. Directive files
essentially use a subset of JSON:

 

The JavaScript Object Notation (JSON) format is used for data-interchange. The
directive files have the following formatting differences from JSON:

  • int and doubles are the only supported number formats
  • The double forward slash (//) can be used for comment lines
  • Trailing commas (,) can be used in arrays and objects
  • Escape characters are not supported
    Option names are formatted as strings and do not have to be quoted

You can learn more about JSON at http://www.json.org

 

We can add our directive file using the following syntax at the command line:
            -XX:CompilerDirectivesFile=<file>
Here is a shell example of a directives file:

[ // Open square bracket marks the start of the directives file
	{ // Open curly brace marks the start of a directive block
		// A directives block that applies specifically to the C1 mode
		c1: {
			// directives go here
		},
		// A directives block that applies specifically to the C2 mode
		c2: {
			// directives go here
		},
		// Here we can put a directives that do not apply to
		// a specific compiler mode
	},
	{ // can have multiple directive blocks
		c1: {
			// directives go here
		}
		c2: {
			// directives go here
		}
	}
] // Close square bracket marks the start of the directives file

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值