Android Runtime.getRuntime().freeMemory()会返回0吗?

背景

上周基于LruCache实现了一个缓存,实现逻辑大概如下:

//使用剩余内存的1/16作为缓存池的最大值 
int size = Runtime.getRuntime().freeMemory() / 16;

LruCache<String, String> myCache = new LruCache<String, String>(cacheSize) {
       protected int sizeOf(String key, String value) {
           //自定义size计算方式
           ....
       }
   };

以上写法和官方文档Caching Bitmaps类似,区别在于官方文档用的是Runtime.getRuntime().maxMemory(),Runtime.getRuntime().maxMemory()意思虚拟机计划使用的最大内存,freeMemory()意思是当前已开辟内存中的剩余部分。似乎freeMemory()比maxMemory()在内存使用上更加保险,就这样用了,但是上述 代码发布到线上却概率性抛出了IllegalArgumentException,原因是初始化传入的cacheSize小于等于0。

Runtime.getRuntime().freeMemory()会返回0吗?

从API文档看,没有这方面的说明,从意义上看,貌似可能会。但是还是想从源码上佐证一下。
Runtime.getRuntime().freeMemory()具体源码是在Native中实现,具体对应的Native代码在,platform/runtime/gc/heap.h


  // Returns how much free memory we have until we need to grow the heap to perform an allocation.
  // Similar to GetFreeMemoryUntilGC. Implements java.lang.Runtime.freeMemory.
  size_t GetFreeMemory() const {
    size_t byte_allocated = num_bytes_allocated_.load(std::memory_order_seq_cst);
    size_t total_memory = GetTotalMemory();
    // Make sure we don't get a negative number.
    return total_memory - std::min(total_memory, byte_allocated);
  }

从代码看,如果byte_allocated大于等于total_memory,那么的确是返回了0。

那么Runtime.getRuntime().maxMemory()呢?

// Implements java.lang.Runtime.maxMemory, returning the maximum amount of memory a program can
  // consume. For a regular VM this would relate to the -Xmx option and would return -1 if no Xmx
  // were specified. Android apps start with a growth limit (small heap size) which is
  // cleared/extended for large apps.
  size_t GetMaxMemory() const {
    // There is some race conditions in the allocation code that can cause bytes allocated to
    // become larger than growth_limit_ in rare cases.
    return std::max(GetBytesAllocated(), growth_limit_);
  }

Runtime.getRuntime().maxMemory()至少返回的是growth_limit_,这个值是在APP初始化时被赋值的,因此这里返回结果必然大于0。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用提到了Runtime.getRuntime().exec()方法,该方法可以在Android中执行命令或创建进程。根据引用,您可以使用这种方法从Java代码中调用Python脚本。但需要注意的是,引用指出正确的写法应该是Runtime.getRuntime()而不是getruntime。因此,根据您的问题"android8 Runtime.getRuntime().exec",您可以使用以下代码来执行命令或创建进程: ```java Runtime runtime = Runtime.getRuntime(); String command = "your_command_here"; try { Process process = runtime.exec(command); // 处理进程的输出或其他操作 } catch (IOException e) { e.printStackTrace(); } ``` 在上述代码中,将"your_command_here"替换为您要执行的具体命令。这样,您就可以在Android 8及以上版本中使用Runtime.getRuntime().exec()方法执行命令或创建进程了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [使用runtime.getruntime.exec从Java调用python脚本(Calling python script from Java using runtime....](https://blog.csdn.net/weixin_39618730/article/details/110567660)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [Android Runtime.getRuntime().exec()](https://blog.csdn.net/xianrenli38/article/details/117484708)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值