This article was originally published on IBM Developer.
The Java Virtual Machine (JVM) is the engine that runs your Java application. It handles memory allocation, garbage collection (GC), thread management, and JIT compilation.
VM performance tuning is the process of optimizing the Java Virtual Machine (JVM) configuration and behavior to improve the performance, scalability, and reliability of Java applications. If your JVM is not configured properly, you might experience high latency, out of memory errors, CPU spikes, slow response times, or application crashes.
In this article, we’ll review two key performance tuning techniques: memory management and garbage collection. By optimizing your JVM with these two techniques, you will improve the performance, scalability, and reliability of your Java applications.
Memory management in JVMs
In this article, we will explore these JVM flags:
- -Xmx: The maximum heap size, which you use to limit how much memory is used.
- -Xms: The initial heap size, which you use to pre-allocate memory to improve performance.
- -Xss: The thread stack size, which you use to control how much memory each thread can use.
- -XX or -Xlog:gc*:file: The automatic process of reclaiming memory by removing unused or unreachable objects to ensure efficient memory management and prevent leaks.
Maximum heap size (-Xmx)
The -Xmx flag sets the maximum heap memory that the JVM is allowed to use. The JVM will not allocate more heap memory that the value specified.
For example, if you use -Xmx2G, then the JVM can use up to 2 gigabytes of heap memory.
If your application needs more heap memory than what is specified during runtime, an OutOfMemoryError exception is thrown.
Initial heap size (-Xms)
The -Xms flag sets the initial heap memory that is allocated when the JVM starts. By setting the initial heap memory, you help prevent dynamic memory allocation during early runtime.
For example, if you specify -Xms512M, then the JVM starts with 512 megabytes of heap memory.
To avoid heap resizing and improve performance, you can set -Xms to the same value that you set -Xmx.
Thread stack size (-Xss)
The -Xss flag sets the stack memory size for each thread. Each thread has its own stack that is used for method calls and local variables.
For example, if you specify -Xss1M, then each thread will be given 1 megabyte of stack memory.
If you specify a size that is too small, then a StackOverflowError exception is thrown. If you specify a size that is too large, then you have fewer threads available due to high memory usage per thread.
Garbage collection (-XX or -Xlog:gc)
Garbage Collection (GC) in Java is the process by which the Java Virtual Machine (JVM) automatically identifies and removes unused or unreachable objects from memory to free up space and ensure efficient memory management. Garbage collection helps prevent memory leaks, avoid dangling pointers, and reduce programmer error.
Garbage collection does the following:
- Finds objects that are no longer needed. These are objects that your code can no longer access (no active reference to them).
- Reclaims the memory used by these objects. This memory is then made available for new object creation.
- Optionally returns unused memory back to the OS (in some JVMs).
Java 8: -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:
-XX flags are non-standard, advanced options (as opposed to standard -X or normal flags) and are used to enable, disable, or configure GC behavior, memory management and more.
Format:
- -XX:+OptionName - Enable the option.
- -XX:-OptionName - Disable the option.
- -XX:OptionName=value - Set the option to a specific value.
Java 9 onwards: -Xlog:gc*:file=
Continue reading on IBM Developer...
Top comments (0)