JVM vs APPLICATION ARGS - mosinn/DOCS-n-Snippets-n-Steps GitHub Wiki
1. APPLICATION ARGS:
In a Spring Boot application, you can use command line arguments to pass configuration parameters and other runtime options to the application at startup. Spring Boot provides several different types of command line arguments, including:
Application arguments
These are arguments that are passed to the application at startup, after the -- delimiter. For example, if you run java -jar myapp.jar --foo=bar --baz=qux, the application arguments are --foo=bar and --baz=qux. You can access these arguments in your Spring Boot application using the ApplicationArguments
bean or the CommandLineRunner
interface.
System properties
These are arguments that are passed to the JVM using the -D flag. For example, if you run java -Dmy.prop=value -jar myapp.jar, the system property is my.prop=value. You can access system properties in your Spring Boot application using the System.getProperty()
method.
Environment variables
These are variables that are set in the operating system's environment and can be accessed by any process running on the system. In a Spring Boot application, you can access environment variables using the System.getenv()
method.
Configuration files
These are files that contain application configuration properties. Spring Boot supports several types of configuration files, including application.properties, application.yml, and others. Configuration properties can be set in these files and accessed in your Spring Boot application using the @Value
annotation or the Environment bean.
Profile-specific properties
These are properties that are specific to a certain application profile (e.g. dev, prod, etc.). In a Spring Boot application, you can define different properties for different profiles in the configuration files, using a naming convention like application-{profile}.properties or application-{profile}.yml. You can activate a specific profile by setting the spring.profiles.active
system property or environment variable.
2. JVM ARGS
JVM args are arguments that are passed to the Java Virtual Machine (JVM) at startup. They are used to configure the runtime environment of a Java application, such as memory allocation, garbage collection, and system properties.
JVM args are different from other command line arguments in that they are specific to the JVM and not the application being run. Other command line arguments are passed directly to the application and can be used to configure its behavior.
For example, if you wanted to run a Java application with a maximum heap size of 1 gigabyte, you would use the JVM arg "-Xmx1g" when starting the JVM. This arg is specific to the JVM and not the application. On the other hand, if you wanted to pass a command line argument to the application itself, you would use a different syntax, such as "-Dmyproperty=value".
In summary, JVM args are used to configure the runtime environment of a Java application, while other command line arguments are used to configure the behavior of the application itself.
Here are some examples of commonly used JVM args:
-
-Xmx: This arg sets the maximum heap size that the JVM can use. For example, "-Xmx2g" sets the maximum heap size to 2 gigabytes.
-
-Xms: This arg sets the initial heap size that the JVM uses. For example, "-Xms256m" sets the initial heap size to 256 megabytes.
-
-XX:+UseG1GC: This arg enables the G1 garbage collector, which is a low-pause, high-throughput garbage collector introduced in Java 7.
-
-XX:+UnlockExperimentalVMOptions -XX:+UseZGC: This arg enables the Z Garbage Collector, which is a scalable, low-latency garbage collector introduced in Java 11.
-
-D: This arg sets system properties that can be accessed by the Java application. For example, "-Dmyproperty=value" sets the "myproperty" system property to "value".
-
-verbose:gc: This arg prints information about garbage collection to the console, which can be useful for debugging memory-related issues.
-
-Xss: This arg sets the size of the thread stack. For example, "-Xss1m" sets the thread stack size to 1 megabyte.
-
-XX:+HeapDumpOnOutOfMemoryError: This arg tells the JVM to dump the heap to a file when an OutOfMemoryError occurs. This can be useful for analyzing memory-related issues.
-
-XX:MaxMetaspaceSize: This arg sets the maximum size of the metaspace, which is used to store metadata for class definitions. For example, "-XX:MaxMetaspaceSize=256m" sets the maximum metaspace size to 256 megabytes.
-
-XX:PermSize and -XX:MaxPermSize: These args set the initial and maximum size of the permanent generation, which is used to store metadata for classes and interned strings. These args are only applicable in Java versions prior to Java 8.
-
-XX:+PrintGCDetails: This arg prints detailed information about garbage collection, including the time spent in each phase of the garbage collector.
-
-XX:+UseCompressedOops: This arg enables the use of compressed object pointers, which can reduce memory usage on 64-bit platforms.
There are several JVM args that can be used to set debug properties:
-
-Xdebug: This arg enables debugging support for the JVM.
-
-Xrunjdwp: This arg specifies the debugging protocol that the JVM should use. For example, "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" specifies that the JVM should use the Socket Transport and listen on port 8000 for incoming connections.
-
-agentlib:jdwp: This arg is an alternative to -Xrunjdwp and is used to specify the JDWP agent library. For example, "-agentlib:jdwp=transport=dt_socket,address=localhost:8000,suspend=n" specifies that the JVM should use the JDWP agent library and listen on port 8000 for incoming connections.
-
-XX:CompileCommand: This arg is used to set commands that are executed during JIT compilation. For example, "-XX:CompileCommand=print,"*MyClass.myMethod"" prints debugging information during compilation of the "myMethod" method of the "MyClass" class.
-
-XX:+TraceClassLoading: This arg prints debugging information when a class is loaded.
-
-XX:+TraceClassUnloading: This arg prints debugging information when a class is unloaded.
These are just a few examples of JVM args that can be used to set debug properties. There are many other args that can be used depending on the specific debugging scenario.