CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-native-device-info

Get device information using react-native

Pending
Overview
Eval results
Files

memory-storage.mddocs/

Memory and Storage

Memory usage and storage capacity information for performance monitoring and storage management. Essential for optimizing app performance and managing device resources.

Capabilities

Memory Information

Get memory usage information for performance monitoring.

/**
 * Get used memory (async)
 * @returns Promise resolving to used memory in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getUsedMemory(): Promise<number>;

/**
 * Get used memory (sync)
 * @returns Used memory in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getUsedMemorySync(): number;

/**
 * Get total memory (async)
 * @returns Promise resolving to total memory in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getTotalMemory(): Promise<number>;

/**
 * Get total memory (sync)
 * @returns Total memory in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getTotalMemorySync(): number;

/**
 * Get maximum memory (async)
 * @returns Promise resolving to maximum memory in bytes
 * @platforms Android, Windows, Web
 */
function getMaxMemory(): Promise<number>;

/**
 * Get maximum memory (sync)
 * @returns Maximum memory in bytes
 * @platforms Android, Windows, Web
 */
function getMaxMemorySync(): number;

Usage Examples:

import { 
  getUsedMemory, 
  getTotalMemory, 
  getMaxMemory 
} from 'react-native-device-info';

// Memory monitoring
const usedMemory = await getUsedMemory();
const totalMemory = await getTotalMemory();
const maxMemory = await getMaxMemory();

// Convert bytes to MB for display
const usedMB = Math.round(usedMemory / 1024 / 1024);
const totalMB = Math.round(totalMemory / 1024 / 1024);
const maxMB = Math.round(maxMemory / 1024 / 1024);

console.log(`Memory Usage: ${usedMB}MB / ${totalMB}MB`);
console.log(`Max Memory: ${maxMB}MB`);

// Memory usage percentage
const memoryUsagePercent = (usedMemory / totalMemory) * 100;
console.log(`Memory Usage: ${Math.round(memoryUsagePercent)}%`);

// Memory warnings
if (memoryUsagePercent > 80) {
  console.warn('High memory usage detected');
}

// Performance optimization decisions
const availableMemory = totalMemory - usedMemory;
const canLoadLargeAssets = availableMemory > 100 * 1024 * 1024; // 100MB
console.log(`Can load large assets: ${canLoadLargeAssets}`);

Storage Information

Get disk storage capacity and free space information.

/**
 * Get free disk storage (async)
 * @param storageType - Type of storage to check (iOS only)
 * @returns Promise resolving to free disk space in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getFreeDiskStorage(storageType?: AvailableCapacityType): Promise<number>;

/**
 * Get free disk storage (sync)
 * @param storageType - Type of storage to check (iOS only)
 * @returns Free disk space in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getFreeDiskStorageSync(storageType?: AvailableCapacityType): number;

/**
 * Get total disk capacity (async)
 * @returns Promise resolving to total disk capacity in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getTotalDiskCapacity(): Promise<number>;

/**
 * Get total disk capacity (sync)
 * @returns Total disk capacity in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getTotalDiskCapacitySync(): number;

Usage Examples:

import { 
  getFreeDiskStorage, 
  getTotalDiskCapacity 
} from 'react-native-device-info';

// Storage monitoring
const freeSpace = await getFreeDiskStorage();
const totalSpace = await getTotalDiskCapacity();

// Convert bytes to GB for display
const freeGB = Math.round(freeSpace / 1024 / 1024 / 1024 * 100) / 100;
const totalGB = Math.round(totalSpace / 1024 / 1024 / 1024 * 100) / 100;

console.log(`Storage: ${freeGB}GB free / ${totalGB}GB total`);

// Storage usage percentage
const storageUsedPercent = ((totalSpace - freeSpace) / totalSpace) * 100;
console.log(`Storage Used: ${Math.round(storageUsedPercent)}%`);

// Storage warnings
if (freeGB < 1) {
  console.warn('Low storage space detected');
}

// Feature gating based on available storage
const canDownloadContent = freeSpace > 500 * 1024 * 1024; // 500MB
const canCacheImages = freeSpace > 100 * 1024 * 1024; // 100MB

console.log(`Can download content: ${canDownloadContent}`);
console.log(`Can cache images: ${canCacheImages}`);

iOS Storage Types

iOS-specific storage capacity checking with different storage types.

type AvailableCapacityType = 'total' | 'important' | 'opportunistic';

Usage Examples:

import { getFreeDiskStorage } from 'react-native-device-info';
import { Platform } from 'react-native';

if (Platform.OS === 'ios') {
  // Different storage types on iOS
  const totalSpace = await getFreeDiskStorage('total');
  const importantSpace = await getFreeDiskStorage('important');
  const opportunisticSpace = await getFreeDiskStorage('opportunistic');
  
  const totalGB = Math.round(totalSpace / 1024 / 1024 / 1024 * 100) / 100;
  const importantGB = Math.round(importantSpace / 1024 / 1024 / 1024 * 100) / 100;
  const opportunisticGB = Math.round(opportunisticSpace / 1024 / 1024 / 1024 * 100) / 100;
  
  console.log(`Total available: ${totalGB}GB`);
  console.log(`Important data: ${importantGB}GB`);
  console.log(`Opportunistic data: ${opportunisticGB}GB`);
  
  // Use appropriate storage type for different content
  const canStoreImportantData = importantSpace > 100 * 1024 * 1024; // 100MB
  const canStoreOptionalData = opportunisticSpace > 500 * 1024 * 1024; // 500MB
}

Legacy Storage Methods

Legacy storage methods for backward compatibility.

/**
 * Get free disk storage (legacy method, async)
 * @returns Promise resolving to free disk space in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getFreeDiskStorageOld(): Promise<number>;

/**
 * Get free disk storage (legacy method, sync)
 * @returns Free disk space in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getFreeDiskStorageOldSync(): number;

/**
 * Get total disk capacity (legacy method, async)
 * @returns Promise resolving to total disk capacity in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getTotalDiskCapacityOld(): Promise<number>;

/**
 * Get total disk capacity (legacy method, sync)
 * @returns Total disk capacity in bytes
 * @platforms Android, iOS, Windows, Web
 */
function getTotalDiskCapacityOldSync(): number;

Usage Examples:

import { 
  getFreeDiskStorageOld, 
  getTotalDiskCapacityOld 
} from 'react-native-device-info';

// Legacy storage methods for backward compatibility
const freeSpaceOld = await getFreeDiskStorageOld();
const totalSpaceOld = await getTotalDiskCapacityOld();

// Use for compatibility with older Android versions
if (Platform.OS === 'android') {
  // Some older Android versions may return different values
  const freeSpaceLegacy = await getFreeDiskStorageOld();
  const freeSpaceModern = await getFreeDiskStorage();
  
  console.log(`Legacy method: ${Math.round(freeSpaceLegacy / 1024 / 1024)}MB`);
  console.log(`Modern method: ${Math.round(freeSpaceModern / 1024 / 1024)}MB`);
}

Performance Monitoring

Utility functions for performance monitoring and optimization.

Usage Examples:

import { 
  getUsedMemory, 
  getTotalMemory, 
  getFreeDiskStorage 
} from 'react-native-device-info';

// Performance monitoring class
class PerformanceMonitor {
  static async getSystemHealth() {
    const usedMemory = await getUsedMemory();
    const totalMemory = await getTotalMemory();
    const freeStorage = await getFreeDiskStorage();
    
    const memoryUsagePercent = (usedMemory / totalMemory) * 100;
    const freeStorageGB = freeStorage / 1024 / 1024 / 1024;
    
    return {
      memoryUsage: Math.round(memoryUsagePercent),
      freeStorage: Math.round(freeStorageGB * 100) / 100,
      isLowMemory: memoryUsagePercent > 80,
      isLowStorage: freeStorageGB < 1,
      canPerformHeavyOperations: memoryUsagePercent < 60 && freeStorageGB > 2
    };
  }
  
  static async logSystemHealth() {
    const health = await this.getSystemHealth();
    console.log('System Health:', health);
    
    if (health.isLowMemory) {
      console.warn('Low memory detected - consider reducing memory usage');
    }
    
    if (health.isLowStorage) {
      console.warn('Low storage detected - consider cleaning up cached data');
    }
    
    return health;
  }
}

// Usage
const systemHealth = await PerformanceMonitor.getSystemHealth();
if (!systemHealth.canPerformHeavyOperations) {
  console.log('Deferring heavy operations due to system constraints');
}

Types

type AvailableCapacityType = 'total' | 'important' | 'opportunistic';

Install with Tessl CLI

npx tessl i tessl/npm-react-native-device-info

docs

application-information.md

battery-power.md

device-identification.md

hardware-features.md

index.md

memory-storage.md

network-connectivity.md

react-hooks.md

system-information.md

tile.json