Interview QuestionPractical QuestionFollow-up Questions

ActivityManager and System Service Interaction

skydovesJaewoong Eum (skydoves)||8 min read

ActivityManager and System Service Interaction

ActivityManager is a system service in Android that provides access to information about running activities, tasks, processes, and memory state on the device. It serves as the primary API for querying system level resource usage and managing application processes programmatically. Understanding how ActivityManager works reveals how Android manages resources across applications and how tools like LeakCanary leverage system services for memory diagnostics. By the end of this lesson, you will be able to:

  • Explain how to obtain and use ActivityManager through the system service API.
  • Describe the memory information available through MemoryInfo and its practical uses.
  • Identify which ActivityManager methods are restricted or deprecated in modern Android.
  • Explain how ActivityManager is used in memory leak detection tools.
  • Describe the relationship between ActivityManager, process management, and low memory conditions.

Obtaining ActivityManager and Querying Memory

ActivityManager is obtained through Context.getSystemService(), which returns a reference to the system level service managed by the Android framework. The most commonly used capability is querying device memory state through the MemoryInfo structure:

val activityManager = context.getSystemService(
    Context.ACTIVITY_SERVICE
) as ActivityManager

val memoryInfo = ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInfo)

val availableMB = memoryInfo.availMem / (1024 * 1024)
val totalMB = memoryInfo.totalMem / (1024 * 1024)
val isLow = memoryInfo.lowMemory
val thresholdMB = memoryInfo.threshold / (1024 * 1024)

The MemoryInfo object contains four key fields. availMem reports the available system memory in bytes. totalMem reports the total physical memory. lowMemory is a boolean flag that becomes true when available memory drops below the threshold value, which represents the point at which the system starts aggressively killing background processes. Applications can use this information to adjust their caching behavior or release non-critical resources when the device is under memory pressure.

Process Information and Low RAM Detection

ActivityManager provides methods to query running processes and determine device memory characteristics. The getRunningAppProcesses() method returns a list of RunningAppProcessInfo objects describing all processes visible to the caller:

val processes = activityManager.runningAppProcesses
processes?.forEach { process ->
    Log.d("Process", "${process.processName}: " +
        "importance=${process.importance}")
}

val isLowRam = activityManager.isLowRamDevice
val memoryClass = activityManager.memoryClass
val largeMemoryClass = activityManager.largeMemoryClass

The importance field indicates how the system prioritizes the process. IMPORTANCE_FOREGROUND means the process is actively visible to the user. Higher numeric values indicate lower priority, with IMPORTANCE_CACHED representing processes that may be killed at any time. The isLowRamDevice() method returns true for devices with constrained memory (typically 1GB or less), which applications can use to disable memory intensive features. The memoryClass property reports the per-application heap limit in megabytes, while largeMemoryClass reports the limit when android:largeHeap="true" is set in the manifest.

This interview continues for subscribers

Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.

Become a Sponsor