指引在Android上的C / C内存使用率++ [英] Guideline for memory usage in C/C++ on Android

查看:109
本文介绍了指引在Android上的C / C内存使用率++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android.com他们说,如果你用Java开发的,可以使用的最大内存为16 MB。至少这是一个设备都应该支持。如果你有一个旧手机,你会发现,你不能得到更多,你会得到一个OutOfMemoryError代替。若你正在做使用NDK同样的事情。在我的应用程序上,我试图让50MB多,到目前为止Android的很好这一点。

On android.com they say, that if you're working in Java, the maximum memory you can use is 16 MB. At least that's the one the devices are supposed to support. If you have an older phone, you'll notice that you can't get more, you get an OutOfMemoryError instead. Not if you're doing the same thing using the NDK. In on of my applications I am trying to get 50MB and more, and so far Android was fine with that.

我没有带相关的上android.com发现任何东西。

I havn't found anything related to that on android.com.

有没有像在Java中的任何限制,也?

Is there any limit like in Java, too?

如果是:什么才是极限

如果没有:什么是一个很好的价值为

If no: What is a good value for that?

问题是,我有取决于大小,以建立我的code。

Problem is, that I have to build my code depending on that size.


我试了一下塞瓦·阿列克谢耶夫进行了暗示。

I tried what Seva Alekseyev were suggesting.

     root@android:/ # ulimit -a
     ulimit -a
     time(cpu-seconds)    unlimited
     file(blocks)         unlimited
     coredump(blocks)     0
     data(KiB)            unlimited
     stack(KiB)           8192
     lockedmem(KiB)       64
     nofiles(descriptors) 1024
     processes            7806
     flocks               unlimited
     sigpending           7806
     msgqueue(bytes)      819200
     maxnice              40
     maxrtprio            0
     resident-set(KiB)    unlimited
     address-space(KiB)   unlimited
     root@android:/ # ulimit -v
     ulimit -v
     unlimited
     root@android:/ #

我请求(通过使用黄金或新)的内存是虚拟内存(​​的ulimit -v)。因此,有没有机会弄清楚多少我可以得到?!

The memory I am requesting (by using "alloc" or "new") is virtual memory (ulimit -v). So there's no chance to figure out how much I can gain?!

推荐答案

您是受三种类型的内存限制:

You're subject to three types of memory limits:

1)到位人工限制多任务时,以保持系统响应 - 在VM堆限制是这种主要的例子。的ulimit是一个潜在的机制OS提供你更多的限制,但我还没有看到它正在使用限制性在Android设备上。

1) Artificial limits put in place to keep the system responsive when multitasking -- the VM heap limitation is the main example of this. ulimit is a potential mechanism for a the OS to provide further limitations on you, but I have not seen it being used restrictively on Android devices.

2)的基础上可用的实际内存的物理极限。你应该有一个基线设备你正在开发/测试上,而应该是pretty激进的假设其他进程(后台服务,其他应用程序),需要的内存了。还记得在使用该内存与操作系统与操作系统版本而异(和往往会随着时间增加)。股票Android不换,所以如果你走得太远你就死定了。一个潜在的方案是一个Nexus之一(512MB RAM),与音频播放机和电话应用在后台准备,和一个气球的服务进食另一个100MB物理存储器得到一些余地;在这种配置中,你还是会发现超过100MB可用。

2) Physical limits based on available real memory. You should have a baseline device you're developing/testing on, and should be pretty aggressive in assume other processes (background services, other apps) need memory too. Also remember that memory in use by the OS varies with OS version (and will tend to increase over time). Stock Android doesn't swap, so if you go too far you're dead. One potential scenario is a Nexus One (512MB RAM) with an audio player and the phone app going in the background, and a "balloon" service eating another 100MB physical memory to give some leeway; in this configuration you'll still find more than 100MB available.

3)基于地址空间的虚拟内存限制。普通的Andr​​oid允许内存过量,所以如果你问与512MB的RAM设备上的1GB虚拟分配(通过mmap的,等等),它不会闪烁,这往往是做一个非常有用的东西。但是,当你再触摸内存,它需要被带入物理内存。如果是只读的,在物理内存中的页面,他们可以被驱逐出场,但很快你会跑出来,没有交换 - 死了。 (组合和过量使用,并没有交换直接导致在处理内存不足的形势下死亡,而不是像malloc的返回null可恢复的错误)。

3) Virtual memory limits based on address space. Stock android allows overcommitment of memory, so it won't blink if you ask for a 1GB virtual allocation (via mmap, etc) on a device with 512MB of RAM, and this is often a very useful thing to do. However, when you then touch the memory, it needs to be brought into physical memory. If there are read-only pages in physical memory they can be ejected, but soon enough you're going to run out, and without swap -- dead. (The combination and overcommit and no swap leads directly to process death in out-of-memory situations, rather than recoverable errors like malloc returning null).

最后,值得一提的是,释放calloc /的malloc /新是否需要物理分配是分配相关的,但它不是大量的网页是安全的假设,是的,尤其是对分配较少。所以:如果你正在处理以< 100 MB的标准,很乖的分配,你在清澈的可能是 - 但是测试!如果你正在处理大量的,你想内存映射数据,MMAP是你的朋友,使用时要小心,并仅PROT_READ使用时,是你最好的朋友。如果你正在处理> 100 MB的物理内存分配,期望在现代设备很好地运行,但你必须仔细定义一个基准测试,测试,测试,因为在检测出内存不足的情况​​下,飞通常是不可能

Finally, it's worth noting that whether calloc/malloc/new require physical allocation is allocator-dependent, but it's safer to assume yes, especially for allocations less than a large number of pages. So: If you're dealing with < 100 MB of standard, well behaved allocations, you're probably in the clear -- but test! If you're dealing with large amounts of data that you'd like memory mapped, mmap is your friend, when used carefully, and is your best friend when used with PROT_READ only. And if you're dealing with > 100 MB of physical memory allocations, expect to run quite nicely on modern devices, but you'll have to define a baseline carefully and test, test, test, since detecting out-of-memory situations on the fly is not generally possible.

还要说明一点:APP_CMD_LOW_MEMORY存在,并清除缓存的好地方,但不能保证它被称为及时挽救你的生命。它并不会改变大局。

One more note: APP_CMD_LOW_MEMORY exists, and is a great place to purge caches, but there's no guarantee it's called in time to save your life. It doesn't change the overall picture at all.

这篇关于指引在Android上的C / C内存使用率++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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