当前位置:网站首页>vm options、program arguments、environment property
vm options、program arguments、environment property
2022-07-29 01:21:00 【MrMoving】
System variables (system property)
yes java Variables specified by the application itself , Usually we can specify , There are two ways to specify
Input java -jar -help Take a look back. java start-up jar The order of the document
java [-options] -jar jarfile [args…]
One option options It includes -Dkey=value, Corresponding vm options Standard parameters in
among args It is a custom parameter , Corresponding program arguments
The former can be passed directly System.getProperty(key) Get , The latter is passed in as text main Function , Further processing is required before using , Here is a further introduction to
vm options
VM options : JVM Virtual machine startup command options , Here you can set some operating parameters .
Parameters can be roughly divided into Standards (-D etc. ) And nonstandard (-X、-XX) Two kinds of , Non standard options are not guaranteed to be implemented on all platforms , And it may be modified in future versions without informing , In short, it is unstable (Unstable). However, some non-standard options are very useful .( There is no detailed introduction here , Refer to another article )
Set up VM When parameters are , If we need to customize some parameter information ( This behavior is called , Set system properties ), Must be used -Dkey=value This form , Multiple sets of parameters are separated by spaces
Through the parameters set here ,JVM At initialization , Would be right System Medium Properties Make corresponding assignment , So we can also pass System.getProperty(String key) Get ( Limited to -D The key value of form can be obtained for standard parameters , Other formats and non-standard parameters cannot be read ).
Examples of non-standard parameters
-Xms1024m, Set up JVM The initial heap memory is 1024m. This value can be set with -Xmx identical , To avoid every garbage collection after completion JVM Reallocate memory . -Xmx1024m, Set up JVM The maximum heap memory is 1024m. -Xss512k, Set the stack size of each thread .JDK5.0 After that, the stack size of each thread is 1M, Before that, the stack size of each thread was 256K. In the same physical memory , Reducing this value can generate more threads , Of course, the operating system has a limit on the number of threads in a process , Can't generate... Indefinitely . The size of thread stack is a double-edged sword , If the setting is too small , There may be stack overflow , Especially in this thread there is recursion 、 There is a greater chance of spillover in a large cycle , If the value is set too high , It affects the number of stacks created , If it's a multithreaded application , There will be a memory overflow error . -Xmn341m, Set the size of the younger generation to 341m. With the entire heap memory size determined , Increasing the younger generation will reduce the older generation , vice versa . This value is related to JVM Garbage collection , It has a great impact on the system performance , The official recommendation is to configure the 3/8. -XX:NewSize=341m, Set the initial value of the young generation to 341M. -XX:MaxNewSize=341m, Set the maximum value of young generation to 341M. -XX:PermSize=512m, Set the persistent generation initial value to 512M, But in java8 And then it won't support , change to the use of sth. -XX:MetaspaceSize=512m. -XX:MaxPermSize=512m, Set the maximum persistent generation to 512M, Also in java8 And then it won't support , change to the use of sth. -XX:MaxMetaspaceSize=512m. -XX:NewRatio=2, Set up younger generation ( Include 1 individual Eden and 2 individual Survivor District ) The ratio to the old . It means that the younger generation is more than the older generation 1:2. -XX:SurvivorRatio=8, Set up a young generation Eden District and Survivor Area ratio . Express 2 individual Survivor District (JVM The default heap memory in the younger generation is 2 Equal size Survivor District ) And 1 individual Eden The ratio of regions is 1:1:8, namely 1 individual Survivor The area is the size of the whole young generation 1/10. -XX:MaxTenuringThreshold=15, Please refer to JVM Aging process of objects in a series of memory allocation and recycling strategies . -XX:ReservedCodeCacheSize=256m, Set the size of the code cache , Used to store native code generated by compiled methods .
program arguments
The parameter set here is as main Method parameters String[] args Incoming
-Dkey=value It belongs to automatic setting of environment variables , Fixed format , There are many limitations
program arguments Parameters in , With a lot of flexibility , You can customize the code to parse ,
Here is a brief introduction to spring Some attribute parsing codes in CommandLinePropertySource
CommandLinePropertySource
When we started the project , Yes, you can specify command line parameters Of , Such as :
This class defines command line parameters as two :
option arguments: The designation method is usually as follows --k=v
non-option arguments: Not in the form of – Attributes decorated with prefixes like , Is interpreted as non-option arguments, Such as a b c It will be resolved to a,b,c, Its key by nonOptionArgs, Of course we can pass it setNonOptionArgsPropertyName Method to modify the key value
Spring in the light of CommandLinePropertySource Two implementations are provided ( Of course, we can do it ourselves ), Respectively SimpleCommandLinePropertySource and JOptCommandLinePropertySource, Basically, what we use is SimpleCommandLinePropertySource 了
SimpleCommandLinePropertySource
In this class , Command line arguments Is interpreted as CommandLineArgs, The class responsible for parsing is SimpleCommandLineArgsParser, See its construction method :
public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> {
public SimpleCommandLinePropertySource(String... args) {
super(new SimpleCommandLineArgsParser().parse(args));
}
}
Property to get the test code
public void command() {
String[] command = {
"--o1=v1", "--o2", "a", "b", "c"};
SimpleCommandLinePropertySource propertySource
= new SimpleCommandLinePropertySource("command", command);
// v1
String o1 = propertySource.getProperty("o1");
// ""
String o2 = propertySource.getProperty("o2");
// null
String o3 = propertySource.getProperty("o3");
// "a,b,c"
String nonOptionArgs = propertySource.getProperty("nonOptionArgs");
}
The practical application , take args Resolve to system variables
public void source(String... args) {
simpleCommandLinePropertySource = new SimpleCommandLinePropertySource(args);
Set<String> stringSet = simpleCommandLinePropertySource.getSource().getOptionNames();
if (CollectionUtils.isNotEmpty(stringSet)) {
for (String key : stringSet) {
String values = simpleCommandLinePropertySource.getProperty(key);
if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(values)) {
String oldValue = System.setProperty(key, values);
LOGGER.info(" Set the system environment variable to :{}={} The original value was zero :{}", key, values, oldValue);
}
}
}
}
environment variable (environment property)
Can pass idea Start parameter settings for
stay spring In the project , It can also be done by @value The annotation gets environment property, But if there is one with the same name system property, The latter will be read
in addition ,spirng In the project , Defines its own set of parsing Program arguments Rules for parameters , If there is one that conforms to the format Program arguments,
Such as –serverId=8080, This parameter will be parsed into the corresponding system property, And its priority is greater than VM options Property with the same name in
All in all , stay spring In the project , Priority of the three
Program arguments (–priority=program-agrs) >
VM options (-Dpriority=vm-options) >
Environment variable (priority=environment-variables)
in addition , All three of them have higher priority than external configuration files , Refer to the link below for this point
The environment variables here are not system level environment variables , It should be for this process only , adopt System.getEnv(key) Can get
public static void main(String[] args) {
Map<String, String> env = System.getenv();
env.entrySet().forEach(System.out::println);
}
Extended article
springboot Environment Properties Loading details
https://blog.csdn.net/qq_31086797/article/details/107395108?
spring How to put environment The attribute values in use @Value Injected into the field
https://blog.csdn.net/qq_31086797/article/details/124136140
边栏推荐
- Prometheus 的 API 稳定性保障
- Google play APK uploads other international app stores
- Consumer unit
- Google Play APK 上传其他国际应用商店
- Principle and usage setting of large page memory
- [target detection] Introduction to yolor theory + practical test visdrone data set
- 教你一文解决 js 数字精度丢失问题
- Django使用MySQL数据库已经存在的数据表方法
- Flink Postgres CDC
- Textkit custom uilabel identification link
猜你喜欢

18张图,直观理解神经网络、流形和拓扑

表达式求值

Error reporting: SQL syntax error in flask. Fields in SQL statements need quotation marks when formatting

测试/开发程序员靠技术渡过中年危机?提升自己本身的价值......

How to deal with the time, scope and cost constraints in the project?

mysql分表之后怎么平滑上线?

递归与分治

Day2:三种语言暴刷牛客130题

20220728-不纯为数字的字符串排序

ThinkPHP high imitation blue cloud disk system program
随机推荐
【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
Univariate function integration 1__ Indefinite integral
Synchronized关键字详解
Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
大页内存原理及使用设置
一元函数积分学之1__不定积分
A new generation of ultra safe cellular battery, Sihao aipao, is on the market, starting from 139900
C语言300行代码实现扫雷(可展开+可标记+可更改困难级别)
Dart array, map, type judgment, conditional judgment operator, type conversion
括号匹配的检验
【刷题笔记】从链表中删去总和值为零的连续节点
Consumer unit
ActiveMQ基本详解
[idea] where to use the query field
写作作业一
nep 2022 cat
Linux Redis 源码安装
进程和线程知识点总结1
Summary of process and thread knowledge points 2
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description