定位Android卡顿与ANR性能瓶颈
沿关键操作追踪主线程、启动、内存与渲染负载,区分实测问题和代码风险并提出验证方法。
完整提示词
# ANDROID PERFORMANCE AND ANR HUNTER I want you to perform a maximally deep, systematic, evidence-first, and production-oriented performance analysis of the entire Android application, with a dedicated focus on ANR, main-thread blocking, UI jank, startup latency, memory pressure, and sluggish critical user flows. Primary objective: > Pinpoint concrete places where the application can become sluggish, freeze the UI, drop frames, consume excessive memory, needlessly overload CPU/GPU, or trigger an ANR, strictly distinguishing genuine performance bottlenecks from trivial micro-optimizations. This is not: - a generic "optimize performance" checklist - advice to blindly offload everything onto a background thread - automated coroutine insertions - a witch hunt for every single heap allocation - labeling every recomposition as an acute bug - randomly throwing in-memory caches everywhere - automated introduction of Baseline Profiles - drawing conclusions from debug builds - fabricating benchmark numbers The focus is on: - the actual critical path - the actual execution thread - operation frequency - payload and dataset volume - lifecycle context - production runtime behavior - measurable user impact Priority: **ANR risk > user-blocking latency > jank > startup > memory pressure > CPU/battery > micro-optimization** If an issue is not backed by runtime profiling, strictly distinguish: **MEASURED PROBLEM** from: **CODE-LEVEL PERFORMANCE RISK** --- # 1. DETERMINE PERFORMANCE CONTEXT Prior to analysis, identify: - Android Gradle Plugin - Kotlin version - minSdk - targetSdk - Compose or Views - Coroutines - Room - Network stack - Image loading library - Media3 if present - WorkManager - Foreground and background services - Dependency injection framework - Analytics and crash reporting SDKs - Build types and flavors - R8 minification and optimization settings - Baseline Profiles configuration - Macrobenchmark test modules - Jetpack App Startup library Do not analyze performance based on false architectural assumptions. --- # 2. SEPARATE DEBUG AND RELEASE Debug builds can run significantly slower. Especially with: - Jetpack Compose - Heavy logging - Debugger attachment - Layout and memory inspectors - StrictMode checks - Unoptimized DEX bytecode Never assert: > this screen is slow merely because the debug build lags. If release behavior has not been tested: **RELEASE PERFORMANCE: NOT VERIFIED** --- # 3. MAP CRITICAL PATH PERFORMANCE For each critical user flow, trace the execution pipeline: ```text User action ↓ UI handler ↓ ViewModel ↓ Repository ↓ Database / Network / CPU work ↓ State update ↓ UI render ``` Identify the dispatcher or thread at each hop: - Main thread - Dispatchers.IO - Dispatchers.Default - Background worker - Unknown / unconfined The goal is to determine where the user is genuinely blocked waiting. --- # 4. CRITICAL USER FLOWS Identify and prioritize primary journeys: - App startup - Authentication / login - Home screen / dashboard - List loading and scrolling - Search query and suggestions - Detail screen opening - Save and edit mutations - File import / export - Media playback initiation - Data synchronization - Screen-to-screen navigation Do not optimize secondary features before critical user flows are performant. --- # 5. ANR MODEL An Application Not Responding (ANR) error is not merely "a slow app." Look for scenarios where the main thread is blocked from processing: - Touch and key input events (5-second threshold) - Activity / Fragment lifecycle transitions - BroadcastReceiver execution (10-second threshold) - Service callbacks - System window interactions Never report an ANR without a concrete, demonstrable main-thread blocking path. --- # 6. MAIN THREAD INVENTORY Identify operations repository-wide that can execute on the Main thread. Check for: - Database access - File I/O - Network socket operations - Data parsing (JSON, XML, Protocol Buffers) - Cryptographic operations - Compression / decompression - Bitmap transformations - In-memory sorting and filtering of large datasets - Blocking locks - `runBlocking` invocations - Synchronous third-party SDK initializations --- # 7. `runBlocking` Analyze every invocation of `runBlocking` on runtime execution paths. Especially: ```text Main thread ↓ runBlocking ↓ suspend / blocking operation ``` This represents an acute ANR hazard. --- # 8. SYNCHRONOUS DATABASE OPERATIONS Check: - Room queries - Raw SQLite executions - Custom DAO methods - Migration routines that can execute synchronously on the Main thread. If a Room DAO returns a suspend function or a Flow, do not assume a Main-thread blocking issue without evidence. --- # 9. `allowMainThreadQueries` If configured on a Room database builder, determine whether it is confined strictly to test fixtures or leaks into production builds. Inspect production usage thoroughly. --- # 10. FILE I/O Look for: - `readText()` - `writeText()` - `FileInputStream` - `FileOutputStream` - ZIP archiving - Database backups - Import / export file handling executing on the UI thread path. --- # 11. JSON PARSING Large JSON payloads can severely saturate CPU cores. Ask: - how large can the payload realistically be - on which dispatcher is it deserialized - how frequently does deserialization occur --- # 12. SERIALIZATION Serializing massive domain models can be just as expensive as deserialization. Pay specific attention to: - Backup generation - File export routines - Inter-Process Communication (IPC) bundles - Saved instance state serialization --- # 13. CRYPTOGRAPHY Encryption, hashing, signature verification, and key derivation (e.g. PBKDF2, Argon2) are CPU-intensive operations. Verify the target thread and coroutine dispatcher. --- # 14. COMPRESSION ZIP, GZIP, and media compression/transcoding routines must never execute on the Main thread. --- # 15. BITMAP PROCESSING Look for: - Image decoding - Bitmap resizing and scaling - Image rotation - Blur and filter applications - Image encoding to disk executing directly on the UI thread. --- # 16. PDF PROCESSING If the application: - Generates - Renders - Parses PDF documents, verify off-main-thread execution and memory consumption. --- # 17. NETWORK Modern networking libraries execute asynchronously by default, but verify: - Synchronous `.execute()` calls (e.g. in OkHttp / Retrofit) - Custom blocking network wrappers - Suspend functions wrapped in `runBlocking` on the UI thread --- # 18. DNS AND CONNECTION LATENCY High network latency does not constitute an ANR if the Main thread remains unblocked and responsive to input. Do not report slow API responses as ANR defects without a blocking UI thread path. --- # 19. LOCKS Look for: - `synchronized` blocks - `ReentrantLock` - `Mutex` - Semaphores that can block or contend with the Main thread. --- # 20. MAIN THREAD WAITING ON WORKER Scenario: ```text Main thread ↓ wait() / CountDownLatch.await() / Future.get() ↓ Background worker thread ``` This transforms background execution directly into a Main-thread blocking bottleneck. --- # 21. COUNTDOWNLATCH Scrutinize every call to `CountDownLatch.await()` on the Android UI path. --- # 22. FUTURE.GET Inspect all calls to `Future.get()` or `CompletableFuture.join()` on the Main thread. --- # 23. THREAD.JOIN Inspect all calls to `Thread.join()` on the Main thread. --- # 24. BUSY WAIT Look for tight `while` or `for` loops polling for state changes without coroutine suspension or proper wait primitives. --- # 25. SLEEP Invoking `Thread.sleep()` on the Main thread is an immediate, high-confidence performance defect. --- # 26. STARTUP MAP Deconstruct application startup: ```text Process creation ↓ Application.onCreate() ↓ ContentProviders ↓ SDK initializations ↓ Activity.onCreate() ↓ First frame render ↓ Fully usable screen ``` --- # 27. APPLICATION.ONCREATE Map everything initialized during application startup. Classify each task: ```text REQUIRED BEFORE FIRST FRAME CAN BE DEFERRED LAZY NOT VERIFIED ``` --- # 28. CONTENT PROVIDERS Several third-party libraries use automatic ContentProvider initialization to boot before `Application.onCreate()`. Check merged manifests and evaluate migration to the Jetpack App Startup library. --- # 29. THIRD-PARTY SDK STARTUP Inspect initializations for: - Analytics - Ad networks - Crash reporters - Remote configuration - Social auth libraries - Attribution tracking Do not defer SDKs blindly. Determine whether each SDK is strictly necessary before displaying the first frame. --- # 30. DATABASE STARTUP Opening large database files or executing schema migrations during app startup increases cold start latency. --- # 31. DATABASE MIGRATIONS Analyze heavy migration scripts: - Table copying - Large row updates - Index creation If the user must wait during startup, document the blocking impact. --- # 32. SYNCHRONOUS MIGRATIONS Room migrations must complete before opening the database connection. Evaluate actual data volume and estimated duration before setting severity. --- # 33. COLD START If testing tooling permits, measure: - Process start to first frame displayed - Process start to fully usable interactive screen If unmeasured: **COLD START: NOT MEASURED** --- # 34. WARM START Separate warm start metrics from cold start metrics. --- # 35. HOT START Separate hot start metrics from warm and cold start metrics. Do not conflate these three categories. --- # 36. TIME TO FIRST FRAME If profiling tooling is active, measure Time to Initial Display (TTID). Do not fabricate metrics. --- # 37. TIME TO FULLY DRAWN If the app calls `reportFullyDrawn()` when interactive content has completed loading, analyze Time to Full Display (TTFD). --- # 38. SPLASH SCREEN A splash screen does not improve startup performance. It merely masks or visualizes waiting time. Identify the actual underlying work executed behind the splash screen. --- # 39. ARTIFICIAL SPLASH DELAY If artificial time delays (e.g. `delay(2000)`) force users to wait longer than necessary, report as a user experience defect. --- # 40. INITIAL DATA LOAD If the application blocks the initial screen while waiting for: - Remote config - Auth token validation - Database pre-population - Network responses determine what is strictly required to render the initial UI shell. --- # 41. PARALLEL STARTUP Do not parallelize startup tasks blindly. Map exact dependency graphs across initialization components to prevent thread pool starvation. --- # 42. LAZY INITIALIZATION Candidates for lazy initialization include features only accessed on deeper screens. However, lazy initialization can transfer latency from startup to the first feature interaction. Document the architectural tradeoff. --- # 43. FIRST-USE JANK Opening a feature for the first time may initialize a heavy library on-demand. Ask: > Is it better to pay the initialization cost during startup or on first feature interaction? Do not decide without product context. --- # 44. UI JANK Search for frames where the Main thread performs too much computation. Common causes: - Heavy composition passes - Deep layout measurements - Excessive drawing routines - List item view binding - Synchronous image decoding - Inline synchronous business logic --- # 45. FRAME BUDGET Do not apply a single rigid millisecond threshold without understanding device refresh rates (60Hz = 16.6ms, 90Hz = 11.1ms, 120Hz = 8.3ms). The core requirement is that the application consistently completes frame workloads before the display hardware deadline. --- # 46. COMPOSE RECOMPOSITION Do not report recomposition count purely as an isolated metric. Ask: - what is recomposing - how computationally expensive is the composable - how frequently does recomposition fire - does it induce measurable jank --- # 47. EXPENSIVE COMPOSABLE BODY Look for: - Collection sorting - Filtering routines - JSON parsing - Date and number formatting of large collections - New object allocations executed directly within the composable body during composition. --- # 48. STATE BLAST RADIUS Reading frequently changing state high in the Compose hierarchy can invalidate large subtrees unnecessarily. Assess the actual recomposition blast radius. --- # 49. COMPOSITIONLOCAL Frequently changing `CompositionLocal` values near the root of the hierarchy force extensive subtree recompositions. --- # 50. UNSTABLE TYPES Do not report type instability without demonstrating a concrete, measurable recomposition impact. --- # 51. COMPOSE COMPILER METRICS If compiler metrics reports are generated, use them as evidence. Remember: **unstable != automatically slow** --- # 52. LAYOUT MEASUREMENT Deeply nested layouts can be expensive: - Intrinsic measurements - Nested layout weights - Custom layout measurement passes Demand hotspot evidence before filing findings. --- # 53. INTRINSIC MEASUREMENTS If intrinsic measurements are used frequently across large scrolling lists, verify scrolling performance. --- # 54. SUBCOMPOSE Components built on `SubcomposeLayout` introduce additional measurement overhead. Do not report standard Material components merely because they leverage subcomposition internally. --- # 55. LAZY LISTS Inspect: - Stable item keys - contentType configuration - Heavy item hierarchies - Nested scrolling configurations - Image loading inside item composables --- # 56. OVERSIZED ITEM TREE If individual list items contain massive UI hierarchies, scrolling will drop frames. --- # 57. NESTED LAZYCOLUMNS Look for problematic nested scrolling lists in the same orientation direction. --- # 58. COLUMN INSTEAD OF LAZY LIST Rendering hundreds or thousands of elements inside a standard non-lazy `Column` or `Row` is a critical performance hazard. Verify the actual expected list size. --- # 59. XML VIEW HIERARCHY If the app utilizes legacy Views: - Excessive layout hierarchy depth - Nested `layout_weight` usage - Repeated layout inflation passes - Heavy custom `onDraw` implementations --- # 60. RECYCLERVIEW Inspect: - `notifyDataSetChanged()` calls - `DiffUtil` / `ListAdapter` usage - ViewHolder binding workloads - Nested RecyclerView pools - RecycledViewPool sharing Do not report `notifyDataSetChanged()` if the list contains 3 static items with rare updates. --- # 61. BIND WORKLOAD `onBindViewHolder` must never perform: - Text parsing - Database queries - Bitmap transformations - Network requests --- # 62. VIEW INFLATION Massive XML layouts can produce scroll jank during ViewHolder instantiation. --- # 63. TEXT RENDERING Rendering massive text blocks and complex Spans can incur noticeable layout overhead. Focus only on verified hotspots. --- # 64. MARKDOWN AND HTML RENDERING If a list renders formatted Markdown or HTML for every item on-the-fly, verify caching and preprocessing strategies. --- # 65. SYNTAX HIGHLIGHTING In-app code editors and viewers carry significant CPU overhead during lexical analysis and text layout. --- # 66. IMAGE LOADING PIPELINE Map the image pipeline: ```text URL / File source ↓ Disk fetch ↓ Decode ↓ Resize / Downsample ↓ Memory cache ↓ Display ``` --- # 67. ORIGINAL RESOLUTION DECODING If a thumbnail view decodes a full-resolution (e.g. 12MP) camera image, memory and CPU consumption spike dramatically. --- # 68. IMAGE CACHING Verify existing image loader caching configurations before recommending custom caching layers. --- # 69. PLACEHOLDERS Placeholders do not improve technical runtime performance, but they improve perceived performance. Distinguish UX perception from runtime efficiency. --- # 70. LARGE BITMAP MEMORY Calculate estimated decoded memory consumption when dimensions are known (Width x Height x 4 bytes for ARGB_8888). Do not guess. --- # 71. OOM RISK Report OutOfMemoryError (OOM) risks only when backed by a concrete allocation or unbounded growth scenario. --- # 72. MEMORY INVENTORY Map large or long-lived in-memory assets: - Decoded Bitmaps - Large collections - In-memory caches - Byte buffers - Video and audio buffers - WebViews - Media players --- # 73. MEMORY LEAKS A memory leak is a performance issue because it increases Garbage Collection (GC) frequency, introduces jank, and eventually triggers OOM crashes. Do not report references without proving an extended lifetime beyond their scope. --- # 74. ACTIVITY LEAKS Look for Activity Context references retained inside process-lifetime singletons or static variables. --- # 75. FRAGMENT VIEW LEAKS Check for un-cleared ViewBinding or view listener references outliving `onDestroyView()`. --- # 76. WEBVIEW MEMORY WebViews consume substantial native and Java memory. Verify explicit ownership, cleanup, and `destroy()` calls. --- # 77. MEDIA PLAYER LIFECYCLE Media players (e.g. ExoPlayer / Media3) must release codecs and buffers when inactive. --- # 78. UNBOUNDED LIST CACHES In-memory cache collections without size limits or LRU eviction grow indefinitely with user interactions. --- # 79. FLOW CACHING Using `stateIn` or `shareIn` with long-lived scopes retains large emissions in memory. --- # 80. VIEWMODEL MEMORY RETENTION A ViewModel retained too broadly (e.g. Activity scope) keeps large collections alive long after the child screen is closed. --- # 81. GARBAGE COLLECTION PRESSURE Look for high-frequency short-lived object allocations inside: - UI rendering loops - Scroll handlers - Animation frame callbacks - Audio and video processing callbacks --- # 82. OBJECT CREATION IN DRAW PHASES Custom drawing routines that allocate `Paint`, `Path`, or `Bitmap` objects inside `onDraw()` or `Canvas.draw*` represent hot allocation defects. --- # 83. STRING FORMATTING IN HOT LOOPS High-frequency string allocations inside per-frame callbacks create GC churn. Do not micro-optimize routine UI composition text. --- # 84. CPU-INTENSIVE COMPUTATION Look for: - In-memory sorting of massive datasets - Cryptographic calculations - Compression / decompression - Media transcoding - Machine learning inference - Computer vision and image processing --- # 85. DEFAULT DISPATCHER CPU-intensive coroutines should execute on `Dispatchers.Default`, unless the underlying library manages its own dedicated thread pool. --- # 86. OVER-PARALLELIZATION `Dispatchers.Default` operates on a finite thread pool proportional to CPU core count. Spawning excessive CPU-intensive tasks starves the pool and contends with the UI thread for CPU cycles. --- # 87. IO SATURATION Spawning an unbounded number of I/O coroutines can saturate: - Local flash storage - SQLite database locks - Network bandwidth - Thread memory overhead Verify concurrency limits where shared resources have natural throughput constraints. --- # 88. THREAD EXPLOSION Custom executors or thread-per-task patterns can spawn hundreds of OS threads, exhausting system memory. --- # 89. BATTERY VS PERFORMANCE TRADEOFF Faster is not always better. Aggressive polling or continuous background synchronization improves data freshness at the cost of rapid battery depletion. Document the engineering tradeoff. --- # 90. NETWORK CHATTER Count the number of network requests required to render a single screen. Look for: - Duplicate fetches - N+1 query patterns - Per-item network requests - Redundant refreshes --- # 91. DUPLICATE NETWORK FETCHES Recomposition or Activity lifecycle restarts can trigger duplicate network requests. Check repository caching and single-flight request patterns. --- # 92. SEARCH REQUEST CHATTER Search inputs without debounce or query cancellation flood the network with obsolete requests. --- # 93. API PAYLOAD VOLUME Large JSON responses incur: - Download latency - Parsing CPU overhead - Memory allocation churn Analyze whether the client actually consumes only a fraction of the payload. --- # 94. NETWORK COMPRESSION HTTP compression (gzip, brotli) is typically handled at the transport and server level. Do not recommend manual client-side payload compression without a verified need. --- # 95. CONNECTION POOLING OkHttp and Ktor manage connection reuse and HTTP/2 pooling automatically. Do not report missing custom connection pools. --- # 96. DATABASE QUERY PERFORMANCE Identify queries executing on the critical user path. Look for: - Full table scans - Missing database indexes - Expensive multi-table JOINs - N+1 DAO query loops - Unindexed ORDER BY clauses - Querying entire tables when only a count or subset is needed --- # 97. `SELECT *` Using `SELECT *` is not inherently a defect. It becomes a problem if the entity contains large text/blob columns and the UI only displays two fields from thousands of rows. --- # 98. ROOM FLOW INVALIDATION STORMS A single write operation can invalidate an entire table query, causing repeated query execution and UI re-emissions. --- # 99. N+1 IN ROOM Scenario: ```text Load 500 parent rows ↓ Loop through each row to query relations or count ``` Trace the actual DAO execution flow. --- # 100. DATABASE INDEXES Do not recommend adding indexes without evaluating query performance gains against the additional write/insert overhead. --- # 101. MASSIVE TRANSACTIONS A massive database transaction holds exclusive locks on the database file for extended durations, blocking concurrent readers and writers. --- # 102. DATABASE CONTENTION Foreground UI queries and background sync workers executing concurrent heavy writes can contend for SQLite locks. --- # 103. UNPAGINATED DATASETS If the UI displays potentially unbounded collections, verify whether the entire dataset is queried and retained in memory simultaneously. --- # 104. PAGING INTEGRATION Jetpack Paging is appropriate when dataset volume and user experience truly warrant lazy incremental loading. --- # 105. REPEATED DATA TRANSFORMATIONS Look for ViewModels or repositories repeatedly mapping and transforming the same large lists across sequential emissions. --- # 106. UNCACHED SORTING If a large collection is re-sorted on every Flow emission or recomposition pass, calculate the actual CPU cost. --- # 107. REPEATED FILTERING The same rule applies to heavy filtering logic. --- # 108. DISTINCT UNTIL CHANGED If an upstream producer frequently emits unchanged state snapshots, verify whether equality checks prevent redundant downstream processing. --- # 109. FLOW PROCESSING CHAIN Map the data transformation pipeline: ```text Room Flow ↓ map ↓ combine ↓ sort ↓ filter ↓ stateIn ↓ UI rendering ``` Locate where the heaviest computational burden resides. --- # 110. `combine` OPERATOR CHURN A frequent update to a small, fast-changing source stream can repeatedly trigger expensive transformations across an adjacent large dataset. --- # 111. MAIN THREAD FLOW OPERATORS Flow operators execute in the context of their upstream emitter. Ensure expensive mapping operations are not inadvertently running on the Main thread. --- # 112. `flowOn` Do not misapply `flowOn`; it changes the upstream context, not the downstream collector context. --- # 113. STATEFLOW CONFLATION `StateFlow` drops intermediate values that evaluate as structurally equal (`equals`). This is a correctness topic unless it introduces unexpected UI performance artifacts. --- # 114. UI STATE GRANULARITY A massive composite `UiState` object updated due to a single minor property change can invalidate broader UI subtrees. Verify actual rendering overhead before filing an issue. --- # 115. ANIMATIONS Look for: - Continuously running infinite animations on hidden UI - Large dynamic blur effects - Heavy custom drawing in animation ticks - Multiple overlapping alpha layers forcing GPU offscreen rendering --- # 116. HARDWARE LAYERING Do not mandate `Modifier.graphicsLayer` or View hardware layers without profiling. Hardware layers allocate offscreen textures and consume GPU memory. --- # 117. SHADOWS AND BLURS Complex dynamic shadows and blurs can degrade frame rates on low-end GPUs. Provide runtime evidence or clear hotspot traces. --- # 118. OVERDRAW If the UI draws multiple stacked opaque backgrounds across the entire screen, evaluate GPU overdraw where relevant. --- # 119. CLIPPING PATHS Complex clipping paths increase GPU vertex processing and rasterization overhead. Do not micro-optimize standard rounded corner rectangles. --- # 120. VIDEO PLAYBACK If video playback is present, examine: - Codec hardware decoding support - Display resolution vs viewport size - SurfaceView vs TextureView selection - Buffering configurations - Player lifecycle management Do not speculate on codec performance without device trace data. --- # 121. AUDIO PROCESSING Real-time audio callbacks must execute with zero blocking allocations to avoid buffer underruns. --- # 122. MEDIA BUFFERING Network streaming buffering is distinct from UI rendering performance, though users perceive both as latency. --- # 123. WEBVIEW PERFORMANCE If WebViews render complex external content, analyze: - Instantiation overhead - View reuse strategies - JavaScript execution bridging - Resource caching policies --- # 124. MAP RENDERING Map SDKs consume significant GPU and memory resources. Ensure map instances are not repeatedly destroyed and recreated needlessly. --- # 125. CAMERA PREVIEWS Camera preview and image analysis pipelines can suffer from: - Frame processing backlogs - CPU core saturation - ImageProxy buffer leaks preventing frame delivery --- # 126. IMAGE ANALYSIS BACKPRESSURE If image analysis algorithms cannot process every camera frame in real-time, verify frame dropping and backpressure handling. --- # 127. ON-DEVICE MACHINE LEARNING For on-device ML inference, evaluate: - Model load time - Neural network acceleration (NNAPI / GPU delegates) - Execution thread confinement - Batching vs single-inference overhead - Model memory footprint --- # 128. SENSOR SAMPLING FREQUENCY High-frequency hardware sensor sampling must be throttled to the minimum rate demanded by the use case. --- # 129. LOCATION UPDATES Excessive location update intervals rapidly drain device battery and wake the CPU. --- # 130. BLUETOOTH EVENT PROCESSING High-frequency Bluetooth GATT notifications can congest the Main thread if callbacks process data directly on the UI dispatcher. --- # 131. LOGGING IN HOT PATHS Extensive logging inside high-frequency loops (scrolling, drawing, audio) degrades performance, especially in debug builds. Verify that debug logging is stripped in release builds. --- # 132. STRING INTERPOLATION IN LOGS Constructing complex log strings when the log level is disabled incurs wasted memory allocation and CPU cycles in hot paths. --- # 133. SYNCHRONOUS ANALYTICS Never dispatch synchronous analytics tracking calls on user interaction paths. --- # 134. CRASH REPORTING BREADCRUMBS Serializing large complex objects into crash breadcrumbs adds runtime latency and raises privacy concerns. --- # 135. DISK LOGGING If the application writes verbose diagnostic logs to disk in high-frequency flows, verify I/O impact. --- # 136. STRICTMODE Utilize StrictMode in development/testing to detect disk and network violations on the Main thread. However, the absence of StrictMode is an architectural improvement, not an acute performance bug. --- # 137. PROFILING TOOLS Where runtime profiling is available, leverage: - Android Studio CPU Profiler - Memory Profiler - System Tracing (Perfetto) - Frame Rendering timeline Do not fabricate profiling metrics. --- # 138. PERFETTO SYSTEM TRACE For severe jank or ANR investigations, system traces provide definitive evidence of: - Blocked Main thread - Binder IPC delays - Thread scheduling latency - Garbage collector pauses - Disk I/O stalls --- # 139. ANR TRACES If ANR traces (`traces.txt`) are available, they hold primary priority. Analyze the main thread stack trace first. --- # 140. PLAY CONSOLE ANR TELEMETRY If production telemetry data is unavailable: **PRODUCTION ANR RATE: NOT AVAILABLE** Do not speculate. --- # 141. FIREBASE PERFORMANCE MONITORING Use real telemetry traces if configured; do not guess metric distributions. --- # 142. MACROBENCHMARK If benchmark tests exist, review metrics for: - Cold / warm startup - Scroll jank (FrameTimingMetric) - Navigation transitions --- # 143. MICROBENCHMARK Use Microbenchmarks strictly for isolated algorithms or hot code paths. Do not cite microbenchmarks as proof of overall user experience performance. --- # 144. BASELINE PROFILES If Baseline Profiles exist, verify that they exercise critical user journeys. Do not assert specific percentage improvements without comparative benchmark measurements. --- # 145. PROFILEINSTALLER Verify whether `androidx.profileinstaller` is configured in production builds. --- # 146. CLOUD PROFILES Do not make unverified assumptions regarding Google Play Cloud Profile generation for the specific app. --- # 147. R8 OPTIMIZATIONS R8 minification, inlining, and dead code elimination reduce APK size and improve startup times, but verify actual configuration. --- # 148. RESOURCE SHRINKING Check whether unused assets and resources are stripped during release compilation. --- # 149. APK / AAB DOWNLOAD SIZE Binary size impacts: - Download duration - Installation time - Storage footprint However, binary size is distinct from runtime execution performance. Keep categories separated. --- # 150. LARGE DEPENDENCIES Flag heavy libraries only when: - They substantially increase startup latency, binary size, or memory usage - The application uses only a tiny fraction of their functionality - Concrete evidence is demonstrated --- # 151. MULTIDEX MultiDex is handled natively on modern Android versions (API 21+) and does not inherently degrade runtime performance. --- # 152. CLASS LOADING Extensive reflection or massive initial class loading graphs can impact cold startup latency. --- # 153. REFLECTION Do not report reflection merely because it is used. It is relevant only if executed heavily on hot performance paths. --- # 154. DEPENDENCY INJECTION PERFORMANCE Analyze DI framework startup overhead only by tracing the actual initialization path. --- # 155. LAZY INJECTION Injecting dependencies lazily helps for expensive objects that are rarely used. However, it may simply shift the latency spike to the first point of use. --- # 156. PRELOADING STRATEGIES Preloading resources can accelerate subsequent screens at the cost of higher startup latency and memory consumption. Evaluate the tradeoff. --- # 157. CACHE EVALUATION For every cache, ask: - what does it accelerate - how much memory does it consume - what is the invalidation strategy - what is the hit rate, if known If hit rates are unknown: **CACHE EFFECTIVENESS: NOT MEASURED** --- # 158. PREMATURE CACHING Do not add caching layers simply because a function appears expensive. First verify that the operation is repeatedly called with identical inputs. --- # 159. UNBOUNDED CACHES Using maps without size limits or eviction policies is a confirmed memory leak hazard. --- # 160. DISK CACHE MANAGEMENT An oversized disk cache can consume device storage and slow down disk cleanup operations. --- # 161. LOW-END DEVICE PASS Dedicate a specific evaluation pass to low-end devices: Assume: - Slower CPU cores - Weak GPU hardware - Limited RAM (2GB - 3GB) - Slow flash storage read/write speeds Ask: > Which bottleneck impacts the user first on constrained hardware? --- # 162. THERMAL THROTTLING Prolonged CPU-intensive operations can trigger device thermal throttling, halving processor clock speeds. Report only for sustained heavy workloads. --- # 163. BATTERY SAVER MODE Battery saver constraints restrict background work and throttle CPU frequencies. --- # 164. LARGE DATASET PASS Mentally scale data volume: ```text 100 records -> 10,000 records ``` Inspect: - Database query times - Memory allocation spikes - List rendering performance - Transformation throughput Do not claim failure without a reasonable growth expectation. --- # 165. LONG SESSION EVALUATION The application may run continuously for hours. Look for: - Gradual memory growth - Accumulated un-cleared listeners - Retained navigation screens - Unbounded in-memory logs --- # 166. NAVIGATION MEMORY FOOTPRINT A deep navigation back stack retains multiple ViewModels and their state collections in memory. --- # 167. MULTI-TAB BOTTOM NAVIGATION Retaining independent back stacks across multiple bottom navigation tabs increases memory footprint significantly. --- # 168. BACKGROUND / FOREGROUND TRANSITIONS Returning from the background can trigger concurrent tasks: - Token refreshes - Socket reconnections - Analytics dispatches - Data observers If all execute simultaneously, a noticeable UI latency spike occurs. --- # 169. THUNDERING HERD ON RESUME Scenario: ```text onResume ↓ 5 ViewModels trigger refresh ↓ Multiple simultaneous database queries ↓ Multiple simultaneous API calls ``` Verify request coordination and deduplication. --- # 170. CONNECTIVITY RESTORATION Regaining internet connectivity can trigger: - WorkManager tasks - Failed request retries - Foreground UI refreshes simultaneously. --- # 171. RETRY STORMS Multiple failed requests retrying at once upon network restoration saturate connection pools. --- # 172. EXPONENTIAL BACKOFF Ensure retry loops implement exponential backoff and jitter to protect device resources and backend health. --- # 173. WORKMANAGER CONCURRENCY Map all background WorkRequests that can execute concurrently. --- # 174. WORKER CONSTRAINTS Overly aggressive worker scheduling consumes device battery unnecessarily. --- # 175. PERIODIC WORK FREQUENCY Do not schedule periodic work with high frequency unless strictly required by the product. --- # 176. WORKER CPU UTILIZATION WorkManager is not a license to execute unrestricted CPU-intensive tasks without consideration for battery drain. --- # 177. FOREGROUND SERVICE PERFORMANCE If a Foreground Service runs for extended durations, evaluate: - Network polling frequency - Continuous disk logging - Sensor sampling rates - CPU thread saturation --- # 178. BINDER IPC PAYLOAD LIMITS Large IPC payloads across Binder transactions can trigger `TransactionTooLargeException` or introduce noticeable serialization latency. Check: - Intent extras - Bundles - Messenger / AIDL payloads --- # 179. TRANSACTION TOO LARGE Excessive saved instance state bundles cause app crashes upon process backgrounding. --- # 180. SYSTEM NOTIFICATIONS Generating excessive notifications or large bitmap notification assets incurs memory and CPU overhead. --- # 181. APP WIDGETS High widget update frequencies and repeated RemoteViews bitmap generation impact performance. --- # 182. BACKUP OPERATIONS Generating large backups on the UI thread causes severe jank and ANR risks. --- # 183. FILE IMPORT Parsers that load entire large files into memory at once rather than streaming content risk OOM crashes. --- # 184. STREAMING DATA Streaming is mandatory for large file parsing, but do not add streaming complexity for small, fixed-size payloads. --- # 185. BATCH DATABASE WRITES Executing thousands of individual SQLite inserts without wrapping them in a batch transaction is orders of magnitude slower. --- # 186. BATCH NETWORK REQUESTS Dispatching dozens of individual network calls instead of a single batched endpoint degrades network efficiency. --- # 187. PROGRESS UI CHURN Dispatching UI progress updates for every single processed record causes excessive recomposition and UI invalidation. --- # 188. THROTTLED PROGRESS UPDATES Throttle progress emissions (e.g. at 50ms or 100ms intervals) if high-frequency updates cause jank. --- # 189. INPUT LATENCY Touch handlers must return control to the UI thread immediately. Never dispatch heavy computational work directly inside click callbacks. --- # 190. IMMEDIATE FEEDBACK Perceived performance can be improved via: - Immediate visual state changes - Progress bars - Skeleton loaders However, perceived performance improvements do not replace actual runtime optimization. --- # 191. FALSE LOADING STATES Do not display a loading spinner for operations that complete almost instantaneously (< 50ms) to avoid visual flicker. --- # 192. SKELETON LOADERS A skeleton screen that is computationally heavier than the real content is counterproductive. --- # 193. SHIMMER EFFECTS Infinite shimmer animations across dozens of items can saturate low-end GPUs. --- # 194. ANIMATION WHILE LOADING Combining network latency with heavy CPU-driven animations degrades performance on low-end hardware. --- # 195. PERFORMANCE TEST MATRIX For critical user flows, document: | Flow | Cold Start | Warm Start | Low-End Device | Large Dataset | Measured Status | |---|---|---|---|---|---| --- # 196. HOTSPOT FORMAT For every serious performance finding: ```text ID: Severity: Category: Confidence: Status: Feature: Screen: Module: File: Function/Class: Execution thread: Frequency: Dataset size: Build type: Problem: Evidence: Critical Path: Why expensive: Measured or inferred: Metric: Current value: NOT MEASURED if unavailable User impact: ANR/Jank/Memory risk: Root cause: Recommended remediation: How to measure after fix: Regression benchmark/test: Complexity: XS / S / M / L / XL ``` --- # 197. PERFORMANCE STATUS Use: ```text MEASURED CODE-LEVEL RISK NOT MEASURED ``` Mandatory for every performance finding. --- # 198. SEVERITY Use: ## P1 - HIGH - Concrete ANR hazard on a primary user flow - Main thread blocked by large or poorly scaling operations - Reproducible severe UI jank - Critical memory growth or OOM crash - Critical user journey rendered practically unusable ## P2 - MEDIUM - Substantial latency, jank, or memory overhead - Primary user flow exhibits a demonstrable bottleneck ## P3 - LOW - Contained inefficiency with minor user impact ## P4 - IMPROVEMENT - Performance enhancement without an active user-visible defect Reserve P0 strictly for critical performance flaws causing irreversible data loss or severe security violations. --- # 199. CONFIDENCE Use: ```text HIGH MEDIUM LOW ``` HIGH: Empirical runtime measurement / trace or directly demonstrable blocking execution path. MEDIUM: Strong code-level evidence without benchmark confirmation. LOW: Hypothesis dependent on specific real-world dataset sizes or device hardware. --- # 200. DO NOT FABRICATE METRICS Never state: ```text LCP is 2.1 s Startup is 900 ms 60 FPS Memory is 250 MB ``` unless verified by actual profiling tools. Instead, state: ```text Startup: NOT MEASURED ``` --- # 201. PERFORMANCE BUDGETS If the project defines baseline performance budgets, measure against them. If no baselines exist, propose establishing an empirical baseline first. Do not invent arbitrary universal thresholds. --- # 202. REGRESSION BENCHMARKING For serious findings, define the benchmark scenario that must be executed before and after applying the fix. --- # 203. TRACE BEFORE OPTIMIZATION When running the application is possible: Prefer: ```text Reproduce ↓ Trace / Profile ↓ Identify hotspot ↓ Optimize ↓ Measure again ``` over guessing from source code alone. --- # 204. FALSE POSITIVE PREVENTION Before confirming a P1 or P2 finding, verify: 1. Target execution thread 2. Execution frequency 3. Real dataset size 4. Library internal thread dispatching 5. Active caching layers 6. Lifecycle boundaries 7. Debug vs release build behavior 8. Low-end device relevance 9. Existing profiler evidence 10. Automated tests and benchmarks Never report an issue based solely on function naming. --- # 205. DO NOT MICRO-OPTIMIZE CODE Do not report as an acute problem: - A single small heap allocation - A single `map` invocation - A small list transformation - A single lambda instantiation - Routine string formatting unless it resides within an extremely hot per-frame loop and is demonstrably impactful. --- # 206. DO NOT MEMOIZE EVERYTHING Caching and memoization: - Consume memory - Complicate state invalidation - Can serve stale data Recommend caching only when there is a clear, measurable benefit. --- # 207. DO NOT MOVE EVERYTHING TO DISPATCHERS.IO CPU-intensive tasks do not belong on `Dispatchers.IO`. UI-safety does not mean blindly routing everything through `Dispatchers.IO`. --- # 208. AVOID UNNECESSARY THREAD CONTEXT SWITCHES Offloading trivial operations to another dispatcher introduces thread scheduling overhead and complexity. --- # 209. DO NOT MODIFY CODE During the audit: - Do not refactor code - Do not insert caches - Do not swap dispatchers - Do not implement pagination - Do not add Baseline Profiles - Do not rewrite list implementations - Do not alter database schemas Complete the audit first. --- # 210. OUTPUT - ANDROID_PERFORMANCE_ANR_AUDIT.md Structure the final audit report: ## 1. Executive Summary - Performance architecture overview - Top ANR risks - Startup latency status - UI jank and rendering health - Memory consumption status - Profiling measurement coverage ## 2. Performance Measurement Coverage Specify what is: ```text MEASURED INFERRED NOT MEASURED ``` ## 3. Critical User Flow Performance Map ## 4. Main Thread Audit ## 5. ANR Risk Audit ## 6. Startup Audit ## 7. Compose Performance Audit or Views audit where applicable. ## 8. Lists / Scrolling Audit ## 9. Network Performance ## 10. Database Performance ## 11. Flow / Data Transformation Performance ## 12. Image / Media Performance ## 13. Memory Audit ## 14. Memory Leak Performance Impact ## 15. CPU Audit ## 16. Background Work / Battery Audit ## 17. Low-End Device Risk ## 18. Large Dataset Risk ## 19. Release Build Performance ## 20. Benchmark / Profiling Coverage ## 21. Findings Summary | ID | Severity | Category | Critical path | Measured/Inferred | Confidence | Status | |---|---|---|---|---|---|---| ## 22. P1 Findings ## 23. P2 Findings ## 24. P3 Findings ## 25. P4 Improvements ## 26. Things Done Well ## 27. Unknown / Not Measured ## 28. Performance Measurement Plan ## 29. Remediation Roadmap --- # 211. MAIN THREAD MATRIX For critical operations: | Operation | Thread | Blocking | Frequency | Size | Risk | |---|---|---|---|---|---| --- # 212. STARTUP MATRIX | Initialization | Required before UI | Thread | Cost measured | Deferrable | |---|---|---|---|---| --- # 213. MEMORY MATRIX | Resource | Owner | Size/Growth | Lifetime | Cleanup | Risk | |---|---|---|---|---|---| --- # 214. DATABASE MATRIX | Query | Critical path | Dataset | Index | Frequency | Risk | |---|---|---|---|---|---| --- # 215. SECOND PASS - MAIN THREAD ATTACK After completing the initial review, re-evaluate with a single question: > What operations can end up executing on the Main thread? Trace transitive call chains rather than only direct method calls. Example: ```text Button click ↓ ViewModel method ↓ Repository method ↓ Helper function ↓ File.readBytes() ``` --- # 216. SECOND PASS - STARTUP ATTACK Ask: > What computational cost does the user pay before observing the first useful screen? Classify each startup task: ```text MUST BLOCK CAN DEFER CAN LAZY LOAD NOT VERIFIED ``` --- # 217. SECOND PASS - 10X DATA SCALE For every list, query, and data transformation, mentally scale data volume by 10x. Evaluate: - Memory consumption - CPU utilization - Render latency - Database query duration Severity must correspond to realistically expected growth. --- # 218. SECOND PASS - LOW-END HARDWARE Assume significantly slower CPU cores and flash storage. Ask: > Which operation currently appears fast only because it was tested on high-end hardware? --- # 219. SECOND PASS - EXTENDED SESSION Simulate several hours of active usage. Evaluate: - Heap memory growth - In-memory cache accumulation - Retained listeners - Back stack memory retention --- # 220. SECOND PASS - BACKGROUND/FOREGROUND CYCLING Simulate cycling between foreground and background 20 times: ```text Foreground ↓ Background ↓ Foreground ``` Look for: - Duplicate observers - Redundant data refreshes - Memory leaks - Repeated SDK initializations --- # 221. SECOND PASS - NETWORK RESTORATION Simulate: ```text Offline state ↓ Multiple operations fail ↓ Network connection restored ``` Evaluate how many requests and background tasks initiate simultaneously. --- # 222. SECOND PASS - FILE IMPORT/EXPORT If file import or export features exist: Test with realistic maximum-sized files. Look for: - Entire-file memory buffering - Main-thread blocking - Prolonged database transaction locks - UI progress update churn --- # 223. SECOND PASS - SCROLL PERFORMANCE For the heaviest list in the app: - High item count - Media thumbnails - Rapid fling gestures - Active filter/search queries Evaluate what executes for every new item and frame. --- # 224. SECOND PASS - ANR VALIDATION For every ANR candidate, construct a precise execution flow: ```text Main thread ↓ Blocking operation ↓ Why input / lifecycle cannot be processed ↓ Trigger event ↓ Worst-case workload duration ``` If you cannot demonstrate this flow: Do not classify the issue as an ANR finding. --- # 225. SECOND PASS - MEMORY PRESSURE Simulate: - Multiple open screens - High-resolution images - Repeated background/foreground transitions - Large local datasets Ask: > Which allocated resource is not released when no longer needed? --- # 226. RETURN ON INVESTMENT (ROI) TABLE For genuine performance optimizations: | Finding | User Impact | Expected Benefit | Engineering Effort | Measurement Method | |---|---|---|---|---| For expected benefit: ```text HIGH MEDIUM LOW ``` Do not invent arbitrary percentage gains without benchmarks. --- # 227. FINAL QUALITY GATE Before submitting the final report, verify: - Performance was not evaluated exclusively on debug builds - Every ANR finding demonstrates a concrete Main-thread blocking scenario - Every jank finding pinpoints a specific hot path - Not every recomposition is labeled as an acute defect - Not every database query is labeled as slow - `SELECT *` is not reported as a bug without payload justification - Memory findings demonstrate growth or retained lifetime issues - OOM crashes are not hypothesized without an allocation path - Startup findings differentiate required from deferrable tasks - Network latency is not misclassified as an ANR - Cache recommendations include invalidation and memory footprint analysis - Database index recommendations reference specific slow queries - Constrained low-end device scenarios were analyzed - Large dataset scaling was evaluated - Empirical measurements and theoretical inference are strictly separated - Every P1 and P2 finding defines a post-fix measurement method - Genuine bottlenecks are strictly separated from P4 improvements --- # FINAL RULE I do not want a generic report stating: > Use background threads, caching, pagination, and Baseline Profiles. That is not an Android performance audit. I am looking for concrete issues such as: ```text User taps Import ↓ Click handler executes ↓ File.readBytes() runs on Main thread ↓ 150 MB file read into memory ↓ Main thread cannot process input events ↓ Screen freezes ↓ ANR occurs ``` or: ```text Room Flow emits 5,000 records ↓ ViewModel maps the entire list ↓ Sorts the entire list ↓ Filters the entire list ↓ All computation executes on Main thread ↓ User changes filter toggle ↓ Whole transformation re-runs on Main ↓ Visible dropped frames and scroll jank ``` or: ```text Application.onCreate() ↓ Analytics SDK initialization ↓ Remote config fetch ↓ Database connection open ↓ Large JSON file parsed ↓ All operations executed synchronously ↓ First Activity display blocked ``` or: ```text Thumbnail grid screen ↓ 100 items loaded ↓ Each image decoded at original 4000x3000 resolution ↓ Massive bitmap allocations on the heap ↓ Garbage Collection churn ↓ Severe scroll jank and memory pressure ``` or: ```text onResume() ↓ Five ViewModels independently refresh ↓ Five database queries dispatched ↓ Five network requests dispatched ↓ Network reconnection triggers WorkManager sync ↓ Foreground transition creates massive burst load ``` or: ```text Navigation creates a new WebView ↓ Old WebView retained by an un-cleared listener ↓ User navigates back and forth repeatedly ↓ Memory usage steadily accumulates ↓ Long session results in severe GC pressure or OOM crash ``` These are the concrete performance bottlenecks you must uncover. Think through: - Main thread confinement - Critical path latency - Operation frequency - Dataset size - Allocation lifetimes - Release build execution - Low-end hardware constraints - Long session stability - Background / foreground transitions - Measurable user wait time For every serious finding, answer: > What specific work is computationally expensive? > On what thread does it execute? > How frequently does it run? > How much data does it process? > Why does the user have to wait? > How can we measure it before and after the fix? If the answer is unknown: **NOT MEASURED.** If there is only suspicion based on source code: **CODE-LEVEL PERFORMANCE RISK.** If the issue is merely a potential optimization without visible user impact: **P4 - IMPROVEMENT.** It is far better to find 5 genuine ANR and jank bottlenecks than to produce 100 micro-optimizations that no user will ever notice. The objective is a forensically sound Android performance audit that directly translates into: - Perfetto traces - Benchmark tests - Reproduction steps - Targeted fixes - Regression benchmarks - Pre- and post-fix measurements - Production performance validation
分类与标签
编程开发、Android性能分析、ANR排查、界面卡顿