如何增加Android应用程序的堆大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11275650/
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
How to increase heap size of an android application?
提问by cooxie
I am writing an Android application which uses several 3D models. Such a model with textures can take up a lot of memory. I found out the manufacturer sets a limit on the heap size an application can use. For example my tablet Samsung Galaxy Tab 8.9 P7310 can take up 64MB of memory.
我正在编写一个使用多个 3D 模型的 Android 应用程序。这种带有纹理的模型会占用大量内存。我发现制造商对应用程序可以使用的堆大小设置了限制。例如我的平板电脑 Samsung Galaxy Tab 8.9 P7310 可以占用 64MB 的内存。
Is there a way to increase this size of memory an application can use?
有没有办法增加应用程序可以使用的内存大小?
回答by Raghav Sood
You can use android:largeHeap="true"to request a larger heap size, but this will not work on any pre Honeycomb devices. On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above.
您可以使用android:largeHeap="true"来请求更大的堆大小,但这不适用于任何预蜂窝设备。在 2.3 之前的设备上,您可以使用 VMRuntime 类,但这不适用于 Gingerbread 及更高版本。
The only way to have as large a limit as possible is to do memory intensive tasks via the NDK, as the NDK does not impose memory limits like the SDK.
拥有尽可能大的限制的唯一方法是通过 NDK 执行内存密集型任务,因为 NDK 不像 SDK 那样强加内存限制。
Alternatively, you could only load the part of the model that is currently in view, and load the rest as you need it, while removing the unused parts from memory. However, this may not be possible, depending on your app.
或者,您可以只加载当前可见的模型部分,并根据需要加载其余部分,同时从内存中删除未使用的部分。但是,这可能无法实现,具体取决于您的应用。
回答by CommonsWare
Is there a way to increase this size of memory an application can use?
有没有办法增加应用程序可以使用的内存大小?
Applications running on API Level 11+ can have android:largeHeap="true"
on the <application>
element in the manifest to request a larger-than-normal heap size, and getLargeMemoryClass()
on ActivityManager
will tell you how big that heap is. However:
在 API 级别 11+android:largeHeap="true"
上运行的应用程序可以<application>
在清单中的元素上请求大于正常的堆大小,并且getLargeMemoryClass()
onActivityManager
会告诉您该堆有多大。然而:
This only works on API Level 11+ (i.e., Honeycomb and beyond)
There is no guarantee how large the large heap will be
The user will perceive your large-heap request, because it will
force their other apps out of RAMterminate other apps' processes to free up system RAM for use by your large heapBecause of #3, and the fact that I expect that
android:largeHeap
will be abused, support for this may be abandoned in the future, or the user may be warned about this at install time (e.g., you will need to request a special permission for it)Presently, this feature is lightly documented
这仅适用于 API 级别 11+(即 Honeycomb 及更高级别)
无法保证大堆有多大
用户会感觉到你的大堆请求,因为它会
迫使他们的其他应用程序耗尽 RAM终止其他应用程序的进程以释放系统 RAM 供你的大堆使用由于#3,以及我预计
android:largeHeap
会被滥用的事实,将来可能会放弃对此的支持,或者可能会在安装时警告用户(例如,您需要为其请求特殊许可)目前,此功能的文档很少
回答by android developer
you can't increase the heap size dynamically.
您不能动态增加堆大小。
you can request to use more by using android:largeHeap="true"
in the manifest.
您可以通过android:largeHeap="true"
在清单中使用来请求使用更多。
also, you can use native memory (NDK & JNI)
, so you actually bypass the heap size limitation.
此外,您可以使用native memory (NDK & JNI)
,因此您实际上绕过了堆大小限制。
here are some posts i've made about it:
这是我发表的一些帖子:
and here's a library i've made for it:
这是我为它制作的图书馆:
回答by Yura Shinkarev
Use second process. Declare at AndroidManifest
new Service
with
使用第二个过程。声明在AndroidManifest
新Service
与
android:process=":second"
Exchange between first and second process over BroadcastReceiver
第一和第二进程之间的交换结束 BroadcastReceiver
回答by Web Developer in Pune
This can be done by two ways according to your Android OS.
根据您的 Android 操作系统,这可以通过两种方式完成。
- You can use
android:largeHeap="true"
in application tag of Android manifest to request a larger heap size, but this will not work on any pre Honeycomb devices. - On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above See below how to do it.
- 您可以
android:largeHeap="true"
在 Android 清单的应用程序标签中使用来请求更大的堆大小,但这不适用于任何蜂窝设备之前的设备。 - 在 2.3 之前的设备上,您可以使用VMRuntime 类,但这不适用于 Gingerbread 及更高版本 请参阅下文如何操作。
VMRuntime.getRuntime().setMinimumHeapSize(BIGGER_SIZE);
Before Setting HeapSize make sure that you have entered the appropriate size which will not affect other application or OS functionality. Before settings just check how much size your app takes & then set the size just to fulfill your job. Dont use so much of memory otherwise other apps might affect.
在设置 HeapSize 之前,请确保您输入了不会影响其他应用程序或操作系统功能的适当大小。在设置之前,只需检查您的应用程序需要多少大小,然后设置大小以完成您的工作。不要使用太多内存,否则其他应用程序可能会影响。
Reference: http://dwij.co.in/increase-heap-size-of-android-application
参考:http: //dwij.co.in/increase-heap-size-of-android-application
回答by Hyman
From what I remember you could use VMRuntime
class in early Android versions but now you just can't anymore.
我记得你可以VMRuntime
在早期的 Android 版本中使用class,但现在你不能再使用了。
I don't think letting the developer choose the heap size in a mobile environment can be considered so safe though. I think it's easier that you can find a way to modify the heap size in a specific device (not on the programming side) that by trying to modify it from the application itself.
我不认为让开发人员在移动环境中选择堆大小可以被认为是安全的。我认为通过尝试从应用程序本身修改堆大小,您可以找到一种方法来修改特定设备(而不是在编程方面)中的堆大小。
回答by Zon
Increasing Java Heap unfairly eats deficit mobile resurces. Sometimes it is sufficient to just wait for garbage collectorand then resume your operations after heap space is reduced. Use this static methodthen.
增加 Java 堆不公平地吃掉了赤字的移动资源。有时只需等待垃圾收集器,然后在堆空间减少后恢复操作就足够了。然后使用此静态方法。