Reputation: 804
Is there is a recommended limit for memory usage in Android applications? Like it should be below 40 MBs or something.. Sometimes my application uses up to 100 MBs in Android Monitor when I load heavy items (news feed with videos) into recyclerview and I wonder if there is some normal memory usage size which we should follow.
Upvotes: 0
Views: 1379
Reputation: 438
Use as little as you can.
To allow multiple running processes, Android sets a hard limit on the heap size alloted for each app. The exact heap size limit varies between devices based on how much RAM the device has available overall. If your app has reached the heap capacity and tries to allocate more memory, the system throws an OutOfMemoryError.
To avoid running out of memory, you can to query the system to determine how much heap space you have available on the current device. You can query the system for this figure by calling getMemoryInfo(). This returns an ActivityManager.MemoryInfo object that provides information about the device's current memory status, including available memory, total memory, and the memory threshold—the memory level below which the system begins to kill processes. The ActivityManager.MemoryInfo class also exposes a simple boolean field, lowMemory that tells you whether the device is running low on memory.
The following code snippet shows an example of how you can use the getMemoryInfo(). method in your application.
public void doSomethingMemoryIntensive() {
// Before doing something that requires a lot of memory,
// check to see whether the device is in a low memory state.
ActivityManager.MemoryInfo memoryInfo = getAvailableMemory();
if (!memoryInfo.lowMemory) {
// Do memory intensive work ...
}
}
// Get a MemoryInfo object for the device's current memory status.
private ActivityManager.MemoryInfo getAvailableMemory() {
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo;
}
Refer to this link for more information.
Upvotes: 3
Reputation: 336
The general recommendation is to use as less as possible. How to achieve that??
Upvotes: 1