Mastering Memory Profiler Skills: Identifying Memory Problems

About the author: CSDN content partner, technical expert, starting from scratch to make tens of millions of daily activities APP.
Focus on sharing original series of articles in various fields, good at java backend, mobile development, artificial intelligence, etc. I hope you will support me a lot.

insert image description here

1. Introduction

We continue to summarize and learn the basics of Android , reviewing the past and learning the new.

2. Overview

Memory Profiler is a component of Android Profiler that helps you identify memory leaks and memory thrashing that can cause your app to stutter, freeze, or even crash.
It shows a real-time graph of your app's memory usage, lets you capture heap dumps, enforce garbage collection, and track memory allocations.

Memory Profiler is mainly used to identify memory leaks and jitters, provide heap dump capture, force GC, and track memory allocation.
Heap dump files saved through Memory Profiler need to be converted before they can be opened in MAT.

hprof-conv heap-original.hprof heap-converted.hprof

hprof-conv是 android sdk 工具,目录\Sdk\platform-tools

Official website documentation

3. How to use

As shown in the figure below, click profile - memory in turn, and then jump to the memory interface, and then you can start recording.
1

If you can't find it, click View > Tool Windows > Profiler in the menu bar

4. Page description

On the memory page, there are two memory recording options, which are described below

4.1 Analysis of Java and Kotlin Allocation

When the recording is completed, the interface is shown in the figure below, let’s explain it separately:
insert image description here

We roughly divide it into seven parts

4.1.1 Time axis and corresponding memory

You can drag on the timeline at any time to choose which area you want to view the allocation, the top is the page and user operation records

4.1.2 tab & visualization

4.1.3

Filters
You can select which heaps to examine and how to organize the data using the two menus above the list of allocated objects, as shown in the image below:
insert image description here

  • View image heap: The system boot image, which contains classes preloaded during startup. Allocations here are guaranteed to never move or disappear.

  • View zygote heap: copy-on-write heap, where the application process is derived from the Android system.

  • View app heap: The main heap where the selected app allocates memory. If we only care about our App, just choose this option.

  • View JNI heap: Shows the heap where Java Native Interface (JNI) references are allocated and freed.

4.1.4

filter
insert image description here

  • Arrange by class: Groups all assignments by class name. This is the default option.

  • Arrange by package: Groups all assignments by package name.

  • Arrange by callstack: Groups all allocations into their corresponding callstack.

Generally, Arrang by class is used to filter classes that occupy a relatively high proportion of memory for analysis. Arrang by package locates the memory problems of its own code and third-party code according to the package name

4.1.5

Input filtering: In the input box, you can enter the class name/package name to quickly locate the memory allocation of the class under the specific class/package name

4.1.6 class name

The number of objects created and their allocated memory
This part will list all the class names after filtering, the number of objects allocated and the memory usage, including

  • Class Name: class name
  • Allocations: the number of instance objects created by this class
  • Dellocations: the number of releases in the object heap (phones below 8.0 do not have this item)
  • Total count: The number of objects in the heap that have not been recycled
  • Shallow Size: The total amount of java memory used by this class (in bytes)
  • Native Size: The total amount of native memory used by this class (only visible to android7.0+ devices) (in bytes)
  • Retained Size: The total size of the memory retained while the instance object is still alive (in bytes)

4.1.7 instance

The list of class instance objects and their detailed information
Click on a class in 6, and the information of all instance objects of this class will be displayed in the lower part, as shown in the figure
insert image description here

The Call Stack tab on the far right shows where and in which thread the instance is allocated.

4.2 Heap dump file analysis

When the recording is completed, the interface is shown in the figure below, let’s explain it separately:
insert image description here
we roughly divide it into 4 parts

4.2.1 Filters

insert image description here

  • View image heap: The system boot image, which contains classes preloaded during startup. Allocations here are guaranteed to never move or disappear.

  • View zygote heap: copy-on-write heap, where the application process is derived from the Android system.

  • View app heap: The main heap where the selected app allocates memory. If we only care about our App, just choose this option.

  • View all heaps: Check all heaps where memory is allocated.

  • Arrange by class: Groups all assignments by class name. This is the default option.

  • Arrange by package: Groups all assignments by package name.

  • Arrange by callstack: Groups all allocations into their corresponding callstack.

  • Show all class: default, show all classes

  • Show activity/fragment Leak: Show activity/fragment with memory leak

  • Show project class: Into display project-related classes

Generally, Arrang by class is used to filter classes that occupy a relatively high proportion of memory for analysis. Arrang by package locates the memory problems of its own code and third-party code according to the package name

Input filtering: In the input box, you can enter the class name/package name to quickly locate the memory allocation of the class under the specific class/package name

4.2.2 Statistics

classes: total number of class types, not instance objects
Leaks: number of memory leaks occurred
count: total number of instance objects created and used
Native Size: total amount of memory used by native c/c++
Shallow Size: total amount of memory used by java
Retained Size: The total amount of reserved memory still in use

4.2.3 class name

The number of objects created and their allocated memory
This part will list all the class names after filtering, the number of objects allocated and the memory usage, including

  • Class Name: class name
  • Allocations: the number of instance objects created by this class
  • Native Size: The total amount of native memory used by this class (only visible to android7.0+ devices) (in bytes)
  • Shallow Size: The total amount of java memory used by this class (in bytes)
  • Retained Size: The total size of the memory retained while the instance object is still alive (in bytes)

4.2.4 instance

List of class instance objects and details of their instance objects
insert image description here

Clicking on an instance will display detailed information on the memory allocation of this instance on the right, including: Fields, References:

  • Information about each field of the Fields
    instance object, including the following information:

  • Instance The name and type of this field, if it is a basic data type and String, the current value of this field will be displayed at the same time

  • Depth: The shortest number of hops reachable by this field, indicating the shortest link edge number from any GC Root to this field

  • Native Size: The memory size of this field in the native memory (only devices on Android7.0+ will see this column)

  • Shallow Size: The memory size of this field in Java memory

  • Retained Size: The memory size currently reserved by this field

  • References:
    The reference chain information of the instance object, which includes the following information:

  • Reference: The reference chain of the instance object, which can be clicked to expand in turn to display which instance objects this instance is referenced by, and the GC Root can be traced through the reference chain

  • Depth: The shortest number of hops reachable by this instance object, indicating the shortest link edge number from any GC Root to this instance object

  • Native Size: The memory size of this instance object in the native memory (only devices on Android 7.0+ will see this column)

  • Shallow Size: The memory size of this instance object in Java memory

  • Retained Size: The memory size currently reserved by this instance object

We can analyze in Fields and References,
right-click and select Go to Instance to display its instance memory data;
or select Jump to source to enter the source code where the instance object is located.

5. Recommended reading

Java column

SQL column

Data Structures and Algorithms

Android Learning Column

おすすめ

転載: blog.csdn.net/fumeidonga/article/details/132104991