memory_info

Flutter plugin to get device memory (RAM) and disk space (ROM) info on Android and iOS.

Requirements

Minimum
Flutter 3.44
Android API 21
iOS 13.0

Supports Swift Package Manager and CocoaPods on iOS, and AGP 9 / built-in Kotlin on Android.

Installation

dependencies:
  memory_info: ^0.1.0

Usage

import 'package:memory_info/memory_info.dart';

final Memory memory = await MemoryInfoPlugin().memoryInfo;
print('Used by app: ${memory.appMem} MB');
print('Total: ${memory.totalMem} MB');
print('Free: ${memory.freeMem} MB');
print('Available to app: ${memory.appAvailableMem} MB'); // iOS only
print('Low memory: ${memory.lowMemory}');

final DiskSpace disk = await MemoryInfoPlugin().diskSpace;
print('Disk free: ${disk.freeSpace} MB');
print('Disk total: ${disk.totalSpace} MB');
print('Disk used: ${disk.usedSpace} MB');

Memory pressure

Listen to the system memory pressure events to release caches before the app gets killed:

final subscription = MemoryInfoPlugin().onMemoryPressure.listen((event) {
  switch (event.level) {
    case MemoryPressureLevel.warning:
    case MemoryPressureLevel.critical:
      imageCache.clear();
    case MemoryPressureLevel.background:
      break;
  }
});

// Later
subscription.cancel();
Level Android iOS
warning TRIM_MEMORY_RUNNING_MODERATE, TRIM_MEMORY_RUNNING_LOW didReceiveMemoryWarningNotification
critical TRIM_MEMORY_RUNNING_CRITICAL, onLowMemory Critical memory pressure (DispatchSource)
background TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE, TRIM_MEMORY_COMPLETE —

On Android, event.androidTrimLevel holds the raw trim level. Since Android 14 the system only sends TRIM_MEMORY_UI_HIDDEN and TRIM_MEMORY_BACKGROUND, so warning and critical are rare there.

Values

All sizes are in megabytes. A value is null when the platform can't provide it.

Memory

Field Android iOS
appMem Total PSS of the app process (Debug.MemoryInfo) App memory footprint (phys_footprint)
totalMem ActivityManager.MemoryInfo.totalMem ProcessInfo.physicalMemory
freeMem ActivityManager.MemoryInfo.availMem Free pages system-wide (host_statistics64)
lowMemory ActivityManager.MemoryInfo.lowMemory true when appAvailableMem is below 25% of the app memory limit
appAvailableMem null Memory the app can still allocate before hitting its limit (os_proc_available_memory)
lowMemoryThreshold ActivityManager.MemoryInfo.threshold null
advertisedMem ActivityManager.MemoryInfo.advertisedMem (Android 14+) null
javaHeapUsedMem Runtime.totalMemory - Runtime.freeMemory null
javaHeapMaxMem Runtime.maxMemory null

On iOS, freeMem only counts completely unused pages. iOS keeps this number low on purpose, so use appAvailableMem to check how close the app is to being terminated for memory.

On the iOS simulator, appAvailableMem and lowMemory are null, because the simulator has no app memory limit.

DiskSpace

Field Android iOS
freeSpace StorageManager.getAllocatableBytes (Android 8+), StatFs before volumeAvailableCapacityForImportantUsage
totalSpace Total size of the data partition (StatFs) volumeTotalCapacity
usedSpace totalSpace - freeSpace totalSpace - freeSpace

freeSpace includes cached data the system can clear when space is needed.

iOS privacy manifest

The plugin ships a PrivacyInfo.xcprivacy declaring its use of the disk space API, so no extra setup is needed.

License

MIT

Libraries

memory_info