Middle+
Where would you look for reasons for slow cold start of an application?
sobes.tech AI
Answer from AI
- Object initialization: Expensive operations in
Application.onCreate()or the first Activity, such as data loading and library initialization. - File reading: Reading large amounts of data from disk files at startup.
- Database operations: Slow queries or a large number of database operations during startup.
- Resource loading: Excessive or unoptimized loading of resources (images, XML layouts).
- Network: Synchronous network requests on the main thread during startup.
- JIT compilation: Time spent by ART on JIT compiling code on the first run after installation/update.
- Layout Inflation: Complex or deep View hierarchy in the first Activity, requiring a long time to inflate.
- Main thread blocking: Any long operations running on the main thread during startup.
Tools:
- Android Profiler (CPU Profiler, System Trace).
- Startup Profiler (special tool for measuring class initialization time).
- Traceview/Systrace (for detailed call analysis).
- logcat (for detecting suspicious logs or errors during startup).
adb shell am start -W <full_package_name>/<full_activity_name>(to measure total startup time).
// Example of measuring time in code
class MyApplication : Application() {
override fun onCreate() {
val startTime = System.currentTimeMillis()
super.onCreate()
// Initialization that might be long:
// MyDatabase.initialize(this)
// SomeLibrary.setup()
// LoadInitialData.execute()
val endTime = System.currentTimeMillis()
Log.d("StartupTime", "Application onCreate took ${endTime - startTime} ms")
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val startTime = System.currentTimeMillis()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Expensive operations related to UI or data
// setupRecyclerView()
// loadContentFromNetwork()
val endTime = System.currentTimeMillis()
Log.d("StartupTime", "MainActivity onCreate took ${endTime - startTime} ms")
}
}
Focus on Application.onCreate() and the first lifecycle methods of the first Activity (onCreate, onStart, onResume).