How to reduce memory consumption when building in Android Studio?

The problem is that on my laptop all 4Gb operatives and to buy will not work (the laptop is old).
So you have to shamanism with the settings gradle. Basically, I have free about 2.5 GB, swap do not want to use.

Tried to set the following build settings in Studio in dialog for Gradle-Android Compiler:
--parallel --max-workers=2 --no-rebuild-Dorg.gradle.jvmargs=-Xmx1536m

a74f4b83646a4afa8a409add6658c829.png

Also tried to set the settings in the file $HOME_DIRECTORY/.gradle/gradle.properties:
org.gradle.parallel=false
org.gradle.daemon=true
org.gradle.jvmargs=-Xms1024m -Xmx1024m -XX:MaxPermSize=512m
org.gradle.configureondemand=false
org.gradle.workers.max=1

Also tried to set the settings in the gradle file.properties in the draft:
org.gradle.jvmargs=-Xms1024m -Xmx1024m -XX:MaxPermSize=512m

Environment variables are also tried to install:
GRADLE_OPTS=-Xmx1024m

In any case, it did not help, the process was trying to eat all available system resources and even more.

Question:
where you should change the project settings?
July 8th 19 at 16:39
1 answer
July 8th 19 at 16:41
Solution
In General, if someone is interesting in my experience.
Cut memory the gradle daemon because it is not too much, and by default, eats well.
3a55f25ee566487dbdd6ca966c3442b1.png

A major role in the Assembly process packaging plays, though perhaps not properly named. Here, the memory consumption can be adjusted as follows:
android {
dexOptions {
maxProcessCount 1
javaMaxHeapSize '1200m'
}
}
Figure 1200Мб achieved empirically, will depend on the application.

I am also the resources idea cut, she was not offended:
-Xms128m
-Xmx128m
-XX:ReservedCodeCacheSize=32m

Find more questions by tags AndroidAndroid StudioGradle