Let's see some examples where we can use these tools to figure out some problems related to memory or cpu!!
- Let's assume a scenario where CPU usage is exceeding the alarming limit. So first we have to check the number of active threads. More active threads mean heavier CPU usage because of the numerous context switch happening. Excessive number of threads can indicate a code issue or a poorly responding backend. However, if we see threads just spinning on CPU it will be good if we learn what they are actually doing. We can use the jstack utility to get the stack dump of each java thread with its state. For example, let us assume that the below trace is taking the highest CPU
"http-0.0.0.0-8080-Processor108" daemon prio=3
tid=0x000000000300e000 nid=0x398f runnable [0xffff80ff9451d000]
java.lang.Thread.State: RUNNABLE at java.util.HashMap.getEntry(HashMap.java:446)
at net.portrix.sw.backend.user.SomeCache.access(SomeCache.java:38)So we can easily drill down saying java.util.HashMap.getEntry is the main culprit.
- Let's take a sceneries where we need to figure out a memory leak. Memory Leaks occurs when object references that are no longer needed are unnecessarily maintained. Just getting an OutOfMemoryError alert doesn’t necessary mean that you're suffering from a memory leak. So, before you dive into "fixing" the problem, you must first find out whether or not a memory leak actually exists. If a memory leak does in fact exist, the next step is to determine which objects are leaking and uncover the source of the memory leak. We can again use Heap Dump and if we see stair case pattern exists which likely is the pattern of memory leak.
There are tons of tools available in the market like XRebel, newrelic, appDynamics, Nastel etc. which gives us a lot of monitoring data about apps, flexible tag-driven alerting, reports on availability scalability etc., also tells us about the time taken by each DB calls of your API and a lot more but the only problem with these tools are they comes with some licensing fee.
So, to start off with the analysis of your APP you can definitely take advantage of JDK's performance profiling tools.