Loop detection support in Java
Site24x7's APM Insight Java agent facilitates loop detection. This feature helps you identify loops in application code that may contain duplicate database queries or consume excessive time, ensuring a more efficient execution of code blocks.
Understanding loops and the need for monitoring
Loops are an essential component of programming that enable developers to execute a set of instructions repeatedly until a specific condition is met. This functionality is vital for performing repetitive tasks, processing data collections, and implementing various algorithms.
However, despite their usefulness, loops can also introduce performance issues if not implemented properly. When loops are executed excessively or contain inefficient logic, they can consume significant processing time and resources, leading to poor user experiences. Poorly written loops can cause application slowdowns, increased load times, and in some cases, even crashes.
To ensure optimal performance, it is crucial for developers to monitor continuously and identify inefficient loops within their code. By proactively detecting these code blocks, developers can optimize them and adopt better practices as their applications scale and evolve.
How loop detection in APM Insight Java agent helps
The loop detection feature in the APM Insight Java agent captures loops within your application's code and identifies duplicate calls. This capability is particularly valuable for uncovering inefficiencies that may not be evident in synchronous or asynchronous code. By detecting and addressing these hidden performance bottlenecks, developers can enhance application performance and provide a smoother user experience.
By default, the loop detection feature is disabled, but you can enable it.
Where to view this in the UI
- Log in to your Site24x7 web client.
- Go to APM > your Java application.
- Click the Traces tab, then select the required trace.
- Select the Trace Details tab.
- Look for spans labeled Loop Detected.
![Loop detected](/help/images/trace-details-loop-detected.png)
The highlighted portion of spans depicts the agent's detection of a nested loop implementation in which the outer loop (at CatalogBean.viewProduct) runs twice, and the inner loop (at Product.sleepTime) runs twice, resulting in four iterations in total.
How to enable/disable loop detection
- Navigate to APM > APM Insight > your Java application.
- Click the hamburger icon (
) near the Application/Instance drop-down menu and select Edit.
- In the Edit Agent Configuration Profile screen, you can enable or disable Loop Detection.
![Edit Agent Configuration Profile](/help/images/config-profile.png)
How are loops captured?
Not all loops are captured.
Loop detection is specifically designed for methods within user packages. A loop is captured if it meets one of the following thresholds:
- Contains at least one external call inside the loop.
- The total duration of the loop exceeds 10ms.
Metrics captured
When a loop is detected, the following metrics are captured and displayed in the Trace Details tab:
Metrics | Description |
---|---|
Duration | Total time taken for the loop execution |
Origin of the loop | Name of the class and method enclosing the loop |
Iteration count | The number of iterations the loop goes through |
Consolidated view of duplicate calls | External calls that occur across iterations at the same place within a loop are merged and shown as a single span |