当前位置:网站首页>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
边栏推荐
- Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
- Return the member function of *this
- 教你一文解决 js 数字精度丢失问题
- Canal实时解析mysql binlog数据实战
- 数字孪生轨道交通:“智慧化”监控疏通城市运行痛点
- Solid smart contract tutorial (5) -nft auction contract
- (update 20211130) about the download and installation of Jupiter notebook and its own configuration and theme
- C language bracket matching (stack bracket matching C language)
- Cookie和Session
- 【刷题笔记】二进制链表转整数
猜你喜欢

Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network

如何在WordPress中创建一个自定义404错误页面

Day2: 130 questions in three languages

Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template

QT static compiler (MinGW compilation)

新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售

Introduction to FLV documents

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

Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network

Visual full link log tracking
随机推荐
SDRAM Controller Design (two design methods of digital controller)
uniapp createSelectorQuery().select 获取返回null报错
solidity实现智能合约教程(5)-NFT拍卖合约
[target detection] Introduction to yolor theory + practical test visdrone data set
【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
18张图,直观理解神经网络、流形和拓扑
Interviewer: programmer, please tell me who leaked the company interview questions to you?
Consumer unit
RHCE命令练习(二)
可视化全链路日志追踪
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
Canal实时解析mysql binlog数据实战
如何执行建设项目的时间影响分析?
nep 2022 cat
How to deal with the time, scope and cost constraints in the project?
ActiveMQ基本详解
Digital twin rail transit: "intelligent" monitoring to clear the pain points of urban operation
时间复杂度、空间复杂度的学习总结
SystemVerilog join and copy operators
表达式求值