Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of vm parameters commonly used in Java Virtual Machine

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you the "sample analysis of commonly used vm parameters in the Java virtual machine", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of commonly used vm parameters in the Java virtual machine".

1. Brief introduction of JVM parameters

First of all, I would like to say that in fact, we are not unfamiliar with these parameters, which are often encountered in peacetime development and use, but lack a more systematic summary at ordinary times, so it feels very strange to these parameters, so, through the summary of this article, I believe you will be familiar with these parameters and know what to do.

In fact, among the parameters of the Java virtual machine, these parameters can be divided into three categories, of course, this is for JDK1.6, if for JDK1.8, then it is not so classified, but because there is little difference between the two versions of many commonly used parameters, so this article will first introduce the VM parameters of JDK1.6.

It can be divided into the following three categories:

Standard parameters (-), all JVM implementations must implement the functionality of these parameters and be backward compatible.

Non-standard parameters (- X), the default JVM implements the functionality of these parameters, but there is no guarantee that all JVM implementations are met, and backward compatibility is not guaranteed.

Non-Stable parameter (- XX), which varies from JVM implementation to JVM implementation and may be canceled at any time in the future and needs to be used with caution.

Although it is classified in this way, in fact, non-standard parameters and unstable parameters are often used in practice, as you will find in the following article.

2 standard parameters

It can be said that we used a lot of parameters as soon as we started Java, such as java-version, java-jar and so on. We can get all the standard parameters of the current version of Java by typing java-help in CMD.

As the figure above is all the standard parameters of JDK1.8, we will introduce some of the parameters that we will use more.

-client

Start JVM in client mode, which starts quickly, but the runtime performance and memory management efficiency are not high, so it is suitable for client programs or development and debugging.

-server

Starting JVM in server mode is the opposite of client. Suitable for production environment, suitable for servers. The 64-bit JVM automatically starts in server mode.

-classpath or-cp

Notify the JVM class search path. If-classpath is specified, JVM ignores the path specified in CLASSPATH. The paths are separated by semicolons. If neither-classpath nor CLASSPATH is specified, JVM looks for class from the current path.

Order of JVM search paths:

1. First search the jar or zip packages that come with JVM.

Bootstrap, the search path can be obtained using System.getProperty ("sun.boot.class.path")

two。 Search the jar package under JRE_HOME/lib/ext.

Extension, the search path can be obtained using System.getProperty ("java.ext.dirs")

3. Search for user-defined directories in the following order: current directory (.), CLASSPATH,-cp.

The search path is obtained using System.getProperty ("java.class.path").

System.out.println (System.getProperty ("sun.boot.class.path")); System.out.println (System.getProperty ("java.ext.dirs")); System.out.println (System.getProperty ("java.class.path"))

This is the path to the JVM of my computer.

-DpropertyName=value

Define the global attribute values of the system, such as the profile address, etc., and use double quotation marks if the value has spaces.

In addition, the property values of these definitions can be obtained with System.getProperty ("hello"), and attributes can also be defined in the form of System.setProperty ("hello", "world") in the code.

For example, the key value pair is set to hello=world.

System.out.println (System.getProperty ("hello"))

The result of the operation is:

-verbose

One of the most common commands for querying GC questions. The parameters are as follows:

-verbose:class

Output information about JVM loading classes, which can be diagnosed when JVM reports that the class cannot be found or that there is a class conflict.

-verbose:gc

Output the relevant information of each GC.

-verbose:jni

Output information about native method calls, which is generally used to diagnose jni call error messages.

In addition, you can use the following command to output GC information from the console:

-XX:+PrintGC-XX:+PrintGCDetails-XX:+PrintGCTimeStamps-XX:+PrintGCApplicationStoppedTime is added to the startup parameters of JVM, and the brief information of GC, the details of GC, the time information of GC and the time of application suspension caused by GC are output respectively according to the order of parameters.

3 non-standard parameters

Non-annotated parameters are mainly about setting parameters for Java memory areas, so before looking at these parameters, you should check out the basics of Java memory regions. You can check out this article: in-depth understanding of the Java virtual machine-Java memory region thorough analysis.

Non-standard parameters are some extended parameters based on the standard parameters. You can input java-X to obtain the non-standard parameters supported by the current JVM.

As you can see from the picture, there are not many of these non-standard parameters. Let's explain some of the more commonly used parameters.

-Xmn

The maximum memory size of the Cenozoic era, including the sum of E and two S regions. Setting method:-Xmn512m,-Xmn2g.

-Xms

The initial heap size, which is also the minimum heap size, defaults to total physical memory / 64 (and less than 1G). By default, when the available memory in the heap is less than 40%, the heap memory starts to grow to the size of-Xmx.

-Xmx

The maximum value of the heap. The default value is total physical memory / 64 (and less than 1G). By default, when the available memory in the heap is greater than 70%, the heap memory begins to decrease to the size of-Xms.

Therefore, in order to avoid such floats, the-Xms and-Xmx parameters are generally set to the same, which can improve performance.

In addition, the official default configuration is the size of the old generation: the size of the young generation is around 2:1. You can use-XX:NewRatio to set the ratio of the older generation to the younger generation. For example,-XX:NewRatio=4 means the old generation: the young generation = 4:1.

Parameter instance

Set the-Xms,-Xmn, and-Xmx parameters to-Xms512m-Xmx512m-Xmn128m, respectively. At the same time, the ratio of the new generation to the old generation is set as 1: 1: 1: 1: 1: 1: 1: 1: 1: 1: 1: 1.

* * @ ClassName MethodTest * @ Description vm parameter setting:-Xms512m-Xmx512m-Xmn128m-XX:NewRatio=4-XX:SurvivorRatio=8 * @ Author Ouyang Sihai * @ Date 20:06 on 2019-11-25 * @ Version 1.0 * * / public class MethodTest {public static void main (String [] args) {List list = new ArrayList (); long I = 0; while (I < 1000000000) {System.out.println (I) List.add (String.valueOf (iTunes +). Intern ();}

After running, use VisualVM to see if the relevant information is correct.

When we do not set-XX:NewRatio=4-XX:SurvivorRatio=8, the official default is as follows:

As can be seen in the picture above, the new generation (Eden Space + Survivor 0 + Survivor 1): the old age (Old Gen) ≈ 1:2.

When we set-XX:NewRatio=4-XX:SurvivorRatio=8, the situation is as follows:

Become the Cenozoic (Eden Space + Survivor 0 + Survivor 1): Old Gen (Old Gen) ≈ 1 Survivor 4 Magi Eden Space:Survivor 0: Survivor 1 = 8:1:1.

As can be seen from the picture above, the information of the heap is correct.

-Xss

Set the stack memory for each thread, which defaults to 1m, which generally does not need to be changed.

-Xprof

Track the running program and output the trace data in the standard output; suitable for debugging in the development environment.

-Xnoclassgc

Disable class garbage collection and turn off the gc feature for class; because it prevents memory collection, it may cause OutOfMemoryError errors, so use it with caution.

-Xincgc

Turn on incremental gc (off by default); this helps reduce the pause of the application during a long GC, but reduces the processing power of the application by CPU because it may be executed concurrently with the application.

-Xloggc:file

Similar to the-verbose:gc function, except that the relevant information about each GC event is recorded in a file, preferably locally, to avoid potential network problems.

If it appears on the command line at the same time as the verbose command,-Xloggc shall prevail.

-Xint

In interpreted mode mode, the-Xint flag forces JVM to execute all bytecode, which slows down, usually 10 times or more.

-Xcomp

The-Xcomp parameter is the opposite of it (- Xint), and JVM compiles all bytecode into native code the first time it is used, resulting in maximum optimization.

However, many applications also suffer some performance loss when using-Xcomp, which is of course less than using-Xint because-xcomp does not allow JVM to enable the full functionality of the JIT compiler. The JIT compiler can determine whether it needs to be compiled or not, and if all the code is compiled, it doesn't make sense for some code that is executed only once.

-Xmixed

-Xmixed is the mixed mode, which is the default and recommended mode for JVM. It is up to JVM to decide to mix interpretation mode with compilation mode.

4 non-Stable parameters

When you look at the official website, you think that you cannot use this kind of parameters. the advice given to you on the official website is that these parameters are unstable and be used cautiously. In fact, the main reason is that the implementation of each company is different, so it leads to instability. However, it is very much in practical use, and the parameters of this part are very important.

These parameters can be roughly divided into three categories:

Performance parameters (Performance Options): performance tuning and memory allocation control for JVM, such as settings for initializing memory size

Behavior parameters (Behavioral Options): used to change the basic behavior of JVM, such as the selection of GC methods and algorithms

Debug parameters (Debugging Options): used to monitor, print, output and other jvm parameters, to display more detailed information of jvm

The following is to list some more commonly used parameters, in fact, there are many articles, here is mainly to make a summary, in the future, it is more convenient to read the article, if a colleague saw the article, you can refer to it, it is still very helpful.

In addition, select some of these parameters to do some examples to explain, this can also be more vivid.

Note: the following parameters are JDK1.7 and the following can be used.

Performance parameter

Parameter and its default value description-XX:LargePageSizeInBytes=4m sets large page size for Java heap-maximum percentage of free in java heap after XX:MaxHeapFreeRatio=70GC-minimum percentage of free in java heap after XX:MinHeapFreeRatio=40GC-maximum memory occupied by newly generated XX:MaxNewSize=size objects-maximum memory occupied by XX:MaxPermSize=64m older objects-ratio of XX:NewRatio=2 new generation memory capacity to old generation memory capacity-XX: Default value of memory occupied when NewSize=2.125m new generation objects are generated-XX:ReservedCodeCacheSize=32m preserves the amount of memory occupied by code-XX:ThreadStackSize=512 sets the thread stack size If 0, use the system default-XX:+UseLargePages uses large page memory

Behavior parameter

Parameter and its default value description-XX:+ScavengeBeforeFullGC Cenozoic GC takes precedence over FullGC execution-XX:+UseGCOverheadLimit limits the proportion of time jvm spends on GC before throwing OOM-XX:-UseParNewGC turns on this switch, using ParNew+Serial Old Collector-XX:-UseConcMarkSweepGC uses ParNew+CMS+Serial Old Collector for GC-XX:-UseParallelGC using concurrent tag Exchange algorithm to enable parallel GC, use ParallelScavenge+Serial Old Collector-XX:-UseParallelOldGC to enable parallelism for FullGC This is automatically enabled when-XX:-UseParallelGC is enabled, and ParallelScavenge+Parallel Old Collector-XX:-UseSerialGC enables serial GC-XX:+UseG1GC to use the garbage first (G1) collector-the ratio of XX:SurvivorRatio=nEden area size to Survivor area size. The default value is 8-XX:PretenureSizeThreshold=n, which is directly promoted to the old age object size. After setting this parameter, the objects larger than this parameter directly enter the old age allocation-XX:MaxTenuringThreshold=n is promoted to the old age object age. After each object has persisted Minor GC once, the age is increased by 1. When this value is exceeded, the object enters the old age. The default value is 15-XX:+UseAdaptiveSizePolicy to dynamically resize each area of the Java heap and the age of the old age-XX:ParallelGCThreads=n sets the number of CPU used for parallel collector collection. Number of parallel collection threads-XX:MaxGCPauseMillis=n sets the maximum pause time for parallel collection-XX:GCTimeRatio=n sets the percentage of garbage collection time as a percentage of program running time. The formula is 1 / (1cm N)-XX:+UseThreadPriorities enables local thread priority-XX:-DisableExplicitGC forbids calling System.gc (); but jvm's gc is still valid-XX:+MaxFDLimit maximizes the number of file descriptors

The first six parameters are all about the behavior parameters of the garbage collector and are frequently used parameters.

Debugging parameters

Parameter and its default value description-XX:-CITime prints the time consumed by JIT compilation-XX:ErrorFile=./hs_err_pid\ .log saves the error log or data to a file-XX:HeapDumpPath=./java_pid\ .hprof specifies the path or file name when exporting heap information-XX:-HeapDumpOnOutOfMemoryError exports relevant information in the heap when it first encounters OOM-XX:OnError= "\;\" run custom command-XX:OnOutOfMemoryError= after fatal ERROR occurs " \ "execute custom command when you encounter OOM for the first time-XX:-PrintClassHistogram prints the column information of the class instance when it encounters Ctrl-Break, which has the same function as jmap-histo-XX:-PrintConcurrentLocks prints information about concurrent locks when it encounters Ctrl-Break. Same as jstack-l function-XX:-PrintCommandLineFlags prints tags that appear on the command line-XX:-PrintCompilation prints relevant information when a method is compiled-XX:-PrintGC prints relevant information every time GC-XX:-PrintGCDetails prints details every time GC-XX:-PrintGCTimeStamps prints time stamp of each GC-loading information of XX:-TraceClassLoading tracking class-XX:-TraceClassLoadingPreorder tracks the addition of all referenced classes Load information-XX:-TraceCla***esolution trace constant pool-unload information for XX:-TraceClassUnloading trace classes-information about XX:-TraceLoaderConstraints trace class loader constraints these are all the contents of the article "sample analysis of common vm parameters in the Java virtual machine" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report