Android App Memory Management: Understanding Dalvik VM Parameters

In Android app development, memory management is a critical part of application quality. Reasonable memory management improves performance and helps prevent out-of-memory, or OOM, issues. This article looks at the maximum memory available to an Android app and explains how Dalvik virtual machine parameters can be adjusted to optimize memory usage.

Device-level memory overview

You can inspect the maximum available memory for an Android app through ADB, the Android Debug Bridge. The adb shell command lets you run system-level operations. Here are several common commands.

  1. View total system memory:
adb shell cat /proc/meminfo

This command returns the total memory size of the system.

  1. View an app’s memory usage:
adb shell dumpsys meminfo <package_name>

Replace <package_name> with the target app’s package name to get detailed memory usage for that app.

  1. View the maximum memory used by an app:
adb shell cat /proc/<pid>/status | grep VmHWM

Replace <pid> with the app process ID. VmHWM represents the process’s peak resident set size, meaning the largest amount of physical memory the process has used.

  1. View memory usage for all processes:
adb shell top

This command displays memory usage for all processes, including each process’s PID, user ID, thread count, memory usage, and related information.

Maximum available memory for an app

Android limits the maximum memory available to a single process through its Dalvik implementation of the Java Virtual Machine, or JVM. The limit is defined in /system/build.prop. Values differ by device, and vendor ROMs often adjust them based on hardware configuration. For example, the first Android phone, the G1, had a 16 MB memory limit.

Maximum app memory value

By default, an app’s maximum memory value is controlled by dalvik.vm.heapgrowthlimit. This parameter applies only to the Dalvik heap and does not include the native heap. If an app needs more memory, set android:largeHeap="true" in AndroidManifest.xml. After largeHeap is enabled, the limit changes to dalvik.vm.heapsize.

You can inspect an app’s maximum memory with the following code:

ActivityManager am = (ActivityManager) getApplication().getSystemService(Context.ACTIVITY_SERVICE);
int limitMemorySize = am.getMemoryClass();
int largeMemorySize = am.getLargeMemoryClass();

long maxMemory = Runtime.getRuntime().maxMemory(); // Depending on largeHeap, this equals limitMemory or largeMemory

Android virtual machine parameters

Run adb shell getprop to retrieve memory-related device parameters. Understanding these parameters is important for app optimization. Common important parameters include:

  • dalvik.vm.heapgrowthlimit: maximum growth limit of the Dalvik heap
  • dalvik.vm.heapmaxfree: maximum amount of free memory in the Dalvik heap
  • dalvik.vm.heapminfree: minimum amount of free memory in the Dalvik heap
  • dalvik.vm.heapsize: maximum size of the Dalvik heap
  • dalvik.vm.heapstartsize: initial Dalvik heap size when the app starts
  • dalvik.vm.heaptargetutilization: target utilization ratio of the Dalvik heap

Assume an app needs to process a large amount of image data. You can configure it as follows:

<application
    android:largeHeap="true">
</application>

Then use adb shell getprop to check the current memory configuration:

adb shell getprop dalvik.vm.heapsize

64-bit apps and memory limits

Although a 64-bit address space is much larger than a 32-bit address space, moving an Android app from 32-bit to 64-bit does not automatically increase its memory threshold. The limit is still determined by heapgrowthlimit and heapsize.

Summary

Android app memory management is complex but essential. By configuring Dalvik virtual machine parameters appropriately, developers can optimize memory usage, improve performance, and reduce OOM risk. The right settings should be chosen based on the app’s specific workload and performance requirements.

Further reading