当前位置:网站首页>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
边栏推荐
- 【刷题笔记】从链表中删去总和值为零的连续节点
- 如何执行建设项目的时间影响分析?
- (perfect solution) why is the effect of using train mode on the train/val/test dataset good, but it is all very poor in Eval mode
- 正则表达式
- ActiveMQ basic details
- Closures and decorators
- MySQL time is formatted by hour_ MySQL time format, MySQL statement queried by time period [easy to understand]
- Transfer: cognitive subculture
- log4j动态加载配置文件
- How to carry out engineering implementation of DDD Domain Driven Design
猜你喜欢

How to implement the time impact analysis of the construction project?

表达式求值

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!

Canal实时解析mysql binlog数据实战

Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers

Google Play APK 上传其他国际应用商店

大页内存原理及使用设置

Thread lock and its ascending and descending levels

ThinkPHP high imitation blue cloud disk system program

Linux Redis 源码安装
随机推荐
[notes for question brushing] delete continuous nodes with a total value of zero from the linked list
[ManageEngine] help Harbin Engineering University realize integrated monitoring and management of network traffic
RHCE命令练习(一)
How to carry out engineering implementation of DDD Domain Driven Design
正则校验与时间格式化
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
Textkit custom uilabel identification link
IT硬件故障的主要原因和预防的最佳实践
Day2:三种语言暴刷牛客130题
Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
Oozie工作调度
Classification prediction | MATLAB realizes time series classification prediction of TCN time convolution neural network
[notes for question brushing] binary linked list to integer
Recursion and divide and conquer
北京护照西班牙语翻译推荐
18张图,直观理解神经网络、流形和拓扑
Canal real-time parsing MySQL binlog data practice
How to implement the time impact analysis of the construction project?
【ManageEngine】局域网监控软件是什么,有什么作用
Interviewer: programmer, please tell me who leaked the company interview questions to you?