0
# Memory and Storage
1
2
Memory usage and storage capacity information for performance monitoring and storage management. Essential for optimizing app performance and managing device resources.
3
4
## Capabilities
5
6
### Memory Information
7
8
Get memory usage information for performance monitoring.
9
10
```typescript { .api }
11
/**
12
* Get used memory (async)
13
* @returns Promise resolving to used memory in bytes
14
* @platforms Android, iOS, Windows, Web
15
*/
16
function getUsedMemory(): Promise<number>;
17
18
/**
19
* Get used memory (sync)
20
* @returns Used memory in bytes
21
* @platforms Android, iOS, Windows, Web
22
*/
23
function getUsedMemorySync(): number;
24
25
/**
26
* Get total memory (async)
27
* @returns Promise resolving to total memory in bytes
28
* @platforms Android, iOS, Windows, Web
29
*/
30
function getTotalMemory(): Promise<number>;
31
32
/**
33
* Get total memory (sync)
34
* @returns Total memory in bytes
35
* @platforms Android, iOS, Windows, Web
36
*/
37
function getTotalMemorySync(): number;
38
39
/**
40
* Get maximum memory (async)
41
* @returns Promise resolving to maximum memory in bytes
42
* @platforms Android, Windows, Web
43
*/
44
function getMaxMemory(): Promise<number>;
45
46
/**
47
* Get maximum memory (sync)
48
* @returns Maximum memory in bytes
49
* @platforms Android, Windows, Web
50
*/
51
function getMaxMemorySync(): number;
52
```
53
54
**Usage Examples:**
55
56
```typescript
57
import {
58
getUsedMemory,
59
getTotalMemory,
60
getMaxMemory
61
} from 'react-native-device-info';
62
63
// Memory monitoring
64
const usedMemory = await getUsedMemory();
65
const totalMemory = await getTotalMemory();
66
const maxMemory = await getMaxMemory();
67
68
// Convert bytes to MB for display
69
const usedMB = Math.round(usedMemory / 1024 / 1024);
70
const totalMB = Math.round(totalMemory / 1024 / 1024);
71
const maxMB = Math.round(maxMemory / 1024 / 1024);
72
73
console.log(`Memory Usage: ${usedMB}MB / ${totalMB}MB`);
74
console.log(`Max Memory: ${maxMB}MB`);
75
76
// Memory usage percentage
77
const memoryUsagePercent = (usedMemory / totalMemory) * 100;
78
console.log(`Memory Usage: ${Math.round(memoryUsagePercent)}%`);
79
80
// Memory warnings
81
if (memoryUsagePercent > 80) {
82
console.warn('High memory usage detected');
83
}
84
85
// Performance optimization decisions
86
const availableMemory = totalMemory - usedMemory;
87
const canLoadLargeAssets = availableMemory > 100 * 1024 * 1024; // 100MB
88
console.log(`Can load large assets: ${canLoadLargeAssets}`);
89
```
90
91
### Storage Information
92
93
Get disk storage capacity and free space information.
94
95
```typescript { .api }
96
/**
97
* Get free disk storage (async)
98
* @param storageType - Type of storage to check (iOS only)
99
* @returns Promise resolving to free disk space in bytes
100
* @platforms Android, iOS, Windows, Web
101
*/
102
function getFreeDiskStorage(storageType?: AvailableCapacityType): Promise<number>;
103
104
/**
105
* Get free disk storage (sync)
106
* @param storageType - Type of storage to check (iOS only)
107
* @returns Free disk space in bytes
108
* @platforms Android, iOS, Windows, Web
109
*/
110
function getFreeDiskStorageSync(storageType?: AvailableCapacityType): number;
111
112
/**
113
* Get total disk capacity (async)
114
* @returns Promise resolving to total disk capacity in bytes
115
* @platforms Android, iOS, Windows, Web
116
*/
117
function getTotalDiskCapacity(): Promise<number>;
118
119
/**
120
* Get total disk capacity (sync)
121
* @returns Total disk capacity in bytes
122
* @platforms Android, iOS, Windows, Web
123
*/
124
function getTotalDiskCapacitySync(): number;
125
```
126
127
**Usage Examples:**
128
129
```typescript
130
import {
131
getFreeDiskStorage,
132
getTotalDiskCapacity
133
} from 'react-native-device-info';
134
135
// Storage monitoring
136
const freeSpace = await getFreeDiskStorage();
137
const totalSpace = await getTotalDiskCapacity();
138
139
// Convert bytes to GB for display
140
const freeGB = Math.round(freeSpace / 1024 / 1024 / 1024 * 100) / 100;
141
const totalGB = Math.round(totalSpace / 1024 / 1024 / 1024 * 100) / 100;
142
143
console.log(`Storage: ${freeGB}GB free / ${totalGB}GB total`);
144
145
// Storage usage percentage
146
const storageUsedPercent = ((totalSpace - freeSpace) / totalSpace) * 100;
147
console.log(`Storage Used: ${Math.round(storageUsedPercent)}%`);
148
149
// Storage warnings
150
if (freeGB < 1) {
151
console.warn('Low storage space detected');
152
}
153
154
// Feature gating based on available storage
155
const canDownloadContent = freeSpace > 500 * 1024 * 1024; // 500MB
156
const canCacheImages = freeSpace > 100 * 1024 * 1024; // 100MB
157
158
console.log(`Can download content: ${canDownloadContent}`);
159
console.log(`Can cache images: ${canCacheImages}`);
160
```
161
162
### iOS Storage Types
163
164
iOS-specific storage capacity checking with different storage types.
165
166
```typescript { .api }
167
type AvailableCapacityType = 'total' | 'important' | 'opportunistic';
168
```
169
170
**Usage Examples:**
171
172
```typescript
173
import { getFreeDiskStorage } from 'react-native-device-info';
174
import { Platform } from 'react-native';
175
176
if (Platform.OS === 'ios') {
177
// Different storage types on iOS
178
const totalSpace = await getFreeDiskStorage('total');
179
const importantSpace = await getFreeDiskStorage('important');
180
const opportunisticSpace = await getFreeDiskStorage('opportunistic');
181
182
const totalGB = Math.round(totalSpace / 1024 / 1024 / 1024 * 100) / 100;
183
const importantGB = Math.round(importantSpace / 1024 / 1024 / 1024 * 100) / 100;
184
const opportunisticGB = Math.round(opportunisticSpace / 1024 / 1024 / 1024 * 100) / 100;
185
186
console.log(`Total available: ${totalGB}GB`);
187
console.log(`Important data: ${importantGB}GB`);
188
console.log(`Opportunistic data: ${opportunisticGB}GB`);
189
190
// Use appropriate storage type for different content
191
const canStoreImportantData = importantSpace > 100 * 1024 * 1024; // 100MB
192
const canStoreOptionalData = opportunisticSpace > 500 * 1024 * 1024; // 500MB
193
}
194
```
195
196
### Legacy Storage Methods
197
198
Legacy storage methods for backward compatibility.
199
200
```typescript { .api }
201
/**
202
* Get free disk storage (legacy method, async)
203
* @returns Promise resolving to free disk space in bytes
204
* @platforms Android, iOS, Windows, Web
205
*/
206
function getFreeDiskStorageOld(): Promise<number>;
207
208
/**
209
* Get free disk storage (legacy method, sync)
210
* @returns Free disk space in bytes
211
* @platforms Android, iOS, Windows, Web
212
*/
213
function getFreeDiskStorageOldSync(): number;
214
215
/**
216
* Get total disk capacity (legacy method, async)
217
* @returns Promise resolving to total disk capacity in bytes
218
* @platforms Android, iOS, Windows, Web
219
*/
220
function getTotalDiskCapacityOld(): Promise<number>;
221
222
/**
223
* Get total disk capacity (legacy method, sync)
224
* @returns Total disk capacity in bytes
225
* @platforms Android, iOS, Windows, Web
226
*/
227
function getTotalDiskCapacityOldSync(): number;
228
```
229
230
**Usage Examples:**
231
232
```typescript
233
import {
234
getFreeDiskStorageOld,
235
getTotalDiskCapacityOld
236
} from 'react-native-device-info';
237
238
// Legacy storage methods for backward compatibility
239
const freeSpaceOld = await getFreeDiskStorageOld();
240
const totalSpaceOld = await getTotalDiskCapacityOld();
241
242
// Use for compatibility with older Android versions
243
if (Platform.OS === 'android') {
244
// Some older Android versions may return different values
245
const freeSpaceLegacy = await getFreeDiskStorageOld();
246
const freeSpaceModern = await getFreeDiskStorage();
247
248
console.log(`Legacy method: ${Math.round(freeSpaceLegacy / 1024 / 1024)}MB`);
249
console.log(`Modern method: ${Math.round(freeSpaceModern / 1024 / 1024)}MB`);
250
}
251
```
252
253
### Performance Monitoring
254
255
Utility functions for performance monitoring and optimization.
256
257
**Usage Examples:**
258
259
```typescript
260
import {
261
getUsedMemory,
262
getTotalMemory,
263
getFreeDiskStorage
264
} from 'react-native-device-info';
265
266
// Performance monitoring class
267
class PerformanceMonitor {
268
static async getSystemHealth() {
269
const usedMemory = await getUsedMemory();
270
const totalMemory = await getTotalMemory();
271
const freeStorage = await getFreeDiskStorage();
272
273
const memoryUsagePercent = (usedMemory / totalMemory) * 100;
274
const freeStorageGB = freeStorage / 1024 / 1024 / 1024;
275
276
return {
277
memoryUsage: Math.round(memoryUsagePercent),
278
freeStorage: Math.round(freeStorageGB * 100) / 100,
279
isLowMemory: memoryUsagePercent > 80,
280
isLowStorage: freeStorageGB < 1,
281
canPerformHeavyOperations: memoryUsagePercent < 60 && freeStorageGB > 2
282
};
283
}
284
285
static async logSystemHealth() {
286
const health = await this.getSystemHealth();
287
console.log('System Health:', health);
288
289
if (health.isLowMemory) {
290
console.warn('Low memory detected - consider reducing memory usage');
291
}
292
293
if (health.isLowStorage) {
294
console.warn('Low storage detected - consider cleaning up cached data');
295
}
296
297
return health;
298
}
299
}
300
301
// Usage
302
const systemHealth = await PerformanceMonitor.getSystemHealth();
303
if (!systemHealth.canPerformHeavyOperations) {
304
console.log('Deferring heavy operations due to system constraints');
305
}
306
```
307
308
## Types
309
310
```typescript { .api }
311
type AvailableCapacityType = 'total' | 'important' | 'opportunistic';
312
```