Android Monitor allows you to track memory allocation as it monitors memory use. Tracking memory allocation allows you to monitor where objects are being allocated when you perform certain actions. Knowing these allocations enables you to adjust the method calls related to those actions to optimize app performance and memory use.
The Allocation Tracker does the following:
- Shows when and where your code allocates object types, their size, allocating thread, and stack traces.
- Helps recognize memory churn through recurring allocation/deallocation patterns.
- Helps you track down memory leaks when used in combination with the HPROF Viewer. For example, if you see a bitmap object resident on the heap, you can find its allocation location with Allocation Tracker.
However, it takes time and experience to learn to interpret the output from this tool.
Understanding the Allocation Tracker display
The Allocation Tracker looks similar to the following figure:
The tool displays the following information:
|Method||The Java method responsible for the allocation.|
|Count||Total number of instances allocated.|
|Size||The total amount of allocated memory in bytes.|
Taking and displaying a snapshot of allocation data
To examine allocation data:
- Display a running app in the Memory Monitor.
- Click Start Allocation Tracking .
- Click Start Allocation Tracking again to deselect it and end the snapshot.
- Optionally click the graphic icon to display a visual representation of the data.
- Select the Group By menu option you want to display:
The Memory Monitor displays the period when it took the snapshot. In the following figure, you can see the snapshot period, as shown on the left. By comparison, when you dump the Java heap, the Memory Monitor displays just the point where the heap snapshot was taken, as shown on the right.
Android Studio creates the heap snapshot file with the
package_yyyy.mm.dd_hh.mm.ss.alloc using the activity package (or
project) name, year, month, day,
hour, minute, and second of the capture, for example,
- Group by Allocator
- Group by Method
Viewing a saved allocation tracking file
After you capture allocation data, Android Studio automatically stores it so you can view it again.
To view an allocation tracking file in the Allocation Tracker:
- Click in the main window.
- Open the Allocation Tracking folder.
- Double-click the file to view it.
Or select View > Tools Windows > Captures.
The Captures window appears.
Sorting allocation data
To sort allocation data:
- In the Allocation Tracker, click a column heading to sort the table by ascending or descending order.
Displaying Java source
For some items displayed in the Allocation Tracker, you can view the Java source.
To display Java source:
- In the Allocation Tracker, right-click a method and then select Jump to Source.
- In the Allocation Tracker, select a method and then click Jump to Source .
The source code appears in the Code Editor.
Working with allocation tracking files
You can rename, locate, and delete an allocation tracking file from within Android Studio.
Renaming an allocation tracking file
If you rename a file from within Android Studio, it continues to appear in the Captures window.
To rename an allocation tracking file:
- In the Captures window, right-click the file and select Rename.
- In the Rename dialog, specify the name of the file and click OK.
Locating an allocation tracking file
You can quickly discover where Android Studio stored allocation tracking files on disk.
To locate an allocation tracking file from Android Studio:
- In the Captures window, right-click allocation file and select Show or Reveal.
Android Studio opens an operating system file browser displaying the location where the file resides.
Note: If you move an allocation tracking file, Android Studio no longer displays it in the Captures window. To display the file, use File > Open. Also, rename the file from the Captures window and not in the operating system file browser.
Deleting an allocation tracking file
To delete an allocation tracking file:
- In the Captures window, right-click an allocation tracking file and select Delete.
Android Studio deletes the file from the Captures dialog and from disk.