Gradle 编译使我的电脑变慢

Gradle 编译使我的电脑变慢

新安装的 Ubuntu 14.04 LTS 的响应能力确实很差。

这是我的笔记本电脑的规格:

Quadcore Intel i7-4600U CPU @ 2.10GHz
12GB Ram
1TB Samsung EVO SSD

当 gradle 正在编译时,我的整个系统就会变得很慢,视频开始卡顿,浏览器滚动滞后……等等。

当 Chrome 中的另一个窗口中发生页面刷新时,也会发生同样的情况,刷新过程中另一个选项卡中的 HTML5 视频开始卡顿。

我怎样才能解决这个问题?

以下是一些日志:

$ vmstat -a 1 5 -http://paste.ubuntu.com/7493966/

$ iostat -x 1 5 -http://paste.ubuntu.com/7493970/

$ 顶部-b-http://paste.ubuntu.com/7493974/

答案1

我没有使用过 gradle。但据我所知,这是一个常见问题。

以下是链接:

非常有帮助的主题

CPU利用率问题

Gradle 性能

谷歌

您可以做的是更改进程调度。对于运行 IDE 或 gradle 的进程。

好的

nice 是 Unix 和类 Unix 操作系统(如 Linux)上的一个程序。它直接映射到同名的内核调用。nice 用于调用具有特定优先级的实用程序或 shell 脚本,从而为该进程提供比其他进程更多或更少的 CPU 时间。nice 值为 -20 表示最高优先级,19 或 20 表示最低优先级。进程的默认 nice 值从其父进程继承,通常为 0。

改变友善你可以使用renice命令

sudo renice <PID> <niceness> 

了解更多信息

对于构建时间,您可以将精细度设置为 15-20。构建后将其更改为默认值,通常为 0。

CPU限制

安装cpulimitsudo 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.

要将名为 firefox 的进程的 CPU 使用率限制为 30%,请输入:

# cpulimit -e firefox -l 30

要使用进程的 PID 将该进程的 CPU 使用率限制为 30%,请输入:

# cpulimit -p 1313 -l 30

cpulimit 至少应该在与受控进程相同的用户下运行。但是,如果您以 root 身份运行 cpulimit,则效果会更好,这样可以获得更高的优先级和更精确的控制。

如果您的机器有一个处理器,您可以将百分比限制在 0% 到 100% 之间,这意味着如果您设置了 50%,您的进程每秒使用的 CPU 时间就不能超过 500 毫秒。但是如果您的机器有四个处理器,百分比可能会从 0% 到 400% 不等,因此将限制设置为 200% 意味着使用不超过可用功率的一半。无论如何,百分比与您运行 top 时看到的百分比相同。

Cpulimit 使用情况

如何限制 CPU 使用率

查特

您还可以更改进程调度程序或 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.

Ubuntu 中的大多数 if 进程都是 SCHED_OTHER

查找调度策略的优先级值

$ 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

设置 SCHED_IDLE 来处理

$ chrt -i -p 0 PID

或者你可以改变优先级 SCHED_OTHER

$ chrt -o -p 1 PID

如何

其他方式

你也可以尝试减少 gradle 的线程数量。你可以阅读在这里。据我所知它有这个选项:

./gradlew -PtaskThreads=2

您也可以尝试减少内存使用量:

GRADLE_OPTS=-Mmx512m

引自非常有帮助的主题

--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.

相关内容