在Android中使用largeheap是一个很好的做法吗?

前端之家收集整理的这篇文章主要介绍了在Android中使用largeheap是一个很好的做法吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在NDK开发.它挂在Galaxy S3中.为了测试我在Manifest中放了 android:largeheap =“true”.然后没有悬而未决的问题.
使用largeHeap =“true”是一个好习惯吗?

由于此标记,Google是否有机会拒绝我的版本
如何在不使用largeheap =“true”的情况下阻止我的应用挂起?

解决方法

简答

不,如果你需要它,这不是一个糟糕的协议,因为它就在那里.

答案很长

官方文件

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

如果你真的需要更多空间可以使用它,你可以使用getMemoryClass()来检查堆和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

原文链接:https://www.f2er.com/android/317683.html

猜你在找的Android相关文章