Android应用程序中建议的内存使用限制是多少?

时间:2016-11-30 11:01:05

标签: android performance memory-management

Android应用程序中是否存在建议的内存使用限制?喜欢它应该低于40 MB或类似的东西..有时我的应用程序在Android监视器中使用多达100 MB的重量项目(带有视频的新闻源)到recyclerview,我想知道是否有一些正常的内存使用量,我们应该遵循

2 个答案:

答案 0 :(得分:3)

尽可能少地使用。

  

为了允许多个正在运行的进程,Android设置了一个硬限制   为每个应用分配的堆大小。确切的堆大小限制是不同的   根据设备可用的RAM量,在设备之间进行   总体。如果您的应用已达到堆容量并尝试   分配更多内存,系统抛出 OutOfMemoryError

     

为避免内存不足,您可以查询系统   确定当前可用的堆空间大小   设备。您可以通过调用查询系统中的此图   的 getMemoryInfo() 即可。这将返回 ActivityManager.MemoryInfo 对象   提供有关设备当前内存状态的信息,   包括可用内存,总内存和内存阈值 -   内存级别低于系统开始杀死进程。该    ActivityManager.MemoryInfo 类还公开了一个简单的 布尔 字段,   lowMemory告诉您设备内存是否运行不足。

     

以下代码段显示了如何使用该代码的示例   的 getMemoryInfo() 即可。你申请中的方法。

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;
}

有关详细信息,请参阅此link

答案 1 :(得分:1)

一般建议尽量少用。 如何实现?

  • 正确处理位图(在大多数情况下,您可能需要使用第3位 派对图书馆,如PicassoFrescoGlide)。
  • 在android-studio中使用堆转储并分析消耗ram的内容并检查活动是否泄漏。
  • 如果您要从网络请求大量数据,那么您可以查看flatbuffers它们将减少内存占用和网络请求的有效负载
  • 不使用深层次结构层次结构