2.AS memory analysis

reference

http://www.jianshu.com/p/216b03c22bb8?nomobile=yes

http://www.jianshu.com/p/c49f778e7acf

https://developer.android.com/studio/profile/android-profiler?hl=zh_cn

https://developer.android.com/studio/profile/memory-profiler.html?hl=zh_cn#capture-heap-dump

Memory analysis by Android monitor

 

You first need to understand a concept, refers to memory leaks, this should be recovered memory, also reside in memory.

In general, high-density mobile phones, a page will consume about 20M of memory, if it is found to exit the interface, the program memory to reduce the delay in the case, probably there was a serious memory leak. We can repeatedly enter the interface, and then click the dump java heap this button, then Android Studio started to work.

Automatically opens after successful hprof dump file, the file is named Snapshot + time, but this is not the standard hprof file you want to use, then need to right-mat analysis files for conversion.

captures hprof file will be saved in the project root directory where. But you need to be open:

 

After opening hprof file:

 

name

description

Class name

The class name

Total Count

The total number of instances of the class

Heap Count

The number of instances of the class of the selected stack

Sizeof

Space occupied by a single instance (each instance, if the space occupied by the display sizes like 0)

Shallow Size

Sum of all instances heap size (Heap Count * Sizeof)

Retained Size

All instances of the class dominated by memory size

   

Instance

Specific examples

Reference Tree

Selected instance, as well as references to this reference.

Depth

The depth of the shortest path to the root node of the selected instance GC

Shallow Size

Examples of the size of the selected

Dominating Size

Examples of the selected memory size dominated

 

Track Memory Allocation

If we want to understand memory allocation more detailed information, you can use Allocation Traker to view memory in the end is what occupied. Usage is very simple:

Click tracking memory allocation

Tap a track, and then tap interface is as follows .alloc file will open automatically after stop tracking, stop tracking, after opening:

When you want to view the source code of a method, the method of selecting the right, click Jump to source it

Time query execution method

If we want to perform the method of observing time, you need to come to CPU interface

Click Start Method Tracking, some time before one click, trace file is automatically opened,

result

Non-exclusive time: a function of CPU time, contains an internal call other functions of CPU time.
Exclusive time: a function of CPU time, but no other function calls internal CPU time.

 

The new Android Profiler

In the new version called Android Profiler, use roughly the same.

Android Profiler with Android 5.0 (API level 21) and later are compatible.

 

This interface has cpu, memory, network and other data, can point to open a module.

Android 1 Profiler display processes and equipment currently being analyzed.

2 In the Sessions pane, select the session you want to view, or start a new analysis session.

3 Use the zoom control button to view the Timeline range, or use the Attach to live button to jump to real-time updates.

4 Event Timeline displays events and user input, including keyboard Activity, volume control and change the screen rotation.

5 shared timeline view, including CPU, memory, network, and power consumption graph.

This shared timeline view to display only the timeline chart. To use the detailed analysis tools, please click on the chart and you want to check performance data corresponding. For example, to use tools to check heap memory allocation and tracking data, please click on the Memory chart.

Not all analytical data are visible by default. If you see a message that says "Advanced profiling is unavailable for the selected process", you can run configuration enable advanced analysis to see additional data.

 

Conversation

You can save as analyzer data sessions, which will remain there until you quit Android Studio.

By recording and analyzing information in multiple sessions and switch between them, you can compare a variety of resource usage scenarios.

  • To start a new session, click Start a new profiling session + button, then select an application process from the drop-down menu that appears.

After you record or capture function data dump heap, Android Studio will set the appropriate data (your application and network Activity) to the current session as a separate entry.

 

  • To stop adding data to the current session, click Stop the current profiling session icon.

 

  • Exported when running Android Studio to import track before recording, click Start new profiler session icon, then select Load from file.

 

Enabling advanced analysis

To run equipped with Android 7.1 or earlier displays advanced analytics to your data when the device, Android Studio monitoring logic must be injected into the application you compile.

Provides advanced analysis features include:

  • All events Timeline Analyzer window
  • Memory Profiler in the number of allocated objects
  • Garbage collection in the event Memory Profiler
  • Network Profiler detailed information about all of the transferred file

Note: If your device is equipped with the Android 8.0 version or later, you can use these functions in the case by default.

 

To enable advanced analysis features, follow these steps:

  1. Select Run> Edit Configurations.
  2. Select your application modules in the left pane.
  3. Click the Profiling tab, then check the Enable advanced profiling.
  4. Recompile and run your application.

 

Advanced analysis configuration causes the compiler to process slows down, so only if you want to start analyzing your application, it should enable the configuration.

 

Note: The advanced analysis capabilities are not available for native code native code. If your application is purely native application native app (free Java Activity class), you can not use advanced analytics. If your application uses JNI, you can use some of the advanced analysis functions (such as event timeline, garbage collection events, Java objects and Java-based distribution network Activity) based on native code and distribution network Activity, but can not be detected.

 

Use Memory Profiler and view the Java heap memory allocation

https://developer.android.com/studio/profile/memory-profiler?hl=zh_cn

 

Memory Profiler is Android Profiler a component that helps you identify Caton may cause the application to freeze or crash of memory leaks and memory jitter. It shows a real-time application memory usage graphs so you can capture heap dumps, garbage collection and enforcement track memory allocation.

Memory Profiler Overview

When you first open the Memory Profiler, you will see a timeline showing the details of the application memory usage, and can use various tools to enforce garbage collection, capture and record the heap dump memory allocation.

图 1. Memory Profiler

 

As shown in FIG. 1, the default view Memory Profiler include the following:

  1. Used to enforce button garbage collection events.

Note: Only the connector is mounted to the Android 7.1 (API level 25) or the earlier device, the right side of this button will record memory allocations button.

 

  1. A pull-down menu, a frequency analyzer to capture the specified memory allocation. Select the appropriate option to help you improve application performance in the analysis .
  2. For the enlargement / reduction button axis.
  3. Real-time data memory buttons for jumping to.
  4. Event timeline that shows the active user input events and screen rotation events.
  5. Timeline memory usage, which includes the following:
  • Stacking a diagram showing how much memory each category of memory currently used, such as key color and the top left of the y-axis shown in FIG.
  • A dotted line represents the distribution of the number of objects, as shown in the right side of the y-axis.
  • Trash can icon, the icon for each garbage collection event.

 

However, if you are using a carry Android 7.1 or earlier devices, not all of the data analysis are visible by default. If you see a message that says "Advanced profiling is unavailable for the selected process", you need to enable advanced analysis to see the following:

  • Event Timeline
  • The number of objects allocated
  • Garbage collection event

Start advanced analysis to see the top.

On Android 8.0 and higher, always advanced analytics applications that can be enabled debugging.

 

How to calculate memory

All private memory page numbers (Figure 2) you see at the top Memory Profiler based on your application submitted under Android system mechanisms. This count does not include shared with other systems or applications page.

Memory count legend at the top of FIG 2. Memory Profiler

 

Memory, the count in the following categories:

  • Java: Java memory allocation from Kotlin or object code.
  • Native: memory allocation from C or C ++ code object.

Even without the use of C ++ your application, you may also see some native memory used here, because the Android framework uses native memory on your behalf deal with a variety of tasks, processing the image resources and other graphics, even if you write code such as Kotlin or using Java language.

 

  • Graphics: pixel graphics buffer queue memory (including surface GL, GL texture, etc.) used in the screen display. (Please note that this is a shared memory with the CPU, not the GPU dedicated memory.)
  • Stack: memory your application in native Java stack and stack usage. This is usually run with your application how many threads related.
  • Code: Your application code and resources for processing (such as dex bytecode, optimized the code or compiled dex, .so libraries and fonts) of memory.
  • Others: your system memory used by the application not sure how to classify it.
  • Allocated: Your application assigned Java / Kotlin number of objects. This number is not included in C or C ++ objects allocated.

If the connection to carry Android 7.1 and earlier devices only in Memory Profiler connects to the application you are running, began this assignment count. So, you start to analyze any object previously allocated will not be counted. However, Android 8.0 and later device comes with a built-in analysis tool that can track all allocations, therefore, on Android 8.0 and later, this number always means that your Java application to be processed and the total number of objects.

 

 

Compared with the previous Android Monitor tool memory count, the new Memory Profiler in different ways to record your memory, so your memory usage now looks likely to be higher. Memory Profiler to monitor more categories, which will increase the total memory usage, but if you only care about the Java heap memory, "Java" digital items should be similar to the previous numerical tools. However, Java figures may not be exactly the same numbers you see in the Android Monitor, this is because the new figures included all the physical memory pages from the application of Java heap since Zygote derived assigned to it. Therefore, it can accurately reflect the actual use of your application how much physical memory.

 

Note: Use equipped with Android 8.0 (API level 26) and higher devices when, Memory Profiler also displays some of the false memory usage of native applications, and these are actually memory analysis tool. For about 100,000 objects, most will report the amount of memory increases 10MB. In future versions of the IDE, these numbers from your data filtered out.

View Memory Allocation

Memory allocation shows you every Java objects in memory and how the JNI references are assigned.

Specifically, Memory Profiler displays the following information about the object assignment for you:

  • What types of objects are allocated and how much space they use.
  • Each allocation stack trace, which is included in the thread.
  • And when the object is unassigned (Android only later when the device is mounted or 8.0).

 

If your device is equipped with Android version 8.0 or later , you can view the object allocation at any time, the specific steps are as follows: drag to select which area allocation (as shown in the video 1) to view on the time axis. Do not need to start a recording session, because Android 8.0 and later devices that came with built-in analysis tools, sustainable track your application distribution.

 

 

If your device is equipped with Android 7.1 or earlier , click the Record memory allocations Memory Profiler icon in the toolbar. When recording, Memory Profiler keeps track of all assigned occurred in your application. When finished, click the Stop recording icon (the same button, watch the video 2) to view the assignment.

 

(Or use the mounting device Android 7.1 or earlier completion of the recording session) after a selected time axis region, assignment list is displayed below the time axis, grouped by class name, in accordance with its count of heap sort .

Note: On Android 7.1 and earlier, you can record up to 65535 allocated. If your recording session exceeds this limit, only the records stored in the latest allocation of 65,535. (On Android 8.0 and higher, there is no practical limit.)

 

To check the allocation record, follow these steps:

  1. Browse the list to find an unusually large heap count and possible leakage of the object. Find a class known to help, click on the Class Name column header to sort alphabetically. Then, click on a category name. On the right of the Instance View pane appears, displaying each instance of the class, as shown in FIG.

Alternatively, you can quickly find the object by clicking the Filter icon to search for a specific category.

 

  1. In the Instance View pane, click an instance. In this case Call Stack label will appear below, show the instance where and which is assigned to the thread.
  2. In the Call Stack tab, right-click on any line and select Jump to Source, to open the code editor.

 

You can use the assigned objects above the list to select two menu to check the heap and how to organize data.

From the left side of the menu, select the check heap:

  • default heap: the stack when the system is not specified.
  • image heap: system boot image, comprising a preloaded classes during startup. Assigned here to ensure that never move or disappear.
  • zygote heap: heap copy-on-write, which is derived from the application process Android system.
  • app heap: your application in which the main heap to allocate memory.
  • JNI heap: Display Java Native Interface (JNI) is to allocate and free heap references to what position.

 

From the menu on the right to choose how to arrange the distribution:

  • Arrange by class: group all allocated according to the class name. This is the default option.
  • Arrange by package: group all allocated according to the package name.
  • Arrange by callstack: assign all the packets to their corresponding call stack.

 

Improve application performance analysis

In order to improve application performance analysis, Memory Profiler for memory allocation regularly sampled by default. When tested on a running API level 26 or higher devices, you can use the Allocation Tracking drop-down menu to change this behavior.

The available options are as follows:

  • Full: capture all object allocation memory. This is the Android Studio 3.2 and earlier versions of the default behavior. If you have a large number of objects assigned application, it may be observed that the application runs slowed significantly in the analysis.
  • Sampled: regular objects allocated memory is sampled. This is the default option when analyzing a small impact on application performance. Application allocate a large object in a short time may still show significant slowing.
  • Off: Stop track memory allocation applications.

 

Note: By default, Android Studio will stop tracking real-time CPU allocation when performing recording and re-open the function after the recording is complete CPU. You can CPU record configuration dialog to change this behavior.

 

Analyze memory skills

When using the Memory Profiler, respond to your application code to put pressure and try to force a memory leak. One way to trigger a memory leak in the application is, let it run for a while, and then check the heap. Leak in the heap may gradually converging to the top of the distribution. However, the smaller the leak, the longer it takes to see leakage need to run the application.

You can also trigger a memory leak in the following ways:

  • Activity in different state, the first device from portrait to landscape rotation, and then back rotation, such rotation is repeated a plurality of times. Rotating equipment often makes use leak Activity , Context or View objects, because the system will re-create the Activity , and if your application remains elsewhere in reference to one of these objects, the system will not be garbage collected.
  • Activity in different state, between your application and other applications handover (to navigate the main screen, and then return to your application).

 

Memory analysis through the mat

Download  http://eclipse.org/mat/downloads.php

 

By Android Studio's own interface, view the memory leaks is not very smart, we can use third-party tools, common tool is the MAT, and here we need to download a separate version of the MAT.

It should remind you, MAT does not tell us exactly where a memory leak occurs , but will provide a lot of data and clues, we need to analyze these data come and go their own judgment in the end is not really happening memory leaks.

 

Android Studio dump will automatically open after a successful hprof file, the file is named Snapshot + time, but this is not the standard hprof file you want to use, then need to right-mat analysis files for conversion.

captures hprof file will be saved in the project root directory where.

 

Then open the exported reason hprof with MAT (File-> Open heap dump) MAT will help us to analyze memory leaks

FIG MAT is the beginning of the open interface

There are a lot of functions, but generally use Histogram, Dominator Tree, Leak Suspects.

  • Histogram: lists each class how many objects,
  • Dominator Tree: list large object,
  • Leak Suspects: comes with a memory leak checking tool that can help us analyze the object may lead to memory leaks.
  • search for

Histogram

Each category lists how many objects

Dominator Tree

Listed large object

Leak Suspects

Comes with a memory leak checking tool that can help us analyze the object may lead to memory leaks,

Automatic analysis of the cause of memory leaks

 

search for

MAT also supports search, so you can directly search category you think is most likely to have memory leaks.

 

Memory leaks are a general activity, so you can directly look for activity analysis can be.

  1. Click on the icon OQL
  2. In the window, enter select * from instanceof android.app.Activity and press Ctrl + F5 or! Button
  3. A miracle happened, and now you find that leak a lot of activity
  4. This is really quite not optimistic, we have to analyze why the GC does not reclaim it

 

Click on an activity subject - "Select the Right Path to GC roots-" and then select exclude weak / soft references (references excluded weak),

In the new window that opens, you can find your Activity is referenced by this $ 0, it is actually an anonymous class reference to the current class. this $ 0 has been cited callback, then it is referenced Message in a series of next, and finally to the main thread until the end. In any case you create a non-static inner classes in the class, inner classes will (automatically) has a strong reference to the current class.

 

 

How do we judge may have a problem of method?

To view the number of calls by the method and exclusive time, usually judging method is:

  1. If the method invocation few times, but each time it takes a call to function for a long time, there may be a problem.
  2. If the occupation itself is not long, but the calls are very frequent functions may also be a problem.

Guess you like

Origin www.cnblogs.com/muouren/p/11746446.html