Android 安卓内存分配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2131947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android memory allocation
提问by prepbgg
I am getting a "bitmap size exceeds VM budget" error. I have read that there is a 16MB memory limit. In this threadRomain Guy says that "you can only allocate 16 MB of memory for your entire application".
我收到“位图大小超出 VM 预算”错误。我读到有 16MB 的内存限制。在此线程中,Romain Guy 说“您只能为整个应用程序分配 16 MB 的内存”。
However, my app must be running out of memory long before it reaches that limit. So my question is: how do I allocate memory to my application ... how can I get the allocation to my app increased (within the 16MB maximum)?
但是,我的应用程序在达到该限制之前很久就必须耗尽内存。所以我的问题是:我如何为我的应用程序分配内存......我怎样才能增加我的应用程序的分配(在 16MB 最大值内)?
回答by botteaap
As with any Java VM, the heap memory will automatically grow to the max size. But, bitmaps are allocated outside the VM, so you don't "see" them easily in the stats. The best thing you can do is make sure you don't use large bitmaps, or scale them down using
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
与任何 Java VM 一样,堆内存将自动增长到最大大小。但是,位图是在 VM 外部分配的,因此您不会在统计信息中轻松“看到”它们。你能做的最好的事情是确保你不使用大的位图,或者使用http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html缩小它们
From Eclipse you can generate a heap dump when you are on Android 1.6 or up and you can analyze the dump with Eclipse MAT.
当您使用 Android 1.6 或更高版本时,您可以从 Eclipse 生成堆转储,并且您可以使用 Eclipse MAT 分析转储。
Generally you can't control the max heap size on a real device, unless you are working with custom hardware or firmware.
通常,您无法控制真实设备上的最大堆大小,除非您使用自定义硬件或固件。
There should be an article at developer.android.com on dumping the heap on 1.6, but I'm unable to find it. :(
developer.android.com 上应该有一篇关于在 1.6 上转储堆的文章,但我找不到它。:(
Edit
Also, I have to mention that you can request more memory for applications by using
编辑
另外,我不得不提一下,您可以通过使用为应用程序请求更多内存
android:largeHeap="true"
机器人:大堆=“真”
in the manifest. But this is highly ill-adviced as most applications do not need this.
在清单中。但这是非常不明智的,因为大多数应用程序不需要这个。
回答by Romain Guy
Note that the heap limit is device-dependent. On a Droid or Nexus One, that limit is 24 MB (to accommodate the larger graphic resources.)
请注意,堆限制取决于设备。在 Droid 或 Nexus One 上,该限制为 24 MB(以容纳更大的图形资源。)
回答by ThomasW
If you're using threads, then the debugger might be the source of the problem. If you run the app under the debugger, then any threads created will still be retained by the debugger, even when they're finished running. This leads to memory errors that won't occur when the app is running without the debugger.
如果您使用的是线程,那么调试器可能是问题的根源。如果您在调试器下运行应用程序,那么创建的任何线程仍将被调试器保留,即使它们已完成运行。这会导致在没有调试器的情况下运行应用程序时不会发生内存错误。
http://code.google.com/p/android/issues/detail?id=7979
http://code.google.com/p/android/issues/detail?id=7979
https://android.googlesource.com/platform/dalvik/+/master/docs/debugger.html
https://android.googlesource.com/platform/dalvik/+/master/docs/debugger.html
回答by Karthi Ponnusamy
I found the answer for your question recently.
我最近找到了你的问题的答案。
Go to Android SDK Directory and
run sdk manager (Tools->android run this file)
转到 Android SDK 目录并
运行 sdk manager (Tools->android run this file)
In SDK manager go to Tools-->Manage AVDs
在 SDK 管理器中转到 Tools-->Manage AVDs
Now Android Virtual Device Manager Dialogue box is open..
现在 Android 虚拟设备管理器对话框打开了..
in that window select your AVD and click Edit
在该窗口中选择您的 AVD 并单击编辑
Now in SD card section select SIZE and set 1024 MiB in Hardware section click New and select property " Maximum VM application heap Size" Now set 100 (based on your app requirement) Finally click "Edit AVD"
现在在 SD 卡部分选择 SIZE 并在硬件部分设置 1024 MiB 单击新建并选择属性“最大 VM 应用程序堆大小”现在设置 100(根据您的应用程序要求)最后单击“编辑 AVD”
After edit click refresh button on sdk Manager
编辑后单击 sdk 管理器上的刷新按钮
Now run your app in AVD your problem is solved.
现在在 AVD 中运行您的应用程序,您的问题就解决了。