Is using hardheap in Android a good practice?

I am developing at NDK . It freezes in the Galaxy S3 . For testing, I put android:largeheap = "true" in Manifest . Then there was no hanging problem. Is it helpful to use largeHeap="true" ?

Is there any chance that Google will reject my build because of this tag and how can I prevent my application from freezing without using largeHeap="true" ?

+7
android android-ndk
source share
2 answers

Short answer

No, if you need it , this is not a bad pactise, because there is one for that.

long answer

Official documents

Should your application processes be created using a large Dalvik heap. This applies to all processes created for expression.
This only applies to the first application loaded into process; if you use a common user ID to allow multiple applications to use the process, all of them must use this option or they will have unpredictable results.
Most applications should not need this and should instead focus on memory usage for better performance . Enabling this also does not guarantee a continuous increase in available memory, as some devices are limited by their total available memory.

Some developers use it to avoid the expropriation of OOM, so if you only use it to avoid using OOM , this is a very bad pactice.

Never request a large heap simply because you run out of memory and need a quick fix . You should use it only when you know exactly where all your memory is allocated and why it should be saved.


If you really need more free space to use it , you can use getMemoryClass() to check the heap and getLargeMemoryClass() big heap.

But if you can avoid using largeHeap, this would be the best way as the official documentation continues:

However, even if you are sure that your application can justify a large pile, you should avoid the request to some extent . Using additional memory will more likely disrupt the general user because garbage collection will take longer and system performance may be slower when switching tasks or performing other general operations.
In addition, the large heap size is not the same on all devices and may be exactly the same as the usual heap size . Therefore, even if you request a large heap size, you should call getMemoryClass () to check the size of the regular heap and strive to always stay below this limit.

I also suggest you look here Application memory management

+17
source share

Personally, I would say that this really does not belong to the category of β€œgood / bad practice” when used properly .

According to docs :

Most applications do not need this and should instead focus on their shared memory usage for better performance. Enabling this also does not guarantee a fixed increase in available memory, as some devices are limited by their total available memory.

If you have done everything to reduce memory usage and still need it, then it's a good idea to use it.

If your application hangs, you will need to directly address this: largeHeap not a magic wand that will cause problems to go to all devices. This point is explained in the following excerpt from Android Training docs:

[Ability] to request a large heap is intended only for a small set of applications that can justify the need to consume more RAM (for example, as a large photo editing application). Never request a large pile simply because you run out of memory and you need a quick fix - you should use it only when you know exactly where all your memory is and why it should be saved. - (source)

I must also add that Google will not reject your application for its use.

+6
source share

All Articles