Android Development Android Performance Optimization 1 — Questions and Answers
Question 1: Which tool in Android Studio provides a real-time graph of CPU, memory, network, and battery usage for a running app?
- Android Profiler (Correct answer)
- Logcat
- Layout Inspector
- Lint
Correct answer: Android Profiler
Android Profiler provides real-time monitoring of CPU, memory, network, and energy usage directly within Android Studio.
Question 2: What is the primary cause of 'jank' (dropped frames) in Android applications?
- Excessive network calls
- UI thread work taking longer than 16ms per frame (Correct answer)
- Too many background threads
- Large APK size
Correct answer: UI thread work taking longer than 16ms per frame
Android targets 60fps, giving each frame 16ms; if the UI thread takes longer than that for a frame, the frame is dropped, causing jank.
Question 3: Which class should you use to run CPU-intensive work off the main thread while easily posting results back to the UI thread?
- Thread
- Service
- AsyncTask (deprecated)
- Coroutines with Dispatchers.IO (Correct answer)
Correct answer: Coroutines with Dispatchers.IO
Kotlin Coroutines with Dispatchers.IO (for IO/CPU work) and Dispatchers.Main (for UI updates) is the modern, recommended approach for off-main-thread work.
Question 4: What does the term 'memory leak' mean in Android development?
- The app uses too much network bandwidth
- An object is kept in memory longer than its intended lifecycle (Correct answer)
- The app writes too much data to disk
- The GPU runs out of texture memory
Correct answer: An object is kept in memory longer than its intended lifecycle
A memory leak occurs when references prevent the garbage collector from reclaiming objects whose lifecycle has ended, such as an Activity referenced by a static field.
Question 5: Which Android tool helps detect memory leaks by watching for destroyed objects that are still referenced?
- StrictMode
- LeakCanary (Correct answer)
- Systrace
- Espresso
Correct answer: LeakCanary
LeakCanary is a popular open-source library that automatically detects memory leaks in Android apps by using weak references and heap dumps.
Question 6: What is the recommended way to load large bitmaps efficiently to avoid OutOfMemoryError in Android?
- Load the full bitmap and then scale it in an ImageView
- Use BitmapFactory.Options to subsample the bitmap before decoding (Correct answer)
- Convert all images to PNG format
- Increase the app's heap size in the manifest
Correct answer: Use BitmapFactory.Options to subsample the bitmap before decoding
Setting BitmapFactory.Options.inSampleSize decodes a subsampled version of the bitmap, reducing memory usage significantly before the bitmap is loaded into memory.
Question 7: Which mode in Android can developers enable to detect accidental disk or network access on the main thread?
- Debug mode
- StrictMode (Correct answer)
- ProGuard mode
- ART mode
Correct answer: StrictMode
StrictMode detects and reports violations such as disk reads/writes and network calls on the main thread, helping developers catch performance mistakes early.
Which tool in Android Studio provides a real-time graph of CPU, memory, network, and battery usage for a running app?