madim
madim

Reputation: 804

What is the recommended limit for memory usage in Android applications?

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

Answers (2)

Arsen Sench
Arsen Sench

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

Vardaan Sharma
Vardaan Sharma

Reputation: 336

The general recommendation is to use as less as possible. How to achieve that??

  • Handling bitmaps properly(In most of the cases you may want to use a 3rd party library like Picasso,Fresco or Glide).
  • Use heap dump in android-studio and analyze what is consuming your ram and check for activity leaks.
  • If you are requesting lots of data from the network then you can have look at flatbuffers they will reduce the memory footprint and payload of a network request
  • Not using deep view hierarchies

Upvotes: 1

Related Questions