如何增加android应用程序的堆大小?

时间:2020-12-29 17:20:21

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.

我正在编写一个Android应用程序,它使用了几个3D模型。这种有纹理的模型会占用很多内存。我发现制造商对应用程序可以使用的堆大小设置了一个限制。例如,我的平板三星Galaxy Tab 8.9 P7310可以占用64MB的内存。

Is there a way to increase this size of memory an application can use?

是否有办法增加应用程序可以使用的内存大小?

8 个解决方案

#1


191  

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”来请求更大的堆大小,但是这将不适用于任何预蜂窝设备。在pre 2.3设备上,您可以使用VMRuntime类,但这不适用于姜饼或以上产品。

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.

另外,您可以只加载当前视图中的模型部分,并根据需要加载其余部分,同时从内存中删除未使用的部分。然而,这可能是不可能的,这取决于你的应用。

#2


103  

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"来请求一个大于正常的堆大小,而ActivityManager上的getLargeMemoryClass()将告诉您这个堆有多大。然而:

  1. This only works on API Level 11+ (i.e., Honeycomb and beyond)

    这只适用于API级别11+(即。、蜂窝和以外)

  2. There is no guarantee how large the large heap will be

    不能保证这个大堆会有多大。

  3. The user will perceive your large-heap request, because it will force their other apps out of RAM terminate other apps' processes to free up system RAM for use by your large heap

    用户将感知到您的大堆请求,因为它将迫使其他应用程序离开RAM,终止其他应用程序的进程,释放系统RAM供您的大堆使用

  4. Because 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)

    由于#3,以及我预计android:largeHeap将会被滥用,支持这个可能在将来被放弃,或者用户在安装时可能会受到警告(例如,你需要请求特别许可)

  5. Presently, this feature is lightly documented

    目前,这个特性已经得到了少量的文档说明

#3


19  

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.

此外,您可以使用本机内存(NDK & JNI),因此您实际上绕过了堆大小限制。

here are some posts i've made about it:

以下是我写的一些文章:

and here's a library i've made for it:

这是我为它建的一个图书馆:

#4


5  

Use second process. Declare at AndroidManifest new Service with

使用第二个过程。在AndroidManifest新服务中声明

android:process=":second"

Exchange between first and second process over BroadcastReceiver

第一和第二进程通过广播接收机进行交换

#5


3  

From what I remember you could use VMRuntime class in early Android versions but now you just can't anymore.

据我所知,你可以在早期的Android版本中使用VMRuntime类,但是现在你再也不能用了。

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.

我不认为让开发人员在移动环境中选择堆大小是安全的。我认为,通过尝试从应用程序本身修改堆大小,您可以更容易地找到一种方法来修改特定设备中的堆大小(而不是在编程方面)。

#6


3  

This can be done by two ways according to your Android OS.

根据你的Android操作系统,这可以通过两种方式实现。

  1. 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.
  2. 您可以在android manifest的应用程序标记中使用android:largeHeap=“true”来请求更大的堆大小,但这在任何pre - Honeycomb设备上都不起作用。
  3. 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.
  4. 在pre 2.3设备上,您可以使用VMRuntime类,但这不适用于姜饼,请参阅下面的方法。
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

#7


0  

Increasing Java Heap unfairly eats deficit mobile resurces. Sometimes it is sufficient to just wait for garbage collector and then resume your operations after heap space is reduced. Use this static method then.

增加Java堆不公平地吃掉了亏空的移动设备表面。有时,只需等待垃圾收集器,然后在减少堆空间之后恢复操作就足够了。然后使用这个静态方法。

#8


-1  

There's another alternate option which is used for tweaking memory settings. All you have to do is add/replace the existing line in gradle.properties file

还有另一个选项用于调整内存设置。您所要做的就是在gradle中添加/替换现有的行。属性文件

org.gradle.jvmargs=-Xmx1024m

org.gradle.jvmargs = -Xmx1024m

#1


191  

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”来请求更大的堆大小,但是这将不适用于任何预蜂窝设备。在pre 2.3设备上,您可以使用VMRuntime类,但这不适用于姜饼或以上产品。

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.

另外,您可以只加载当前视图中的模型部分,并根据需要加载其余部分,同时从内存中删除未使用的部分。然而,这可能是不可能的,这取决于你的应用。

#2


103  

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"来请求一个大于正常的堆大小,而ActivityManager上的getLargeMemoryClass()将告诉您这个堆有多大。然而:

  1. This only works on API Level 11+ (i.e., Honeycomb and beyond)

    这只适用于API级别11+(即。、蜂窝和以外)

  2. There is no guarantee how large the large heap will be

    不能保证这个大堆会有多大。

  3. The user will perceive your large-heap request, because it will force their other apps out of RAM terminate other apps' processes to free up system RAM for use by your large heap

    用户将感知到您的大堆请求,因为它将迫使其他应用程序离开RAM,终止其他应用程序的进程,释放系统RAM供您的大堆使用

  4. Because 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)

    由于#3,以及我预计android:largeHeap将会被滥用,支持这个可能在将来被放弃,或者用户在安装时可能会受到警告(例如,你需要请求特别许可)

  5. Presently, this feature is lightly documented

    目前,这个特性已经得到了少量的文档说明

#3


19  

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.

此外,您可以使用本机内存(NDK & JNI),因此您实际上绕过了堆大小限制。

here are some posts i've made about it:

以下是我写的一些文章:

and here's a library i've made for it:

这是我为它建的一个图书馆:

#4


5  

Use second process. Declare at AndroidManifest new Service with

使用第二个过程。在AndroidManifest新服务中声明

android:process=":second"

Exchange between first and second process over BroadcastReceiver

第一和第二进程通过广播接收机进行交换

#5


3  

From what I remember you could use VMRuntime class in early Android versions but now you just can't anymore.

据我所知,你可以在早期的Android版本中使用VMRuntime类,但是现在你再也不能用了。

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.

我不认为让开发人员在移动环境中选择堆大小是安全的。我认为,通过尝试从应用程序本身修改堆大小,您可以更容易地找到一种方法来修改特定设备中的堆大小(而不是在编程方面)。

#6


3  

This can be done by two ways according to your Android OS.

根据你的Android操作系统,这可以通过两种方式实现。

  1. 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.
  2. 您可以在android manifest的应用程序标记中使用android:largeHeap=“true”来请求更大的堆大小,但这在任何pre - Honeycomb设备上都不起作用。
  3. 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.
  4. 在pre 2.3设备上,您可以使用VMRuntime类,但这不适用于姜饼,请参阅下面的方法。
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

#7


0  

Increasing Java Heap unfairly eats deficit mobile resurces. Sometimes it is sufficient to just wait for garbage collector and then resume your operations after heap space is reduced. Use this static method then.

增加Java堆不公平地吃掉了亏空的移动设备表面。有时,只需等待垃圾收集器,然后在减少堆空间之后恢复操作就足够了。然后使用这个静态方法。

#8


-1  

There's another alternate option which is used for tweaking memory settings. All you have to do is add/replace the existing line in gradle.properties file

还有另一个选项用于调整内存设置。您所要做的就是在gradle中添加/替换现有的行。属性文件

org.gradle.jvmargs=-Xmx1024m

org.gradle.jvmargs = -Xmx1024m