Understand important JVM (Java Virtual Machine) options to improve performance and debugging.
If you are a Java developer or middleware administrator, you need to know the meaning and importance of JVM options and how they affect your applications. Let’s explore them.
Overview of JVM options
When it comes to JVM options, there are three types of options that can be included in a JVM: standard options, non-standard options, and advanced options. If you want to try advanced options, always use options with -XX . Similarly, use -X to apply non-standard options. Standard options do not add anything before the option.
Which JVM options will my application use?
If your application is running on Linux, you can use ps -ef | grep java . Identify the Java process using ps -ef | grep java and check the JVM options printed as process arguments. If you have multiple Java processes running on your system, you may need to use keywords specific to your Java application.
If the argument is too long, try using ps -auxww . This command also displays a long argument list.
Obtaining a list of JVM flags can give you an idea of how a Java application, such as Tomcat, behaves.

Java heap size
-Xms - set initial Java heap size
-Xmx - set maximum Java heap size
-Xss - set java thread stack size -Xms – This option defines the JVM’s starting heap size ( Xms2048m Surprisingly, yes! This is done to prevent resizing at startup and speed up JVM startup time.
-Xmx – This option defines the maximum heap size for the JVM. For example, Xmx2048m means that the JVM’s maximum heap size will only be 2 GB.
Basically, you should always use -Xms and -Xmx together.
Set heap percentage
-XX:MaxHeapFreeRatio – Sets the maximum percentage of the heap that is freed after GC to avoid compaction.
-XX:MinHeapFreeRatio – Sets the minimum percentage of free heap after GC to avoid expansion. You can use JCosole to monitor heap usage.

Enabling class data sharing
Specify the Xshareclasses option to enable sharing of class data in the shared class cache. The JVM connects to an existing cache, or creates one if one does not exist. You can have multiple caches and specify the correct cache by adding suboptions to the -Xshareclasses option.

PermGen size
While the previous JVM options define the size of heap memory, -XX:PermSize defines the size of the PermGen area where string pools and class metadata are stored. This option is particularly useful for web servers like Tomcat that often load classes for web applications during deployment.
By the way, realize that this option is not applicable if you are running on a JRE 8 JVM, as the PermGen space is taken over by Java 8’s Metaspace.

printing GC
-verbose:gc - logs garbage collector runs and how long they're taking.
-XX:+PrintGCDetails - includes the data from -verbose:gc but also adds information about the size of the new generation and more accurate timings.
-XX:-PrintGCTimeStamps - Print timestamps at garbage collection. These JVM options are used to enable garbage collection logging. This is very effective for latency-sensitive operations. We used to have systems that strived for microsecond latencies, but as we know, large garbage collections can last several milliseconds.
Therefore, if you employ a GC-free architecture like LMAX Disruptor, but in fact do not use latency-sensitive applications, this useful option will advise you on important GC statistics. This tells you whether there is a large or small garbage collection, what type of garbage collector is applied, how often the memory is restored, how long the memory is retained, and more.
Handling “OutOfMemory” errors
To trigger a heap dump when out of memory, you can use -XX:+HeapDumpOnOutOfMemoryError .
This JVM option generates a stack dump when the JVM stops with an OutOfMemory error. There is no cost unless there is an actual OOM. This flag is required in production systems, as it is usually the only way to deeply determine the problem.
Heap dumps are set up by default in the JVM’s “current directory”. If you want to create a heap dump in a specific directory, run the following command:
-XX:HeapDumpPath= [path-to-heap-dump-directory]
-XX:+UseGCOverheadLimit
-XX:OnOutOfMemoryError="< cmd args >;< cmd args >"Heap dump files can reach up to gigabytes in size, so make sure the target file system has enough space.
If you want to restart the server immediately after running out of memory, you can set this parameter for the same purpose.
XX:OnOutOfMemoryError="shutdown -r"Trace class loading and unloading
-XX:+TraceClassLoading and -XX:+TraceClassUnloading are two JVM options used to output logging information when classes are loaded into or unloaded from the JVM. These JVM flags are useful if you suspect that there is some type of memory leak related to classloaders and classes are not being unloaded or garbage collected.
java classpath
Talking about JAVA classpath, -Xbootclasspath specifies classpath entries to load without validation. The JVM examines every class it loads to see if it attempts to dereference objects with ints, pop extra entries from the stack, push too many entries, etc.
Placing classes in the boot classpath also saves cost, but should only be used if you know the class has been validated many times before. In JRuby, this reduced startup time by more than half for simple scripts.
Profiling
Java profiling is the process of monitoring various JVM-level parameters such as method execution, thread execution, garbage collection, and object creation. Java profiling gives you more insight into the execution of your target application and its resource usage.
-Xprof
-Xrunhprof64 bit environment
In an OS environment where both 32-bit and 64-bit packages are installed, the JVM automatically selects the 32-bit environment package as the default.
If you want to manually set your environment to 64 bit, you can do so using -d<OS bit> parameter. And obviously the OS bits will be either 32 or 64.
conclusion
I hope this helps you configure JVM parameters for your application. If you want to learn more about Java memory management, check out this Udemy course.




![How to set up a Raspberry Pi web server in 2021 [Guide]](https://i0.wp.com/pcmanabu.com/wp-content/uploads/2019/10/web-server-02-309x198.png?w=1200&resize=1200,0&ssl=1)











































