优化应用内存
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
内存在任何软件开发环境中都是一项宝贵资源,但在移动操作系统中,物理内存通常有限,因此 RAM 更为宝贵。对于搭载 Android(Go 版本)的设备来说尤其如此,因为这类设备的内存原本就比较低。您可以通过多种方式优化应用的内存,使应用在这些环境中顺畅运行。
最佳实践
释放缓存内存
设备可能没有足够的内存来保持后台进程像在典型环境中那样运行。在这种情况下,您可以使用 onTrimMemory()
从应用的进程中削减不必要的内存用量。为了更好地识别应用的当前精简级别,请使用 ActivityManager.getMyMemoryState(RunningAppProcessInfo)
优化或削减任何不必要的资源。例如,您可以从表达式、搜索、视图缓存或可打开的扩展程序中削减不必要的内存用量,以减少应用因内存不足而发生崩溃或 ANR 的次数。
任务调度
并发调度可能会导致多个内存密集型操作并行运行,从而导致资源争用超出应用的内存用量峰值。尝试在适当的线程池中将进程拆分为多个 CPU 密集型、低延迟的任务,以便在可能面临各种资源限制的设备上运行。
内存泄漏
各种工具,例如内存分析器(在 Android Studio 中)和 Perfetto 专门用于帮助您查找并减少应用中的内存泄漏。强烈建议您使用这些工具找出并修复潜在的内存问题,让您的应用的其他组件能够在不对系统造成额外压力的情况下正常运行。
其他提示
- 大型图片或可绘制对象会在应用中消耗更多内存。识别和优化大尺寸或全色位图以减少内存用量。
- 在针对 Android(Go 版本)进行构建时,请尝试在您的应用中选择其他选项替代 GIF,因为 GIF 会消耗大量内存。
- 您可以使用 WebP、pngcrush 和 pngquant 等工具缩减 PNG 文件的大小,同时不损失画质。所有这些工具都可以缩减 PNG 文件的大小,同时保持肉眼感知的画质不变。
- aapt 工具可以在构建流程中通过无损压缩来优化放置在
res/drawable/
中的图片资源。例如,aapt 工具可以通过调色板将不需要超过 256 种颜色的真彩色 PNG 转换为 8 位 PNG。这样做可以生成质量相同但内存占用量更小的图片。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],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."]]