jvm.option是什么,它是如何加载的
jvm.option是一些程序里边的java的配置参数的一个集合,不同的应用都会定义自己的jvm.options用来控制一些jvm的参数
以下,以elasticsearch为例,来说明它是如何加载的
elasticsearch的jvm.options的文件内容如下:
## JVM configuration
################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space
-Xms${heap.min}
-Xmx${heap.max}
################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################
## GC configuration
-XX:+UseConcMarkSweepGC
-XX:CMSInitiatingOccupancyFraction=75
-XX:+UseCMSInitiatingOccupancyOnly
## optimizations
# pre-touch memory pages used by the JVM during initialization
-XX:+AlwaysPreTouch
## basic
# explicitly set the stack size
-Xss1m
# set to headless, just in case
-Djava.awt.headless=true
# ensure UTF-8 encoding by default (e.g. filenames)
-Dfile.encoding=UTF-8
# use our provided JNA always versus the system one
-Djna.nosys=true
# turn off a JDK optimization that throws away stack traces for common
# exceptions because stack traces are important for debugging
-XX:-OmitStackTraceInFastThrow
# flags to configure Netty
-Dio.netty.noUnsafe=true
-Dio.netty.noKeySetOptimization=true
-Dio.netty.recycler.maxCapacityPerThread=0
# log4j 2
-Dlog4j.shutdownHookEnabled=false
-Dlog4j2.disable.jmx=true
-Djava.io.tmpdir=${ES_TMPDIR}
## heap dumps
# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError
# specify an alternative path for heap dumps; ensure the directory exists and
# has sufficient space
${heap.dump.path}
# specify an alternative path for JVM fatal error logs
${error.file}
## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:${loggc}
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m
# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=${loggc}:utctime,pid,tags:filecount=32,filesize=64m
# due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise
# time/date parsing will break in an incompatible way for some date patterns and locals
9-:-Djava.locale.providers=COMPAT
# temporary workaround for C2 bug with JDK 10 on hardware with AVX-512
10-:-XX:UseAVX=2
那在ES里边,是如何设置这个参数的呢
我们手动运行这个java类,会得到如下的结果:
所以,ES提供了一个JvmOptionsParser类,来解析jvm.options里设置的jvm参数,然后在应用启动的时候,把这些参数设置到应用的启动参数里边去,我们也可以参考这个思路,提供一个自己的jvm.options文件,然后写一个Parser类来解析里边的内容,然后设置到jvm的启动参数里边去
以下是ES的JvmOptionsParser类的实现
package org.elasticsearch.tools.launchers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.elasticsearch.tools.java_version_checker.JavaVersion;
/**
* Parses JVM options from a file and prints a single line with all JVM options to standard output.
*/
final class JvmOptionsParser {
/**
* The main entry point. The exit code is 0 if the JVM options were successfully parsed, otherwise the exit code is 1. If an improperly
* formatted line is discovered, the line is output to standard error.
*
* @param args the args to the program which should consist of a single option, the path to the JVM options
*/
public static void main(final String[] args) throws IOException {
if (args.length != 1) {
throw new IllegalArgumentException("expected one argument specifying path to jvm.options but was " + Arrays.toString(args));
}
final List<String> jvmOptions = new ArrayList<>();
final SortedMap<Integer, String> invalidLines = new TreeMap<>();
try (InputStream is = Files.newInputStream(Paths.get(args[0]));
Reader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(reader)) {
parse(
JavaVersion.majorVersion(JavaVersion.CURRENT),
br,
new JvmOptionConsumer() {
@Override
public void accept(final String jvmOption) {
jvmOptions.add(jvmOption);
}
},
new InvalidLineConsumer() {
@Override
public void accept(final int lineNumber, final String line) {
invalidLines.put(lineNumber, line);
}
});
}
if (invalidLines.isEmpty()) {
List<String> ergonomicJvmOptions = JvmErgonomics.choose(jvmOptions);
jvmOptions.addAll(ergonomicJvmOptions);
final String spaceDelimitedJvmOptions = spaceDelimitJvmOptions(jvmOptions);
Launchers.outPrintln(spaceDelimitedJvmOptions);
Launchers.exit(0);
} else {
final String errorMessage = String.format(
Locale.ROOT,
"encountered [%d] error%s parsing [%s]",
invalidLines.size(),
invalidLines.size() == 1 ? "" : "s",
args[0]);
Launchers.errPrintln(errorMessage);
int count = 0;
for (final Map.Entry<Integer, String> entry : invalidLines.entrySet()) {
count++;
final String message = String.format(
Locale.ROOT,
"[%d]: encountered improperly formatted JVM option line [%s] on line number [%d]",
count,
entry.getValue(),
entry.getKey());
Launchers.errPrintln(message);
}
Launchers.exit(1);
}
}
/**
* Callback for valid JVM options.
*/
interface JvmOptionConsumer {
/**
* Invoked when a line in the JVM options file matches the specified syntax and the specified major version.
* @param jvmOption the matching JVM option
*/
void accept(String jvmOption);
}
/**
* Callback for invalid lines in the JVM options.
*/
interface InvalidLineConsumer {
/**
* Invoked when a line in the JVM options does not match the specified syntax.
*/
void accept(int lineNumber, String line);
}
private static final Pattern PATTERN = Pattern.compile("((?<start>\\d+)(?<range>-)?(?<end>\\d+)?:)?(?<option>-.*)$");
/**
* Parse the line-delimited JVM options from the specified buffered reader for the specified Java major version.
* Valid JVM options are:
* <ul>
* <li>
* a line starting with a dash is treated as a JVM option that applies to all versions
* </li>
* <li>
* a line starting with a number followed by a colon is treated as a JVM option that applies to the matching Java major version
* only
* </li>
* <li>
* a line starting with a number followed by a dash followed by a colon is treated as a JVM option that applies to the matching
* Java specified major version and all larger Java major versions
* </li>
* <li>
* a line starting with a number followed by a dash followed by a number followed by a colon is treated as a JVM option that
* applies to the specified range of matching Java major versions
* </li>
* </ul>
*
* For example, if the specified Java major version is 8, the following JVM options will be accepted:
* <ul>
* <li>
* {@code -XX:+PrintGCDateStamps}
* </li>
* <li>
* {@code 8:-XX:+PrintGCDateStamps}
* </li>
* <li>
* {@code 8-:-XX:+PrintGCDateStamps}
* </li>
* <li>
* {@code 7-8:-XX:+PrintGCDateStamps}
* </li>
* </ul>
* and the following JVM options will not be accepted:
* <ul>
* <li>
* {@code 9:-Xlog:age*=trace,gc*,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m}
* </li>
* <li>
* {@code 9-:-Xlog:age*=trace,gc*,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m}
* </li>
* <li>
* {@code 9-10:-Xlog:age*=trace,gc*,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m}
* </li>
* </ul>
*
* If the version syntax specified on a line matches the specified JVM options, the JVM option callback will be invoked with the JVM
* option. If the line does not match the specified syntax for the JVM options, the invalid line callback will be invoked with the
* contents of the entire line.
*
* @param javaMajorVersion the Java major version to match JVM options against
* @param br the buffered reader to read line-delimited JVM options from
* @param jvmOptionConsumer the callback that accepts matching JVM options
* @param invalidLineConsumer a callback that accepts invalid JVM options
* @throws IOException if an I/O exception occurs reading from the buffered reader
*/
static void parse(
final int javaMajorVersion,
final BufferedReader br,
final JvmOptionConsumer jvmOptionConsumer,
final InvalidLineConsumer invalidLineConsumer) throws IOException {
int lineNumber = 0;
while (true) {
final String line = br.readLine();
lineNumber++;
if (line == null) {
break;
}
if (line.startsWith("#")) {
// lines beginning with "#" are treated as comments
continue;
}
if (line.matches("\\s*")) {
// skip blank lines
continue;
}
final Matcher matcher = PATTERN.matcher(line);
if (matcher.matches()) {
final String start = matcher.group("start");
final String end = matcher.group("end");
if (start == null) {
// no range present, unconditionally apply the JVM option
jvmOptionConsumer.accept(line);
} else {
final int lower;
try {
lower = Integer.parseInt(start);
} catch (final NumberFormatException e) {
invalidLineConsumer.accept(lineNumber, line);
continue;
}
final int upper;
if (matcher.group("range") == null) {
// no range is present, apply the JVM option to the specified major version only
upper = lower;
} else if (end == null) {
// a range of the form \\d+- is present, apply the JVM option to all major versions larger than the specified one
upper = Integer.MAX_VALUE;
} else {
// a range of the form \\d+-\\d+ is present, apply the JVM option to the specified range of major versions
try {
upper = Integer.parseInt(end);
} catch (final NumberFormatException e) {
invalidLineConsumer.accept(lineNumber, line);
continue;
}
if (upper < lower) {
invalidLineConsumer.accept(lineNumber, line);
continue;
}
}
if (lower <= javaMajorVersion && javaMajorVersion <= upper) {
jvmOptionConsumer.accept(matcher.group("option"));
}
}
} else {
invalidLineConsumer.accept(lineNumber, line);
}
}
}
/**
* Delimits the specified JVM options by spaces.
*
* @param jvmOptions the JVM options
* @return a single-line string containing the specified JVM options in the order they appear delimited by spaces
*/
static String spaceDelimitJvmOptions(final List<String> jvmOptions) {
final StringBuilder spaceDelimitedJvmOptionsBuilder = new StringBuilder();
final Iterator<String> it = jvmOptions.iterator();
while (it.hasNext()) {
spaceDelimitedJvmOptionsBuilder.append(it.next());
if (it.hasNext()) {
spaceDelimitedJvmOptionsBuilder.append(" ");
}
}
return spaceDelimitedJvmOptionsBuilder.toString();
}
}
jvm.option是什么,它是如何加载的的更多相关文章
- JVM规范系列第5章:加载、链接与初始化
加载是根据特定名称查找类或接口类型的二进制表示(Binary Representation),并由此二进制表示创建类或接口的过程. 加载,就是指去寻找类或接口的过程. 链接是为了让类或接口可以被 Ja ...
- jvm系列一、java类的加载机制
一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构 ...
- jvm(1)类加载(一)(加载过程,双亲加载)
JVM类加载器机制与类加载过程 jvm虚拟机的种类: Hotspot(Oracle)(基本上都是在说这个) J9, JikesRVM(IBM) Zulu, Zing (Azul) Launcher是一 ...
- jvm学习笔记之class文件的加载、初始化
编写的java文件在要真正运行时,会首先被编译成 “.class"结尾的二进制文件,然后被虚拟机加载.那么在虚拟机中一个class文件要成为java实例,需要经历好几个步骤: 1.装载:装载 ...
- JVM系列【3】Class文件加载过程
JVM系列笔记目录 虚拟机的基础概念 class文件结构 class文件加载过程 jvm内存模型 JVM常用指令 GC与调优 Class文件加载过程 JVM加载Class文件主要分3个过程:Loadi ...
- 深入了解java虚拟机(JVM) 第十一章 类的加载
一.类加载机制概述 虚拟机把描述类的数据从class文件加载到内存并对数据进行效验,解析和初始化,最终形成可以被虚拟机直接使用的java类型,这就是虚拟机的类加载机制. 二.类加载的机制 类加载的过程 ...
- jvm中加载类的全过程
ClassLoader的作用:概括来说就是将编译后的class装载.加载到机器内存中,为了以后的程序的执行提供前提条件. jvm的整个生命周期,如下图所示 加载=>验证=>准备=>解 ...
- 【基本功】Java动态追踪技术探究 不重启JVM,替换掉已经加载的类?不重启JVM,获知运行时对象的属性
https://mp.weixin.qq.com/s/_hSaI5yMvPTWxvFgl-UItA 小结: 1.根据Java的类加载机制,在同一个ClassLoader中,类是不允许重复的: 2.单例 ...
- JVM自定义类加载器加载指定classPath下的所有class及jar
一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...
随机推荐
- JS_高程5.引用类型(5)Array类型的操作方法
一.操作方法 1.concat()方法 基于当前数组中的所有项创建一个新数组.具体说,是先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组.在没有给concat() ...
- pygame 笔记-3 角色动画及背景的使用
上二节,已经知道如何控制基本的运动了,但是只有一个很单调的方块,不太美观,本节学习如何加载背景图,以及角色的动画. 素材准备:(原自github) 角色动画的原理:动画都是一帧帧渲染的,比如向左走的动 ...
- python测试开发django-50.jquery发送ajax请求(get)
前言 有时候,我们希望点击页面上的某个按钮后,不刷新整个页面,给后台发送一个请求过去,请求到数据后填充到html上,这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新.Ajax可以完美的 ...
- Mac本地环境配置以及安装织梦CMS,增加新的坑解决办法
Mac上其实已经自带了Apache和PHP,只是默认关闭的.开启一下就行了. Apache配置 apache已经自带了,只需在“终端”输入命令开启下就行了. 开启apache服务 sudo ap ...
- 转: 关于CAS cpu锁的技术说明。
写的不错.原文链接如下: https://blog.csdn.net/dviewer/article/details/51943140 部分摘要: 2016年07月18日 16:30:08 阅读数:3 ...
- SSE图像算法优化系列二十四: 基于形态学的图像后期抗锯齿算法--MLAA优化研究。
偶尔看到这样的一个算法,觉得还是蛮有意思的,花了将近10天多的时间研究了下相关代码. 以下为百度的结果:MLAA全称Morphological Antialiasing,意为形态抗锯齿是AMD推出的完 ...
- masstree Seastar
masstree Seastar 线程锁竞争和切换的开销几乎为0,代码也不用考虑多线程竞争,逻辑大大减化:此外Niagara是一个全异步执行引擎,采用了基于future,promise和contin ...
- 查看SQL Server的版本及License
select @@VERSION Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64) Jun 28 2012 08:36:30 Copyri ...
- xcode10 改动
xcode10 开发环境 比 之前有了稍微的变动 1. 代码块 界面控件 图片资源等 的查看位置发生了变化 之前的开发环境 代码块 统一放在 右侧栏的下方的几个选项中 现在 统一放到了上方 ...
- [转]MPP架构
数据库构架设计中主要有Shared Everthting.Shared Nothing.和Shared Disk: Shared Everthting:一般是针对单个主机,完全透明共享CPU/MEMO ...