Posted by Niharika Arora, Developer Relations Engineer
In Half 1 and Half 2 of our “Optimizing for Android Go” weblog sequence, we mentioned why we should always think about constructing for Android Go and methods to optimize your app to carry out effectively on Go units. On this weblog, we’ll speak concerning the instruments which helped Google optimize their Google apps efficiency.
Instruments
Monitoring Reminiscence
Analyze Reminiscence Footprint
- Resident Set Measurement (RSS): The variety of shared and non-shared pages utilized by the app
- Proportional Set Measurement (PSS): The variety of non-shared pages utilized by the app and a fair distribution of the shared pages (for instance, if three processes are sharing 3MB, every course of will get 1MB in PSS)
- Be aware: Non-public Set Measurement (PSS) = Non-public reminiscence + (shared reminiscence / the variety of processes sharing).
- Distinctive Set Measurement (USS): The variety of non-shared pages utilized by the app (shared pages will not be included)
PSS is beneficial for the working system when it needs to know the way a lot reminiscence is utilized by all processes since pages don’t get counted a number of instances. PSS takes a very long time to calculate as a result of the system wants to find out which pages are shared and by what number of processes. RSS does not distinguish between shared and non-shared pages (making it quicker to calculate) and is best for monitoring modifications in reminiscence allocation.
So, which technique do you have to select? The selection relies on the utilization of shared reminiscence.
For instance, if the shared reminiscence is being utilized by the appliance solely then we should always use the RSS method. Whereas, if the shared reminiscence is taken by the Google Play Companies then we should always use the USS method. For extra understanding, please learn right here.
2. Take a heap dump and analyze how a lot reminiscence is utilized by the operating processes. Observe
- Evaluate the Conditions.
- Developer choices Do not hold actions should be OFF.
- Use latest launch builds for testing.
- Execute the person journey you need to measure.
- Run the next command:
adb shell am dumpheap
- In a second terminal, run the next command and watch for message indicating that the “heap dump has accomplished”:
adb logcat | grep hprof
adb pull <output-file-name>
This may pull the generated file to your machine for evaluation.
To get data on native heap, learn right here :
To find out about Java heap, learn right here :
3. Perceive low-memory killer
In Android, now we have a course of known as low reminiscence killer, and this can decide a course of from the gadget and can kill that course of when the gadget is underneath low RAM, the thresholds may be tuned by OEMs. By doing so, you’re going to get again all of the reminiscence that the method was utilizing.
However what if the low reminiscence killer kills the method that the person cares about?
In Android, now we have a precedence checklist of functions and based mostly on that precedence checklist we take away the app when the low reminiscence killer comes into play. Learn extra right here.You may run this command and know :
adb shell dumpsys exercise oom
To test stats on low reminiscence killer :
adb shell dumpsys exercise lmk
Instruments
1. Debug Reminiscence utilization utilizing Perfetto
That is one the perfect instruments to search out the place all of your app reminiscence is consumed. Use Perfetto to get details about reminiscence administration occasions from the kernel. Deep dive and perceive methods to profile native and Java heap right here.
2. Examine your reminiscence utilization utilizing Reminiscence Profiler
The Reminiscence Profiler is a part within the Android Profiler that helps you determine reminiscence leaks and reminiscence churn that may result in stutter, freezes, and even app crashes. It reveals an actual time graph of your app’s reminiscence use and allows you to seize a heap dump, drive rubbish collections, and monitor reminiscence allocations. To study extra about inspecting efficiency, please test MAD expertise movies right here.
3. Make the most of meminfo
It’s possible you’ll wish to observe how your app’s reminiscence is split between several types of RAM allocation with the next adb command:
adb shell dumpsys meminfo <package_name|pid> [-d]
You may view the next seven reminiscence classes with Meminfo:
- Java heap – reminiscence allotted by Java code
- Native heap – reminiscence allotted by native code. These are greatest understood utilizing debug malloc. Allocations made by the appliance from C or C++ code utilizing malloc or new.
- Code – reminiscence used for Java, native code and a few assets, together with dex bytecode, shared libraries and fonts
- Stack – reminiscence used for each native and Java stacks. This normally has to do with what number of threads your software is operating.
- Graphics – Reminiscence used for graphics buffer queues to show pixels to the display screen, GL surfaces and textures and such.
- Non-public Different – uncategorized non-public reminiscence
- System – reminiscence shared with the system or in any other case underneath the management of the system.
- Non-public – Reminiscence used solely by the method.
- Shared – System reminiscence shared with different processes.
- Clear – Reminiscence-mapped pages that may be reclaimed when underneath reminiscence strain.
- Soiled – Reminiscence-mapped web page modified by a course of. These pages could also be written to file/swap after which reclaimed when underneath reminiscence strain.
- Debug class is tremendous helpful and offers completely different strategies for Android functions, together with tracing and allocation counts. You may examine utilization right here.
- For deeper understanding and monitoring allocations for every web page, examine web page proprietor right here.
4. Detailed evaluation utilizing showmap
The showmap command offers a way more detailed breakdown of reminiscence than pleasant meminfo. It lists the title and sizes of reminiscence maps utilized by a course of. This can be a abstract of the data obtainable at /proc/<pid>/smaps, which is the first supply of data utilized in dumpsys meminfo, aside from some graphics reminiscence.
$adb root
$ adb shell pgrep <course of>
Output – course of id
$ adb shell showmap <course of id>
Pattern Output :
Frequent reminiscence mappings are:
- [anon:libc_malloc] – Allocations constructed from C/C++ code utilizing malloc or new.
- *boot*.artwork – The boot picture. A Java heap that’s pre-initialized by loading and operating static initializers the place doable for frequent frameworks lessons.
- /dev/ashmem/dalvik-main area N – The principle Java heap.
- /dev/ashmem/dalvik-zygote area – The principle Java heap of the zygote earlier than forking a toddler course of. Also referred to as the zygote heap.
- /dev/ashmem/dalvik-[free list ] massive object area – Heap used for Java objects bigger than ~12KB. This tends to be stuffed with bitmap pixel knowledge and different massive primitive arrays.
- *.so – Executable code from shared native libraries loaded into reminiscence.
- *.{oat,dex,odex,vdex} – Compiled dex bytecode, together with optimized dex bytecode and metadata, native machine code, or a mixture of each.
5. Analyze native reminiscence allocations utilizing malloc debug
Malloc debug is a technique of debugging native reminiscence issues. It may possibly assist detect reminiscence corruption, reminiscence leaks, and use after free points. You may test this documentation for extra understanding and utilization.
Starting with Android 27, Android NDK helps Tackle Sanitizer which is a quick compiler-based software for detecting reminiscence bugs in native code. ASan detects:
- Stack and heap buffer overflow/underflow
- Heap use after free
- Stack use outdoors scope
- Double free/wild free
ASan runs on each 32-bit and 64-bit ARM, plus x86 and x86-64. ASan’s CPU overhead is roughly 2x, code dimension overhead is between 50% and 2x, and the reminiscence overhead is massive (dependent in your allocation patterns, however on the order of 2x). To study extra, learn right here.
Digital camera from the Google crew used it and automatic the method that may run and get again to them within the type of alerts in case of Asan points, and located it actually handy to repair reminiscence points missed throughout code authoring/overview.
Monitoring Startup
Analyze Startup
1. Measure and analyze time spent in main operations
After getting a whole app startup hint, take a look at the hint and measure time taken for main operations like bindApplication, activitystart and so on.
Have a look at total time spent to
- Determine which operations occupy massive time frames and may be optimized
- Determine which operations eat excessive time the place it isn’t anticipated.
- Determine which operations trigger the primary thread to be blocked
2. Analyze and determine completely different time consuming operations and their doable options
- Determine all time consuming operations.
- Determine any operations which aren’t speculated to be executed throughout startup (Ideally there are a whole lot of legacy code operations which we aren’t privy to and never simply seen when our app code for efficiency)
- Determine which all operations are completely wanted OR could possibly be delayed till your first body is drawn.
3. Verify Dwelling exercise load time
That is your app’s residence web page and sometimes efficiency will rely on the loading of this web page. For many apps, there’s a whole lot of knowledge displayed on this web page, spanning a number of layouts and processes operating in background. Verify the house exercise structure and particularly take a look at the Choreographer.onDraw technique of the house exercise.
- Measure time taken for total operations of measure, draw,inflate,animate and so on.
- Have a look at body drops.
- Determine layouts taking excessive time to render or measure.
- Determine property taking a very long time to load.
- Determine layouts not wanted however nonetheless getting inflated.
Instruments
- To know CPU utilization, thread exercise, body rendering time, Perfetto would be the greatest software.
- Report hint both through the use of command line or UI instruments like Perfetto. Add app package deal title with the -a tag, to filter knowledge in your app. Some methods to seize hint :
- Produces a report combining knowledge from the Android kernel, such because the CPU scheduler, disk exercise, and app threads.
- Finest when enabled with customized tracing to know which technique or a part of code is taking how lengthy after which develop can dig deep accordingly.
- Perceive Atrace, and ftrace whereas analyzing traces by way of Perfetto.
2. App Startup library
The App Startup library offers a simple, performant technique to initialize parts at software startup. Each library builders and app builders can use App Startup to streamline startup sequences and explicitly set the order of initialization. As an alternative of defining separate content material suppliers for every part you could initialize, App Startup lets you outline part initializers that share a single content material supplier. This could considerably enhance app startup time. To seek out methods to use it in your app, refer right here.
3. Baseline Profiles
Baseline Profiles are an inventory of lessons and strategies included in an APK utilized by Android Runtime (ART) throughout set up to pre-compile important paths to machine code. This can be a type of profile guided optimization (PGO) that lets apps optimize startup, scale back jank, and enhance efficiency for finish customers. Profile guidelines are compiled right into a binary kind within the APK, in property/dexopt/baseline.prof.
Throughout set up, ART performs Forward-of-time (AOT) compilation of strategies within the profile, leading to these strategies executing quicker. If the profile accommodates strategies utilized in app launch or throughout body rendering, the person experiences quicker launch instances and/or lowered jank. For extra data on utilization and benefits, refer right here.
4. Android CPU Profiler
You need to use the CPU Profiler to examine your app’s CPU utilization and thread exercise in actual time whereas interacting together with your app, or you’ll be able to examine the small print in recorded technique traces, perform traces, and system traces. The detailed data that the CPU Profiler information and reveals is set by which recording configuration you select:
- System Hint: Captures fine-grained particulars that can help you examine how your app interacts with system assets.
- Technique and performance traces: For every thread in your app course of, yow will discover out which strategies (Java) or capabilities (C/C++) are executed over a interval, and the CPU assets every technique or perform consumes throughout its execution.
5. Debug API + CPU Profiler
To offer apps the power to begin and cease recording CPU profiling after which examine in CPU profiler is what Debug API is all about. It offers details about tracing and allocation counts the identical manner utilizing startMethodTracing() and stopMethodTracing().
- Debug API is designed for quick intervals or situations which are onerous to begin/cease recording manually. (Used it as soon as to search out the lock rivalry taking place attributable to some library)
- To generate a technique hint of an app’s execution, we will instrument the app utilizing the Debug class. This manner builders get extra management over precisely when the gadget begins and stops recording tracing data.
6. MacroBenchmark
- Measures Scrolling / Animation rendering time.
- Use a UiAutomator to set off a scroll or animation. (It captures body timing / janks for regardless of the app is doing. Scroll and animations are simply the best methods to supply frames the place jank is noticeable)
- Requires Android 10 or increased to run the assessments.
- Can view traces on Systrace/Perfetto Traces.
- FrameTimingMetric is the API reporting body time in ms.
This pattern can be utilized for app instrumentation.
- Added in API degree 30 and supported within the newest Studio Bumblebee preview (2021.1)
- Makes use of simpleperf with personalized construct scripts for profiling.
- Simpleperf helps profiling java code on Android >M.
- Profiling a launch construct requires one in all following:
- Gadget to be rooted
- Android >=O, use a script wrap.sh and make android::debuggable=“true” to allow profiling.
- Android >=Q, add profileable in manifest flag.
<profileable android:shell=[“true” | “false”] android:allow=[“true” | “false”] />
- Useful in app instrumentation with Macrobenchmark.
8. MicroBenchmark
The Jetpack Microbenchmark library lets you shortly benchmark your Android native code (Kotlin or Java) from inside Android Studio. The library handles warmup, measures your code efficiency and allocation counts, and outputs benchmarking outcomes to each the Android Studio console and a JSON file with extra element. Learn extra right here.
Monitoring App dimension
No person needs to obtain a big APK that may eat most of their Community/Wifi Bandwidth, additionally most significantly, area contained in the cell gadget.
The dimensions of your APK has an impression on how briskly your app hundreds, how a lot reminiscence it makes use of, and the way a lot energy it consumes. Decreasing your app’s obtain dimension permits extra customers to obtain your app.
Instruments
- Use the Android Measurement Analyzer
The Android Measurement Analyzer software is a straightforward technique to determine and implement many methods for decreasing the dimensions of your app. It’s obtainable as each an Android Studio plugin in addition to a standalone JAR
- Take away unused assets utilizing Lint
The lint software, a static code analyzer included in Android Studio, detects assets in your res/ folder that your code does not reference. When the lint software discovers a probably unused useful resource in your venture, it prints a message like the next instance.
Be aware : Libraries that you just add to your code could embrace unused assets. Gradle can robotically take away assets in your behalf if you happen to allow shrinkResources in your app’s construct.gradle file.
- Native animated picture decoding
In Android 12 (API degree 31), the NDK ImageDecoder API has been expanded to decode all frames and timing knowledge from pictures that use the animated GIF and animated WebP file codecs. When it was launched in Android 11, this API decoded solely the primary picture from animations in these codecs.
Use ImageDecoder as an alternative of third-party libraries to additional lower APK dimension and profit from future updates associated to safety and efficiency.
- Crunch PNG recordsdata utilizing aapt
The aapt software can optimize the picture assets positioned in res/drawable/ with lossless compression in the course of the construct course of. For instance, the aapt software can convert a true-color PNG that doesn’t require greater than 256 colours to an 8-bit PNG with a colour palette. Doing so leads to a picture of equal high quality however a smaller reminiscence footprint. Learn extra right here.
Be aware : Please test Android developer documentation for all of the helpful instruments which can assist you determine and assist repair such efficiency points.
Recap
This a part of the weblog captures the instruments utilized by Google to determine and repair efficiency points of their apps. They noticed nice enhancements of their metrics. Most Android Go apps may gain advantage from making use of the methods described above. Optimize and make your app pleasant and quick in your customers!