IntelliJ Memory Leak Detection
Also, referred to as IntelliJ IDEA, this IDE is an all-round and handy tool for programming, debugging, and profiling. Therefore, learning how to detect memory leaks in a powerful development environment such as IntelliJ is of utmost importance. Although, IntelliJ comes with an inbuilt profiling functionality, however, the use of certain dedicated and multi-purpose profilers such as FusionReactor would be pretty slick.
How to Find Memory Leaks in IntelliJ IDEA
A robust memory detection tool can efficiently analyze and detect java heap leaks while optimizing memory usage on the fly. Nonetheless, this is a primer exclusive to monitoring and reviewing the conditions that induce object retention which is a very useful information to keep in mind when detecting leaks. Another notable procedure would be to evaluate the memory usage, to better understand the inner workings of your application as well as minimize the creation of redundant objects.
Steps taken for IntelliJ Memory Leak Detection
Firstly, we would need to display the Memory tab by clicking on the window icon in the top-right corner of the Debug tool window.
The Memory tab would display the following information as follows;
The Class: – which is the name of the Class in use.
The Count: – a number indicating the amount of class instances (objects) present in the heap.
The Diff: – this represents the discrepancy that exists between the number of instances in two execution points.
Figure 1: The Memory Tab.
Next, we would need to fetch the information regarding the number of objects present. To do this, we would have to;
Manually pause the program or halt at a certain breakpoint.
Then, we would have to initiate the procedure by selecting Load Classes on the Memory tab settings icon. While the list of loaded Classes is displayed on the Memory tab, the corresponding number of active objects is shown in the Count column.
Note that this process is only initiated on-demand for performance reasons.
Our next task would be to find and sort these Classes. Finding a Class, is as easy as typing the class name in the search bar provided. To sort these Classes, click
Click on any of the headers that correspond to what you want from the option – Class, Count, or Diff. By clicking on the already selected criterion changes, you automatically change the format of the order – to either ascending or descending.
Next up is to get the difference between two execution points by means of the Diff column as described above. In order to achieve this, we would have to set up two points – which we would refer to as the starting and second point throughout the scope of this guide.
Collect the instance data at the starting point.
Figure 2: Collecting instance data at start point.
Resume program execution.
Figure 3: Stepping through the code.
Fetch the instance data from the second point by looking at the Diff column, we could ascertain the changes in the number of instances in real time.
Figure 4: Checking for changes in the number of instances.
Once we double click on a Class in the Memory tab to view its instances, a dialog box pops on, indicating all live instances of that Class. At this juncture, we could filter the list using conditions, while exploring the contents of each object.
For instance, in order to propagate a list of all objects with an empty String, we would have to double-click String on the Memory tab and key in the following condition; this.isEmpty() in the Condition field.
Figure 5: Viewing Instances.
Now the have to keep track of these instances. Additionally, we can view the number of instances and also record the creation of every instance along with its location in the call stack. For this, we would have to
Right-click on that Class and select Track New Instances. Upon initiating this process, the Memory tab would store and all information on every instance of that Class, from that moment onwards. If there are new instances their number would be displayed in parenthesis in the Diff column.
Figure 6: Tracking Instances.
To navigate the contents of each object, view the list of new instances and see the stack trace, click on the number in the Diff column.
Note that, for the Memory tab to track instances, it has to be kept open at all times.
The final step would be to customize the view and thus, the modus operandi of the Memory tab. Now there are a number of options available for this. These settings could be toggled from the Memory View Settings icon.
Show with Instances Only option lets you to only display Classes with live instances.
Show Non-Zero Diff Only option filters objects with a varying total number between the starting and second point.
Show tracked classes only option allows you to only view classes whose new instances you are tracking.
Enable Tracking with Hidden Memory View option permits you access track new instances of selected Classes even when they are hidden.
Update Loaded Classes on Debugger Stop option lets you to auto-load Classes and collect data even when the program is suspended.
Note that, the last two options may likely add extra overhead to the debugged application and thus impact stepping performance.
IntelliJ Memory Leak Detection
A dedicated memory profiler such as FusionReactor initiates the above process by taking individual snapshots of the heap, comparing the differences through GC Root analysis features, and saving them onto the local machine for instant and future references. Hence, through this process, you could quickly and efficiently analyze Java usage and spot heap leaks.