Gradle compiling slows down my computer

Solution 1:

I do not work with gradle. But as I understand it is common problem.

Here is a links:

Very helpful topic

CPU utilization problem

Gradle performance

Google

What you can do is change process scheduling. For process that runs IDE or gradle.

nice

nice is a program found on Unix and Unix-like operating systems such as Linux. It directly maps to a kernel call of the same name. nice is used to invoke a utility or shell script with a particular priority, thus giving the process more or less CPU time than other processes. A niceness of −20 is the highest priority and 19 or 20 is the lowest priority. The default niceness for processes is inherited from its parent process, usually 0.

To change niceness you can use renice command

sudo renice <PID> <niceness> 

Read this for more information

For building time you can set niceness for 15-20. After build change it to default value, usually it is 0.

cpulimit

Install cpulimit. sudo apt-get install cpulimit

-p : Process PID.
-e : Process name.
-l : percentage of CPU allowed from 0 to 100.
-P: absolute path name of the executable program file.

To limit CPU usage of the process called firefox to 30%, enter:

# cpulimit -e firefox -l 30

To limit CPU usage of the process to 30% by using its PID, enter:

# cpulimit -p 1313 -l 30

cpulimit should run at least with the same user running the controlled process. But it is much better if you run cpulimit as root, in order to have a higher priority and a more precise control.

If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top.

Cpulimit usage

How to limit CPU usage

chrt

You also can change process scheduler or PID.

SCHED_FIFO
Scheduling policy designed for special time-critical applications. It uses the First In-First Out scheduling algorithm.

SCHED_BATCH
Scheduling policy designed for CPU-intensive tasks.

SCHED_IDLE
Scheduling policy intended for very low prioritized tasks.

SCHED_OTHER
Default Linux time-sharing scheduling policy used by the majority of processes.

SCHED_RR
Similar to SCHED_FIFO, but uses the Round Robin scheduling algorithm.

Most if process in Ubuntu are SCHED_OTHER

Find priority values for scheduling policy

$ chrt -m
SCHED_OTHER min/max priority    : 0/0
SCHED_FIFO min/max priority : 1/99
SCHED_RR min/max priority   : 1/99
SCHED_BATCH min/max priority    : 0/0
SCHED_IDLE min/max priority : 0/0

Set SCHED_IDLE to process

$ chrt -i -p 0 PID

or you can change priority SCHED_OTHER

$ chrt -o -p 1 PID

Howto

Another way

Also you can try to reduce number of Thread for gradle. You can read about it here. As I see it has this options:

./gradlew -PtaskThreads=2

Also you can try reduce memory usage:

GRADLE_OPTS=-Mmx512m

Quote from Very helpful topic

--parallel-threads only applies to project parallelization.

For android tasks that are running in parallel, we always create as many threads as possible. For slower machine (or with low ram) this is not great. We should allow control on that.

Possible though:
./gradlew assemble -PandroidThread=3

Studio would have to allow configuring and sending this (it should also let you configure --parallel-threads if it doesn't already).

Long term gradle will have a thread pool shared across all level of parallelization (multi-projects. inside a project, inside a task) so this will become obsolete but it would be good to do now.