Sobes.tech
Middle+

Where would you look for reasons for slow cold start of an application?

sobes.tech AI

Answer from AI

  1. Object initialization: Expensive operations in Application.onCreate() or the first Activity, such as data loading and library initialization.
  2. File reading: Reading large amounts of data from disk files at startup.
  3. Database operations: Slow queries or a large number of database operations during startup.
  4. Resource loading: Excessive or unoptimized loading of resources (images, XML layouts).
  5. Network: Synchronous network requests on the main thread during startup.
  6. JIT compilation: Time spent by ART on JIT compiling code on the first run after installation/update.
  7. Layout Inflation: Complex or deep View hierarchy in the first Activity, requiring a long time to inflate.
  8. 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).