Core Java

Java JVM Arguments Prefixes

Java Virtual Machine (JVM) argument prefixes provide essential mechanisms to customize and control how a Java application behaves at runtime. These prefixes define the syntax for passing startup options to the JVM, covering everything from memory configuration and classpath settings to advanced debugging, garbage collection tuning, and runtime instrumentation.

These arguments are passed to the java command when launching an application and are critical for developers and DevOps engineers aiming to fine-tune the execution environment. Each prefix corresponds to a different category of options, with some being standardized across JVM vendors and others considered experimental or implementation-specific.

This guide explores some common prefixes alongside a breakdown of some execution modes enabled via -X arguments.

1. Why JVM Argument Prefixes Matter

Understanding JVM argument prefixes is useful for several reasons:

  • Performance Optimization: Adjusting heap size or garbage collection algorithms via specific prefixes can dramatically improve runtime performance.
  • Debugging and Monitoring: Prefixes enable detailed logging, memory diagnostics, and allow tools like debuggers and profilers to attach to the JVM.
  • Custom Configuration: By passing properties and paths at startup, we can make applications environment-aware without changing source code.
  • Instrumentation: Some prefixes allow for runtime bytecode manipulation or monitoring via Java agents.

2. A Look at Common JVM Prefixes

Here is an overview of some of the most frequently used JVM argument prefixes and what they are generally used for.

3.1 Standard JVM Arguments –

Standard JVM arguments are those officially defined and supported across all JVM implementations. These options are the most commonly used and enable basic functionality for running Java programs.

Examples include:

-cp or -classpath: Specifies the classpath for locating classes and resources.

java -cp target/myapp.jar com.jcg.example.ReplaceWordNio

-jar: Tells the JVM to launch a Java application packaged as an executable JAR file.

java -jar myapp.jar

-version: Displays the version of the JVM.

java -version

3.2 -X Prefix – Non-Standard JVM Arguments

The -X prefix introduces non-standard but widely implemented JVM options. These arguments are not guaranteed by the Java specification but are available in most common JVMs (e.g., HotSpot, OpenJ9).

We can view a list of supported -X options on our system by running:

java -X

This command will output a list of supported -X options for your specific JVM version and vendor, along with short descriptions of what each option does. These options may vary between JVM distributions (e.g., OpenJDK vs. Oracle JDK).

Screenshot showing the output of java -X command demonstrating available Java JVM argument prefixes.
Screenshot showing the output of the java -X command, demonstrating available Java JVM arguments and their corresponding prefixes.

Example:

java -Xms512m -Xmx2048m -Xss1m -XshowSettings -jar target/java-replace-word-in-file-1.0.jar

JVM Execution Modes – Interpreted vs. Compiled Execution

One of the most critical uses of -X arguments is to configure how the JVM executes bytecode—either via interpretation or Just-In-Time (JIT) compilation. These execution modes affect startup time, runtime performance, and how the JVM interacts with native hardware.

-XintInterpreted Mode Only

Runs all bytecode through the JVM interpreter. No JIT compilation is performed.

java -Xint -jar app.jar

Useful primarily for debugging or identifying JIT-related issues, this mode results in significantly slower performance since the JVM interprets all bytecode without compiling it to native code.

-XcompCompile Everything Immediately

Compiles all bytecode to native code at startup, skipping interpretation.

java -Xcomp -jar app.jar

This mode is beneficial for compiler testing or performance profiling, though it may lead to longer startup times and less optimized execution paths due to the immediate compilation of all bytecode.

-XmixedDefault Hybrid Execution

Begins interpreting bytecode and uses JIT to compile frequently-used (“hot”) methods.

java -Xmixed -jar app.jar

This mode offers a balanced approach between startup performance and long-term efficiency by combining interpretation with Just-In-Time compilation, and it serves as the default execution mode in most JVM implementations.

-D Prefix – Define System Properties

The -D prefix is used to define system properties in the JVM. It is commonly used to enable or disable specific features, specify the application environment (such as development, staging, or production), and pass configuration values or sensitive information like secrets and file paths at runtime.

java -Denv=production -Dapp.version=0.0.1 -jar target/java-replace-word-in-file-1.0.jar

These key-value pairs are available to the application during runtime through System.getProperty().

Accessing in Code

public class Main {
    public static void main(String[] args) {
        String env = System.getProperty("env");
        String version = System.getProperty("app.version");
        System.out.println("Running in " + env + " environment, version: " + version);
    }
}

3.3 -XX Prefix – Advanced and Experimental JVM Options

The -XX prefix introduces advanced and often experimental options that allow low-level tuning of JVM internals, such as garbage collectors, memory limits, JIT compiler behavior, and performance thresholds.

These are usually vendor-specific and should be used with caution, especially in production.

java -XX:+PrintFlagsFinal -version

This will print a detailed list of JVM flags, their default values, and whether they are diagnostic or experimental. The following command demonstrates the use of advanced JVM options to configure garbage collection and memory settings:

java -XX:+UseG1GC -XX:MaxMetaspaceSize=512m -Xlog:gc* -jar target/java-replace-word-in-file-1.0.jar

This command starts an application using the G1 garbage collector (-XX:+UseG1GC) and limits the maximum Metaspace size to 512 megabytes (-XX:MaxMetaspaceSize=512m).

3.4 -agentlib, -agentpath, and -javaagent – Agents and Instrumentation

These prefixes are used to attach agents that instrument the JVM at runtime.

-agentlib:<libname>

Loads a native agent library from the JVM’s library path.

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar target/java-replace-word-in-file-1.0.jar

-agentpath:<path-to-lib>

Loads a native agent from a specific path, giving more control over library location.

java -agentpath:/usr/lib/libmonitor.so com.jcg.example.ReplaceWordNio

-javaagent:<jar-path>

Loads a Java agent JAR that implements java.lang.instrument.

java -javaagent:/path/to/my-agent.jar com.jcg.example.ReplaceWordNio

4. Conclusion

JVM arguments prefixes provide powerful ways to configure, tune, and extend the behavior of Java applications. Understanding and using the correct prefixes helps us leverage JVM features effectively, improve performance, and ease debugging or profiling.

Tip: You can explore the available options supported by your JVM using the appropriate command-line flags.

java -X
java -XX:+PrintFlagsFinal -version

Each Java version might support different options, so always consult the official documentation for your JDK.

This article covered Java JVM arguments and their prefixes.

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button