Optimize app memory
Stay organized with collections
Save and categorize content based on your preferences.
Memory is a valuable resource in any software development environment, but it's
even more valuable on a mobile operating system where physical memory is often
constrained. This is especially true for natively low-memory devices found
commonly with Android (Go edition). There are a few ways to help optimize memory
in your app to help it run smoothly in these environments.
Best practices
Release cache memory
There may not be enough memory to keep background processes running as you
would in a typical environment. In this case, you can use
onTrimMemory()
to trim unneeded memory from your app's process. To best
identify the current trim level for your app, use
ActivityManager.getMyMemoryState(RunningAppProcessInfo)
and optimize or trim any unnecessary resources. For example, you can trim
unnecessary memory usage from
expressions, search, view cache, or openable extensions to reduce the number of
times your app experiences crashes or ANRs due to low memory.
Task scheduling
Concurrent scheduling can lead to multiple memory intensive operations to run
in parallel, leading to competition for resources exceeding the peak memory
usage of an app. Try to appropriately allocate resources by separating processes
into CPU intensive, low latency tasks in the right
thread pool to run on devices that may face
various resource constraints.
Memory leaks
Various tools, such as
Memory Profiler in Android Studio and
Perfetto are
specifically available to help find and reduce memory leaks within your app.
It's highly encouraged that you use these tools to identify and fix potential
memory issues to allow other components of your app to run without additional
pressure on the system.
Other tips
- Large images or drawables consume more memory in apps. Identify and optimize
large or full-colored bitmaps to reduce memory usage.
- Try to choose other options for GIFs in your app when building for Android
(Go edition) as GIFs consume a lot of memory.
- You can reduce PNG file sizes without losing image quality using tools like
WebP, pngcrush, and pngquant. All of these tools
can reduce PNG file size while preserving the perceptive image quality.
- The aapt tool can optimize the image resources placed in
res/drawable/
with lossless compression during the build process. For example, the aapt tool
can convert a true-color PNG that does not require more than 256 colors to an
8-bit PNG with a color palette. Doing so results in an image of equal quality
but a smaller memory footprint.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-05-09 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-05-09 UTC."],[],[],null,["# Optimize app memory\n\nMemory is a valuable resource in any software development environment, but it's\neven more valuable on a mobile operating system where physical memory is often\nconstrained. This is especially true for natively low-memory devices found\ncommonly with Android (Go edition). There are a few ways to help optimize memory\nin your app to help it run smoothly in these environments.\n\nBest practices\n--------------\n\n### Release cache memory\n\nThere may not be enough memory to keep background processes running as you\nwould in a typical environment. In this case, you can use\n[`onTrimMemory()`](/reference/android/content/ComponentCallbacks2#onTrimMemory(int))\nto trim unneeded memory from your app's process. To best\nidentify the current trim level for your app, use\n[`ActivityManager.getMyMemoryState(RunningAppProcessInfo)`](/reference/android/app/ActivityManager#getMyMemoryState(android.app.ActivityManager.RunningAppProcessInfo))\nand optimize or trim any unnecessary resources. For example, you can trim\nunnecessary memory usage from\nexpressions, search, view cache, or openable extensions to reduce the number of\ntimes your app experiences crashes or ANRs due to low memory.\n\n### Task scheduling\n\nConcurrent scheduling can lead to multiple memory intensive operations to run\nin parallel, leading to competition for resources exceeding the peak memory\nusage of an app. Try to appropriately allocate resources by separating processes\ninto CPU intensive, low latency tasks in the right\n[thread pool](/guide/background/threading) to run on devices that may face\nvarious resource constraints.\n\n### Memory leaks\n\nVarious tools, such as\n[Memory Profiler](/studio/profile/memory-profiler) in Android Studio and\n[Perfetto](https://perfetto.dev/docs/case-studies/memory) are\nspecifically available to help find and reduce memory leaks within your app.\nIt's highly encouraged that you use these tools to identify and fix potential\nmemory issues to allow other components of your app to run without additional\npressure on the system.\n\n### Other tips\n\n- Large images or drawables consume more memory in apps. Identify and optimize large or full-colored bitmaps to reduce memory usage.\n- Try to choose other options for GIFs in your app when building for Android (Go edition) as GIFs consume a lot of memory.\n- You can reduce PNG file sizes without losing image quality using tools like [WebP](/studio/write/convert-webp), pngcrush, and pngquant. All of these tools can reduce PNG file size while preserving the perceptive image quality.\n- The aapt tool can optimize the image resources placed in `res/drawable/` with lossless compression during the build process. For example, the aapt tool can convert a true-color PNG that does not require more than 256 colors to an 8-bit PNG with a color palette. Doing so results in an image of equal quality but a smaller memory footprint."]]