最佳化應用程式記憶體
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
記憶體在任何軟體開發環境都是非常寶貴的資源,而在行動裝置作業系統上尤其如此,因為行動裝置的實體記憶體通常十分有限。對 Android (Go 版本) 常見的低記憶體裝置而言更是如此。您可以透過下列幾種方式最佳化應用程式中的記憶體,幫助應用程式在這些環境中能順暢運作。
最佳做法
釋出快取記憶體
記憶體可能不足,可能無法像在一般環境中一樣執行背景程序。在這種情況下,您可以使用 onTrimMemory()
從應用程式的程序中修剪不需要的記憶體。為了最好地確定應用程式目前的修剪程度,請使用 ActivityManager.getMyMemoryState(RunningAppProcessInfo)
,並最佳化或修剪任何不必要的資源。例如,您可以透過運算式、搜尋、檢視快取或可開啟的擴充功能,調整不必要的記憶體用量,從而減少應用程式因為記憶體不足而發生當機或 ANR 的次數。
工作排程
並行排程可能會導致同時執行會佔用大量記憶體的多項作業,導致資源超出應用程式尖峰記憶體用量。請嘗試將程序分成正確的執行緒集區中的 CPU 密集型低延遲工作,以便在可能遇到各種資源限制的裝置上執行。
記憶體流失
諸如 Android Studio 中的記憶體分析器和 Perfetto 等各種工具,專門用於找出並減少應用程式中的記憶體流失情形。強烈建議您使用這些工具,找出並修正潛在的記憶體問題,讓應用程式的其他元件在不對系統施加額外壓力的情況下執行。
其他提示
- 大型圖片或可繪項目會耗用應用程式中較多的記憶體。識別並最佳化大型或全彩點陣圖,以降低記憶體用量。
- 為 Android (Go 版本) 建構 GIF 時,請試著在應用程式中選擇 GIF 的其他選項,因為 GIF 會耗用大量記憶體。
- 您可以使用 WebP、pngcrush 和 pngquant 等工具縮減 PNG 檔案大小,同時不犧牲圖片品質。這些工具都能縮減 PNG 檔案的大小,同時維持良好的圖片品質。
- aapt 工具可以在建構過程中,以無損壓縮的方式調整
res/drawable/
中的圖片資源。舉例來說,aapt 工具可以將不需要超過 256 種顏色的真實色彩 PNG 轉換成具有調色盤的 8 位元 PNG。這樣圖片品質相同,但記憶體使用量較低。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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."]]