In this article, I will explain about the principles of Java application performance tuning. Specifically, I will explain the steps you need to perform to identify whether your application needs tuning and the problems you may encounter during performance tuning.
OverviewEvery application doesn't require tuning. If an application performs as per expectations, then you don't need to exert additional efforts to enhance its performance. However, it would be difficult to expect an application would reach its target performance. If application is not performing as per expectations then tuning is required. Regardless of the implementation language, tuning an application requires high expertise and concentration.
Also, one tuning method which work on one application may not work on another. This is because each application has its unique action and a different type of resource usage. For this reason, tuning an application requires more expertise. When you focus on an application domain based on such knowledge, you can successfully tune an application.
Sometimes Java application tuning requires only changing JVM options, such as Garbage Collector, but sometimes it requires changing the application source code. Whichever method you choose, you need to monitor the process of executing the Java application first. A couple of details, this article will deal with:
- How to monitor a Java application?
- What should be JVM options to an application?
- How can it come to know that modifying source code is required or not?
Knowledge Required to Tune Java Application PerformanceJava applications operate inside Java Virtual Machine (JVM). Therefore, to tune a Java application, you need to understand how JVM works. To understand, the working of JVM, You must have knowledge of How Garbage collector works in Java. But, all types of applications can't be tunned only with the knowledge of How Garbage collector works in Java.
Another aspect is that knowledge of Java language domain is also important. It is also important to understand lock or concurrency and to be familiar with class loading or object creation. When you carry out Java application performance tuning, you should approach it by integrating all this knowledge.
Java Application Performance Tuning ProcessBelow are the points that we noted down above. Now, we'll discuss all those points one by one:
Java Application MonitoringBelow Figure shows a flow chart from the book
The above process is not a one-time process. You may need to repeat it until the tuning is completed. This also applies to determining an expected performance value.
JVM OptionsWhen we run a large enterprise application, then it consumes a large amount of memory. If, JVM options are not defined properly then we get Exception in thread "main" java.lang.OutOfMemoryError: Java heap space error and our application stops working. So, a proper memory must be allocated to the application. You can't find out the total memory consumed by your application. You need to repeat many times and then according to the memory consumed by your application, allocate heap size and max. permgen space for JVM.
Also, there are many performance monitoring tools available in the market. Using a monitoring tool, you collect data for various system performance indicators. Almost all the monitoring tools shows CPU usage, memory consumed and many more indicators that can help you to analyze your application.
Whether modifying source code is required or not
When you analyze the results of the monitoring or profiling tool, then check whether your code is consuming more memory space or not. If it is consuming more space and JVM is not garbage collecting object from memory then go to that loop hole and check your code once because there may be references that have not been released and due to this those objects are not getting garbage collected.
Summary: Tuning ApproachBelow are few of the advises that you may follow to tune applications.
- First, check whether or not performance tuning is necessary for your application. It isn't an easy work and also, you are not guaranteed to obtain a satisfactory result all the time. Therefore, if the application already meets its target performance, you don't need to invest additional effort to tune the application.
- The problem may lie at a single place but this doesn't mean that the low performance of a certain feature results necessarily from a single problem. You should focus on one factor that has the biggest influence on the performance while approaching performance tuning. Thus, you could handle another problem after fixing the most important one. You are advised to try to fix just one problem at a time.
- You can improve responsiveness by using caching but if the cache size increases, then the time it takes to carry out a garbage collection will increase as well. In general, if you want a small amount of memory usage then throughput or responsiveness could be deteriorated. Thus, you need to consider what is most important and what is less important.