Android Gradle: What is javaMaxHeapSize "4g"?
Asked Answered
F

4

51

In an android project, build.gradle file, I have been through this line

dexOptions{
    javaMaxHeapSize "4g"
}

I would like to know the exact purpose of this javaMaxHeapSize and what does that 4g means. What are other values I can give ?

Fritzsche answered 17/11, 2015 at 6:21 Comment(1)
'Is there any downside to setting this number real high?' is a question I haven't seen addressed. I have recently changed mine from 4g to 8g to future-proof my game app with no apparent ill effects.Somnambulate
A
36

As it mentioned in the answer above, it is just an option to specify the maximum memory allocation pool for a Java Virtual Machine (JVM) for dex operation. And it's the same, as to provide to java the -xmx argument. Due to it's source codes from here, it's setter look like:

if (theJavaMaxHeapSize.matches("\\d+[kKmMgGtT]?")) {
    javaMaxHeapSize = theJavaMaxHeapSize
} else {
    throw new IllegalArgumentException(
            "Invalid max heap size DexOption. See `man java` for valid -Xmx arguments.")
}

So, you can see, that the accepted value should match the \d+[kKmMgGtT]? pattern, and hence not, it even refers to the man java to get to know, how to set the -xmx. You can read the man page here. And it says, that this flag:

Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is chosen at runtime based on system configuration.

In your example, 4g is 4 Gigabytes and this is a maximum heap size for dex operation.

Afterlife answered 17/11, 2015 at 7:50 Comment(0)
R
9

This is an undocumented option to increase the heap size for dex operation: https://groups.google.com/d/msg/adt-dev/P_TLBTyFWVY/4TPJ2YY6khUJ

Roshelle answered 17/11, 2015 at 6:27 Comment(0)
A
0

its the maximum Ram that gradle can use while creating the build(apk file). This can be changed as per the project structure and size, its always safe to have atleast "4g" Ram allowed, however sometimes we also need to raise it as like if we want to use minifyEnabled true or shrinkResources true flag in the gradle for release build

Astound answered 6/7, 2019 at 11:23 Comment(0)
H
-3
dexOptions{
    javaMaxHeapSize "4g"
}

after writing this in build.gradle go to gradle.propertiesand enable this line:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Horripilation answered 27/5, 2017 at 7:14 Comment(2)
Xmx should be set to at least javaMaxHeapSize. In your example it is half the size.Taker
@Taker he is right. it should be more than 4096m.Gardiner

© 2022 - 2024 — McMap. All rights reserved.