From f6d03e527c2baf6cec779d89cac454debd9195df Mon Sep 17 00:00:00 2001
From: Rich Slogar Because Android is designed for mobile devices, you should always be careful about how much
-random-access memory (RAM) your application uses. Although Dalvik and ART perform
-routine garbage collection (GC), this doesn’t mean you can ignore when and where your application allocates and
+random-access memory (RAM) your app uses. Although Dalvik and ART perform
+routine garbage collection (GC), this doesn’t mean you can ignore when and where your app allocates and
releases memory. In order to provide a stable user experience that allows the system to quickly
-switch between apps, it is important that your application does not needlessly consume memory when the user
+switch between apps, it is important that your app does not needlessly consume memory when the user
is not interacting with it. Even if you follow all the best practices for Managing Your App Memory during
development (which you should), you still might leak objects or introduce other memory bugs. The
-only way to be certain your application is using as little memory as possible is to analyze your app’s
+only way to be certain your app is using as little memory as possible is to analyze your app’s
memory usage with tools. This guide shows you how to do that. The simplest place to begin investigating your application’s memory usage is the runtime log messages.
+ The simplest place to begin investigating your app’s memory usage is the runtime log messages.
Sometimes when a GC occurs, a message is printed to
logcat. The logcat output is also available in the
Device Monitor or directly in IDEs such as Eclipse and Android Studio.Interpreting Log Messages
-GC_FOR_MALLOCGC_HPROF_DUMP_HEAP
Unlike Dalvik, ART doesn't log messqages for GCs that were not explicity requested. GCs are only +
Unlike Dalvik, ART doesn't log messqages for GCs that were not explicitly requested. GCs are only printed when they are they are deemed slow. More precisely, if the GC pause exceeds than 5ms or -the GC duration exceeds 100ms. If the application is not in a pause perceptible process state, +the GC duration exceeds 100ms. If the app is not in a pause perceptible process state, then none of its GCs are deemed slow. Explicit GCs are always logged.
ART includes the following information in its garbage collection log messages:
@@ -131,15 +131,15 @@ What triggered the GC and what kind of collection it is. Reasons that may appear include:ConcurrentAllocExplicit
-HomogeneousSpaceCompactTo get a little information about what kind of memory your application is using and when, you can view -real-time updates to your app's heap in the Device Monitor:
+To get a little information about what kind of memory your app is using and when, you +can view real-time updates to your app's heap in Android Studio's +HPROF viewer or in the Device Monitor:
+Use Android Studio to view your app's memory use:
+Android Studio creates the heap snapshot file with the filename
+ Snapshot-yyyy.mm.dd-hh.mm.ss.hprof in the Captures tab.
Note: To convert a heap dump to standard HPROF format in + Android Studio, right-click a heap snapshot in the Captures view and select + Export to standard .hprof.
)
+ icon to cause heap allocation.
+ From your <sdk>/tools/ directory, launch the monitor tool.
Continue interacting with your application to watch your heap allocation update with each garbage -collection. This can help you identify which actions in your application are likely causing too much + +
Continue interacting with your app to watch your heap allocation update with each garbage +collection. This can help you identify which actions in your app are likely causing too much allocation and where you should try to reduce allocations and release resources.
@@ -266,9 +293,9 @@ resources.As you start narrowing down memory issues, you should also use the Allocation Tracker to get a better understanding of where your memory-hogging objects are allocated. The Allocation Tracker can be useful not only for looking at specific uses of memory, but also to analyze critical -code paths in an application such as scrolling.
+code paths in an app such as scrolling. -For example, tracking allocations when flinging a list in your application allows you to see all the +
For example, tracking allocations when flinging a list in your app allows you to see all the allocations that need to be done for that behavior, what thread they are on, and where they came from. This is extremely valuable for tightening up these paths to reduce the work they need and improve the overall smoothness of the UI.
@@ -281,7 +308,7 @@ improve the overall smoothness of the UI.Figure 2. The Device Monitor tool, -showing recent application allocations and stack traces in the Allocation Tracker.
+showing recent app allocations and stack traces in the Allocation Tracker.Note: You will always see some allocations from {@code @@ -458,7 +485,7 @@ with all the others.
.so mmap and .dex mmap.so (native) and .dex (Dalvik or ART)
+.so (native) and .dex (Dalvik or ART)
code. The Pss Total number includes platform code shared across apps; the
Private Clean is your app’s own code. Generally, the actual mapped size will be much
larger—the RAM here is only what currently needs to be in RAM for code that has been executed by
@@ -544,7 +571,7 @@ window, so this can help you identify memory leaks involving dialogs or other wi
AppContexts and ActivitiesNote: A {@link android.view.View} or {@link android.graphics.drawable.Drawable} object also holds a reference to the {@link android.app.Activity} that it's from, so holding a {@link android.view.View} or {@link -android.graphics.drawable.Drawable} object can also lead to your application leaking an {@link +android.graphics.drawable.Drawable} object can also lead to your app leaking an {@link android.app.Activity}.
To retrieve your heap dump:
+ +To retrieve your heap dump from within Android Studio, use the +Memory Monitor and +HPROF viewer. + +
You can also still perform these procedures in the Android monitor:
From your <sdk>/tools/ directory, launch the monitor tool.
If you need to be more precise about when the dump is created, you can also create a heap dump -at the critical point in your application code by calling {@link android.os.Debug#dumpHprofData +at the critical point in your app code by calling {@link android.os.Debug#dumpHprofData dumpHprofData()}.
The heap dump is provided in a format that's similar to, but not identical to one from the Java HPROF tool. The major difference in an Android heap dump is due to the fact that there are a large number of allocations in the Zygote process. But because the Zygote allocations are shared across -all application processes, they don’t matter very much to your own heap analysis.
+all app processes, they don’t matter very much to your own heap analysis.To analyze your heap dump, you can use a standard tool like jhat or the Eclipse Memory Analyzer Tool (MAT). However, first @@ -609,7 +641,7 @@ hprof-conv heap-original.hprof heap-converted.hprof
Note: If you're using the version of DDMS that's integrated into -Eclipse, you do not need to perform the HPROF converstion—it performs the conversion by +Eclipse, you do not need to perform the HPROF conversion—DDMS performs the conversion by default.
You can now load the converted file in MAT or another heap analysis tool that understands @@ -660,7 +692,7 @@ showing what your largest objects are. Below this chart, are links to couple of
Note: Most apps will show an instance of {@link android.content.res.Resources} near the top with a good chunk of heap, but this is - usually expected when your application uses lots of resources from your {@code res/} directory.
+ usually expected when your app uses lots of resources from your {@code res/} directory. @@ -699,19 +731,19 @@ to inspect the changes in memory allocation. To compare two heap dumps using MATWhile using the tools described above, you should aggressively stress your application code and try -forcing memory leaks. One way to provoke memory leaks in your application is to let it +
While using the tools described above, you should aggressively stress your app code and try +forcing memory leaks. One way to provoke memory leaks in your app is to let it run for a while before inspecting the heap. Leaks will trickle up to the top of the allocations in -the heap. However, the smaller the leak, the longer you need to run the application in order to see it.
+the heap. However, the smaller the leak, the longer you need to run the app in order to see it.You can also trigger a memory leak in one of the following ways:
You can use "-z" to filter out zygote allocations shared by all applications.
+ +Note: Android Studio provides integrated access to this conversion +process. To convert a heap dump to standard HPROF format in Android Studio, right-click a heap +snapshot in the Captures view and select Export to standard .hprof.
+ + diff --git a/docs/html/tools/performance/comparison.jd b/docs/html/tools/performance/comparison.jd index cf4b7120047e..06407173fffa 100644 --- a/docs/html/tools/performance/comparison.jd +++ b/docs/html/tools/performance/comparison.jd @@ -86,7 +86,7 @@ page.article=true
@@ -94,6 +94,7 @@ page.article=true
- Figure 1. Starting Android Device Monitor. + Figure 1. Starting Memory Monitor.
You can also start the Memory Monitor + in Android Studio: Click the Android tab in the lower-left corner of the application + window. The CPU and Memory Monitor views appear.
+ +Snapshot-yyyy.mm.dd-hh.mm.ss.hprof appears in the
+ Captures tab.Note: To visualize allocation changes over time, combine - several snapshots of the bar graph into an animated gif or video.
diff --git a/docs/html/tools/performance/memory-monitor/index.jd b/docs/html/tools/performance/memory-monitor/index.jd index 756ca1400725..a083a14b05fa 100644 --- a/docs/html/tools/performance/memory-monitor/index.jd +++ b/docs/html/tools/performance/memory-monitor/index.jd @@ -82,37 +82,18 @@ page.article=true -- Figure 4. Forcing a GC (Garbage Collection) event. + Figure 2. Forcing a GC (Garbage Collection) event.
diff --git a/docs/html/tools/studio/index.jd b/docs/html/tools/studio/index.jd index 5041b8380761..ee69d857f2b4 100644 --- a/docs/html/tools/studio/index.jd +++ b/docs/html/tools/studio/index.jd @@ -206,6 +206,25 @@ runtime window to launch the Android runtime window. Click the MemoryFigure 4. Monitor memory and CPU usage.
+When you're monitoring memory usage in Android Studio you can, at the same time, initiate +garbage collection and dump the Java heap to a heap snapshot in an Android-specific HPROF binary +format file. The HPROF viewer displays classes, instances of each class, and a reference tree to +help you track memory usage and find memory leaks.
+ +
+ Figure 5. HPROF viewer with heap dump.
+ +To create a snapshot of the Android app heap memory, click the
+Dump Java Heap icon (
)
+in the Memory Monitor. Android Studio creates the heap snapshot file with the filename
+Snapshot-yyyy.mm.dd-hh.mm.ss.hprof
+in the Captures tab. Double-click the heap snapshot file to open the HPROF viewer.
To convert a heap dump to standard HPROF format in Android Studio, right-click a heap +snapshot in the Captures view and select Export to standard .hprof.
+ +The Android SDK tools, such as Systrace, -- cgit v1.2.3-59-g8ed1b