"java -X" - Specifying Non-Standard Options

This section provides a tutorial example on how to use the '-X' option for the 'java' tool to specify non-standard options. The tutorial example shows how to control the memory size of the JVM.

"java" can also take "non-standard" options by using the "-X" option.

Two of the "non-standard" options are very useful to control the memory size of the JVM to be launched.


where "ms" specifies the initial size of the JVM heap, "mx" specifies the maximum size of the JVM heap, and "n" specifies the memory size in bytes, kilo bytes or mega bytes. For example: -Xms32M and -Xmx64M.

Here is a program to show you how to use these options:

/* ShowMemory.java
 * Copyright (c) 2005 HerongYang.com. All Rights Reserved.
class ShowMemory {
   public static void main(String[] a) {
      Runtime rt = Runtime.getRuntime();
      System.out.println(" Free memory: " + rt.freeMemory());
      System.out.println("Total memory: " + rt.totalMemory());
      while (true);

I launched the program with different options:

herong> javac ShowMemory.java

herong> java ShowMemory
 Free memory: 61276664
Total memory: 62914560

herong> java -Xms32M -Xmx128M ShowMemory
 Free memory: 31916520
Total memory: 33554432

herong> java -Xms64M -Xmx128M ShowMemory
 Free memory: 65470984
Total memory: 67108864

Note that ShowMemory is running an infinite loop at the end. You need to stop the program by "Ctrl-C"

Here is how to get a list of "java" non-standard options:

herong> java -X

-Xbatch           disable background compilation
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
                  append to end of bootstrap class path
-Xcheck:jni       perform additional checks for JNI functions
-Xcomp            forces compilation of methods on first invocation
-Xdebug           provided for backward compatibility
-Xdiag            show additional diagnostic messages
-Xfuture          enable strictest checks, anticipating future default
-Xint             interpreted mode execution only
   displays more detailed JVM version information than the -version option
-Xloggc:<file>    log GC status to a file with time stamps
-Xmixed           mixed mode execution (default)
-Xmn<size>        sets the initial and maximum size (in bytes) of the heap
                  for the young generation (nursery)
-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xnoclassgc       disable class garbage collection
-Xrs              reduce use of OS signals by Java/VM (see documentation)
-Xshare:auto      use shared class data if possible (default)
-Xshare:off       do not attempt to use shared class data
-Xshare:on        require using shared class data, otherwise fail.
-XshowSettings    show all settings and continue
                  show all settings and continue
                  show all locale related settings and continue
                  show all property settings and continue
-XshowSettings:vm show all vm related settings and continue
-Xss<size>        set java thread stack size
-Xverify          sets the mode of the bytecode verifier
--add-reads <module>=<target-module>(,<target-module>)*
   updates <module> to read <target-module>, regardless of module
   declaration. <target-module> can be ALL-UNNAMED to read all unnamed
--add-exports <module>/<package>=<target-module>(,<target-module>)*
   updates <module> to export <package> to <target-module>, regardless
   of module declaration. <target-module> can be ALL-UNNAMED to export
   to all unnamed modules.
--add-opens <module>/<package>=<target-module>(,<target-module>)*
   updates <module> to open <package> to <target-module>, regardless of
   module declaration.
   permit or deny access to members of types in named modules by code
   in unnamed modules. <value> is one of "deny", "permit", "warn", or
   "debug". This option will be removed in a future release.
--limit-modules <module name>[,<module name>...]
                  limit the universe of observable modules
--patch-module <module>=<file>(;<file>)*
   override or augment a module with classes and resources in JAR files
   or directories.
--disable-@files  disable further argument file expansion

These extra options are subject to change without notice.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

java - The Java Program Launcher

 java - Program Launching Command and Options

 Launching Hello.java - My First Java Program

 "java -classpath" - Specifying Class Path

 "java -jar" - Specifying Executable JAR File

"java -X" - Specifying Non-Standard Options

 "java --list-modules" - Listing Modules in JDK

 "java --describe-module" - Printing Module Definition

 "java --module" - Launching Program from Module

 "java --module" - Launching Program from Module JAR

 javaw - Launching Java Programs without Console

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials


 Full Version in PDF/EPUB