有没有办法让整个设备的RAM?我需要它的优化 [英] Is there a way to get total device ram?I need it for an optimization

查看:90
本文介绍了有没有办法让整个设备的RAM?我需要它的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可能包含静态数据,这样,即使当用户返回他能找到数据更快我的应用程序被关闭的lrucache

I have a lrucache that could contain static data, so that even if my app is closed when user returns he can find data faster.

然而,这大约需要10至15 MB的内存,所以我想提出一个分支,如果像这样

However this takes about 10-15 MB of memory and so I'd like to make an if branch like this

if(deviceOverallRAM > treshold)
      preserve static memory on app exit
else
      clear static memory on app exit

所以,我可以得到设备的RAM,也许通过一些隐藏的API?什么会好价值,为treshold一个呢?

So, can I get device's ram, maybe through some hidden api? And what would be a good value for the treshold?

推荐答案

要你读了Android内核在proc / meminfo中文件做到这一点pre-API 16:

To do this pre-API 16 you have to read the proc/meminfo file of the android kernel:

 public long getTotalMemory() {  

    String str1 = "/proc/meminfo";
    String str2;        
    String[] arrayOfString;
    long initial_memory = 0;
    try {
    FileReader localFileReader = new FileReader(str1);
    BufferedReader localBufferedReader = new BufferedReader(    localFileReader, 8192);
    str2 = localBufferedReader.readLine();//meminfo
    arrayOfString = str2.split("\\s+");
    for (String num : arrayOfString) {
    Log.i(str2, num + "\t");
    }
    //total Memory
    initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;   
    localBufferedReader.close();
    return initial_memory;
    } 
    catch (IOException e) 
    {       
        return -1;
    }
  }  

来源:<一href="http://stackoverflow.com/questions/9869761/read-android-device-total-ammount-of-ram-with-proc-meminfo">this问题

但是,在API 16及以后,你可以用下面的code检索的总内存:

However, in API 16 and onward, you can use the following code to retrieve the total memory:

ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;

这篇关于有没有办法让整个设备的RAM?我需要它的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆