在 Android 中使用 largeheap 是一种好的做法吗?
Is using largeheap in Android manifest a good practice?
我正在 NDK
开发。它挂在 Galaxy S3
。为了测试,我将 android:largeheap = "true"
放在 Manifest
中。然后就没有挂的问题了。
使用 largeHeap="true"
是一种好习惯吗?
Google 是否有可能因为这个标签而拒绝我的构建并且
如何在不使用 largeheap="true"
的情况下防止我的应用挂起?
就我个人而言,如果 正确使用 ,它实际上并不属于 'good/bad practice' 的范畴。
根据 docs:
Most apps should not need this and should instead focus on reducing
their overall memory usage for improved performance. Enabling this
also does not guarantee a fixed increase in available memory, because
some devices are constrained by their total available memory.
如果您已经竭尽全力减少内存使用,但仍然需要它,那么使用它并不是一件坏事。
如果您的应用程序挂起,您将需要直接解决这个问题 - largeHeap
并不是可以解决所有设备问题的魔杖。 Android 培训文档的以下摘录清楚地表明了这一点:
[The] ability to request a large heap is intended only for a
small set of apps that can justify the need to consume more RAM (such
as a large photo editing app). Never request a large heap simply
because you've run out of memory and you need a quick fix—you should
use it only when you know exactly where all your memory is being
allocated and why it must be retained. - (source)
我还应该补充一点,Google 不会 拒绝使用您的应用。
简答
不,如果您需要它这不是一个坏习惯,因为它就在那里。
长答案
官方文档说明
Whether your application's processes should be created with a large
Dalvik heap. This applies to all processes created for the
application.
It only applies to the first application loaded into a
process; if you're using a shared user ID to allow multiple
applications to use a process, they all must use this option
consistently or they will have unpredictable results.
Most apps should
not need this and should instead focus on reducing their overall
memory usage for improved performance. Enabling this also does not
guarantee a fixed increase in available memory, because some devices
are constrained by their total available memory.
一些开发人员使用它来避免 OOM 异常,所以如果您使用它只是为了避免一些 OOM 这是一个非常非常糟糕的做法。
Never request a large heap simply because you've run out of memory and
you need a quick fix. You should use it only when you know exactly
where all your memory is being allocated and why it must be retained
如果你确实需要更多space使用它是可以的,你可以使用getMemoryClass()
to check the heap and getLargeMemoryClass()
大堆。
但是,如果您可以避免使用 largeHeap,那将是最好的方法,正如官方文档所述:
Yet, even when you're confident your app can justify the large heap,
you should avoid requesting it to whatever extent possible. Using the
extra memory will increasingly be to the detriment of the overall user
experience because garbage collection will take longer and system
performance may be slower when task switching or performing other
common operations.
Additionally, the large heap size is not the
same on all devices and may be exactly the same as the regular heap
size. So even if you do request the large heap size, you should call
getMemoryClass() to check the regular heap size and strive to always
stay below that limit.
我也建议你看看这里Managing Your App's Memory
我正在 NDK
开发。它挂在 Galaxy S3
。为了测试,我将 android:largeheap = "true"
放在 Manifest
中。然后就没有挂的问题了。
使用 largeHeap="true"
是一种好习惯吗?
Google 是否有可能因为这个标签而拒绝我的构建并且
如何在不使用 largeheap="true"
的情况下防止我的应用挂起?
就我个人而言,如果 正确使用 ,它实际上并不属于 'good/bad practice' 的范畴。
根据 docs:
Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
如果您已经竭尽全力减少内存使用,但仍然需要它,那么使用它并不是一件坏事。
如果您的应用程序挂起,您将需要直接解决这个问题 - largeHeap
并不是可以解决所有设备问题的魔杖。 Android 培训文档的以下摘录清楚地表明了这一点:
[The] ability to request a large heap is intended only for a small set of apps that can justify the need to consume more RAM (such as a large photo editing app). Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained. - (source)
我还应该补充一点,Google 不会 拒绝使用您的应用。
简答
不,如果您需要它这不是一个坏习惯,因为它就在那里。
长答案
官方文档说明
Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application.
It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results.
Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.
一些开发人员使用它来避免 OOM 异常,所以如果您使用它只是为了避免一些 OOM 这是一个非常非常糟糕的做法。
Never request a large heap simply because you've run out of memory and you need a quick fix. You should use it only when you know exactly where all your memory is being allocated and why it must be retained
如果你确实需要更多space使用它是可以的,你可以使用getMemoryClass()
to check the heap and getLargeMemoryClass()
大堆。
但是,如果您可以避免使用 largeHeap,那将是最好的方法,正如官方文档所述:
Yet, even when you're confident your app can justify the large heap, you should avoid requesting it to whatever extent possible. Using the extra memory will increasingly be to the detriment of the overall user experience because garbage collection will take longer and system performance may be slower when task switching or performing other common operations.
Additionally, the large heap size is not the same on all devices and may be exactly the same as the regular heap size. So even if you do request the large heap size, you should call getMemoryClass() to check the regular heap size and strive to always stay below that limit.
我也建议你看看这里Managing Your App's Memory