如何避免android上的进程的内存限制

转载 2012年03月28日 18:05:56
The Android framework enforces a per-process 24 MB memory limit. On some older devices, such as the G1, the limit is even lower at 16 MB. 

What’s more, the memory used by Bitmaps is included in the limit. For an application manipulating images it is pretty easy to reach this limit and get the process killed with an OOM exception: 

E/dalvikvm-heap(12517): 1048576-byte external allocation too large for this process. 

E/GraphicsJNI(12517): VM won't let us allocate 1048576 bytes 

D/AndroidRuntime(12517): Shutting down VM 

W/dalvikvm(12517): threadid=1: thread exiting with uncaught exception (group=0x4001d7f0) 

E/AndroidRuntime(12517): FATAL EXCEPTION: main 

E/AndroidRuntime(12517): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 

This limit is ridiculously low. For a device, like the Nexus One, with 512MB of physical RAM, setting the per-process memory limit for the foreground activity to only 5% of the RAM is a silly mistake. But anyway, that’s how things are and we have to live with it — i.e. find how to work around it. 

There are two ways to allocate much more memory than the limit: 

One way is to allocate memory from native code. Using the NDK (native development kit) and JNI, it’s possible to allocate memory from the C level (e.g. malloc/free or new/delete), and such allocations are not counted towards the 24 MB limit. It’s true, allocating memory from native code is not as convenient as from Java, but it can be used to store some large amounts of data in RAM (even image data). 

Another way, which works well for images, is to use OpenGL textures — the texture memory is not counted towards the limit. 

To see how much memory your app has really allocated you can use android.os.Debug.getNativeHeapAllocatedSize(). 

Using either of the two techniques presented above, on a Nexus One, I could easily allocate 300MB for a single foreground process — more than 10 times the default 24 MB limit. 

从上面来看使用navtive code分配内存是不在24MB的限制内的(open GL的texture也是使用navtive code分配内存的)。

相关文章推荐

Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法

Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法 来自  Fgamers 2011年 2月 21日 2011.8.18--> 5,848 次阅读 评...

Android 避免bitmap内存限制

在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那么这个错误究竟是怎么来的呢,可以先看一下这篇文章ANDROID BITMAP内存限制OOM,OUT OF...

Android 避免bitmap内存限制

http://bbs.chinaunix.net/viewthread.php?tid=3555271 在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,...
  • cc_lq
  • cc_lq
  • 2011-08-09 21:21
  • 376

Android 避免bitmap内存限制

在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那么这个错误究竟是怎么来的呢,可以先看一下这篇文章ANDROID BITMAP内存限制OOM,OUT OF...

Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法

在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那么这个错误究竟是怎么来的呢,可以先看一下这篇文章ANDROID BITMAP内存限制OOM,OUT OF...

Android 避免bitmap内存限制

在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那么这个错误究竟是怎么来的呢,可以先看一下这篇文章ANDROID BITMAP内存限制OOM,OUT OF...

android避免bitmap内存限制

http://www.eoeandroid.com/thread-80104-1-1.html     在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那...

Android-避免出现bitmap内存限制OUT OF MEMORY的一种方法

在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那么这个错误究竟是怎么来的呢,可以先看一下这篇文章ANDROID BITMAP内存限制OOM,OUT OF...

[Android实例] Android 避免bitmap内存限制

在编写Android程序的时候,我们总是难免会碰到OOM(OUT OF MEMORY)的错误,那么这个错误究竟是怎么来的呢,可以先看一下这篇文章ANDROID BITMAP内存限制OOM,OUT OF...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)