当前位置:网站首页>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
边栏推荐
- 如何处理项目中的时间、范围和成本限制?
- RHCE命令练习(一)
- 如何在WordPress中创建一个自定义404错误页面
- Bracket matching test
- Dart array, map, type judgment, conditional judgment operator, type conversion
- Transfer: cognitive subculture
- Synchronized关键字详解
- 【刷题笔记】二进制链表转整数
- Closures and decorators
- Self-attention neural architecture search for semantic image segmentation
猜你喜欢
New pseudo personal guide page source code
C语言300行代码实现扫雷(可展开+可标记+可更改困难级别)
How to carry out engineering implementation of DDD Domain Driven Design
How to create a custom 404 error page in WordPress
Seven SQL performance optimizations that spark 3.0 must know
Beginner's Guide to electronic bidding
20220728 sorting strings that are not pure numbers
[web development] basic knowledge of flask framework
Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
大页内存原理及使用设置
随机推荐
TextKit 自定义UILabel识别链接
Y80. Chapter 4 Prometheus big factory monitoring system and practice -- Kube state metrics component introduction and monitoring extension (XI)
转:认知亚文化
Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
[unity] configure unity edit C as vscode
【unity】将unity编辑c#配置为vscode
Date conversion EEE MMM DD hh:mm:ss zzz YYYY
面试官:程序员,请你告诉我是谁把公司面试题泄露给你的?
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
Oozie job scheduling
uniapp createSelectorQuery().select 获取返回null报错
正则表达式
system verilog常用语法
solidity实现智能合约教程(5)-NFT拍卖合约
QT静态编译程序(Mingw编译)
MySQL stored procedure realizes the creation of a table (copy the structure of the original table and create a new table)
[idea] where to use the query field
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
SDRAM Controller Design (two design methods of digital controller)
Hilbert 变换与瞬时频率