laravel flutter spring boot nginx performance monitoring monitoring

System performance: Java Application Process specifically with the Java Virtual Machine (JVM) - part 2

07-Feb-2025 - Muthomi Kathurima

 

With this items resolved;

  1. Logging
  2. Uptime Monitoring
  3. Observability tool, Performance & Error Monitoring

Lets start to resolve issues as we observed. This article is based on actual experience in a day to day experience solving issues. If you have not read the previous article please do to understand this better. System performance: What you need to know about monoliths and bare metal servers setup optimization - part 1

Java Process Configuration

1. Introduce a program to monitor core elements of the system affecting Java processes

Items we expect to monitor;

  • Thread activity
  • Garbage collection time
  • Memory consumption
  • File and socket IO
  • Locks

Our tool of choice was JProfiler. However there are other tools you can use to monitor with including Java Flight Recorder.

JVM Heap Monitoring

 

With the tool on place, we could proceed and optimise for the core progress.

2. Update the Maximum Heap Size

-Xmx is the JVM option for controlling memory usage for Java applications. To understand what maximum heap size means, we have to define heap size. Heap Size is the portion of memory allocated for application runtime data. Heap size defines what memory the application can use for creating objects.

Maximum Heap Size defines the upper limit of heap size the JVM can use. The JVM cannot exceed beyond the value defined even if the application requires it. Typically if the application requires more, a Out Of Memory exception will be thrown.

Typically heap size is define in MegaBytes (how we define storage) or GigaBytes. The value is an integer followed by the symbol m or M and g or G respectively.

Example;

java -Xmx512m SpringBootApplication

The above represents 512 MB of maximum heap size.

To properly define this, start with about 50% or more depending on what other applications you are running on the server. Once you have monitoring data from the JProfiler you can either add the memory or reduce based on the usage.

3. Update the Initial Heap Size

-Xms is the JVM option for controlling the minimum amount of memory the JVM starts with. As defined on the previous item, its defined the same way. This value should always be lower than the maximum heap size.

With monitoring, you can be able to define the minimum memory for the JVM to start to avoid issues such as slow startup due to need to adjust the memory.

Typically there is recommendation to keep the value of initial heap size and maximum heap size the same. However when memory is a crunch you can keep the initial memory lower to avoid unused memory.

Heap size affects other items such as garbage collector frequency which is one of the items we will also talk about later.

Conclusion

Install a profiler such as JConsole. Update your Xmx and Xms. Monitor and adjust the memory appropriately.

Sample code

java -Xmx4096m -Xms3072m SpringBootApplication

On the next segment, we will continue on optimizating our application on bare metal server.