Rate This Document
Findability
Accuracy
Completeness
Readability

Tuning Process

Prerequisites

  • The server and OS are running properly.
  • An SSH remote login tool has been installed on the local PC.
  • The Java program (demo) to be tuned is running on the target server.

Preparations

  1. Log in to the Java Profiler.
  2. Add the target server (server where the Java process to be profiled is located).
    Figure 1 Adding a target environment
  3. Click Real-Time Profiling.
    Figure 2 Real-Time Profiling

Real-Time Profiling

  1. View the overall process status on the Overview tab page.
    Figure 3 Overview

    It can be seen from the Overview tab page that the heap usage of the program keeps increasing in a period of time with frequent GC. However, the heap is not released obviously, which may cause memory leak.

  2. View GC details on the GC tab page.
    Figure 4 GC

    According to the analysis, young GC and old GC occur frequently. However, basically no memory is collected by each GC, which indicates that there is a high probability that memory leak occurs.

  3. View memory details on the Memory tab page.
    Figure 5 Memory

    On the Memory tab page, click Start Heap Dump to generate a heap dump file. To view heap changes, click to save the snapshot of the current page. Then click Start Heap Dump again to generate another heap dump file.

  4. Generate a memory heap dump file again.
    Figure 6 Result comparison

    After generating two heap dump files, we can find that the TestMemoryLeak$Person class is a user-defined class. The memory usage is high and increases over time. Observation on the memory usage is required.

    Click to generate a new snapshot. Use the snapshot function to compare and view the changes of the same class instances and retained heaps in the two heap dump files. The comparison result shows that the TestMemoryLeak$Person instance and retained heap increase significantly.

  5. Select Dominant Tree from the Display drop-down list to switch to the dominant tree mode. In this mode, the memory dump information is displayed in a tree structure.
    Figure 7 Dominator tree view

    Switch to the dominator tree mode. It is found that the retained heaps of the TestMemoryLeak class account for 99% of the total heaps. Click the dominator tree and it is found that most objects are byte[] held by TestMemoryLeak$Person. In the histogram, byte[] also occupies a large proportion.

  6. Analyze the result.
    Figure 8 Code snippet

    According to the analysis of the histogram and dominator tree, it is found that the instance corresponding to the TestMemoryLeak$Person class may have memory leaks. We can see that the hashSet instance holds the TestMemoryLeak$Person object from the dominator tree. Next, we can search for related items in the code. The demo in this example is simple. Search for the TestMemoryLeak class and you can find the memory leak points. A thread continuously adds Person instances to the hashSet and hashSetPerson static classes, and there is no logic for clearing the instances. As a result, memory leak occurs.

Sampling Profiling

This section describes how to sample and analyze a program. It is recommended that the target program be sampled for multiple times to ensure a relatively long sampling duration.

  1. Click Sampling Profiling.
    Figure 9 Configuring sampling profiling parameters
  2. View the sampling profiling result.
    Figure 10 Sampling profiling result