构建 Android 时忽略 -j (jobs) 选项(默认为所有核心)

make -j (jobs) option ignored when building Android (defaulting to all cores)

我正在使用以下命令序列构建 Android-x86(nougat-x86 分支):

repo init -u git://git.osdn.net/gitroot/android-x86/manifest -b nougat-x86

repo sync --no-tags --no-clone-bundle

virtualenv2 venv

. venv/bin/activate

. build/envsetup.sh

lunch android_x86_64-userdebug

export ANDROID_JACK_VM_ARGS="-Xmx4g -XX:+TieredCompilation -Dfile.encoding=UTF-8"
export JACK_SERVER_VM_ARGUMENT=$ANDROID_JACK_VM_ARGS
export SERVER_NB_COMPILE=1
export USE_CCACHE=1

LANG=C LC_ALL=C make -j2 iso_img

请注意我如何在最后一行使用选项 -j2 指定两个 make 作业。

无论如何,构建实际上产生了 8 个进程 clang / clang++ / javac 以及其他任何进程。

这会导致构建失败:

GC overhead limit exceeded
Try increasing heap size with java option '-Xmx'
Warning: This may have produced partial or corrupted output.
ninja: build stopped: subcommand failed.
make: *** [ninja_wrapper] Error 1

make failed to build some targets


如何强制执行 -j2 选项以确保我的 CPU 和 RAM 没有用完?

原因:JACK服务器是邪恶的。

下面解决了out-of-ram问题:

./prebuilts/sdk/tools/jack-admin kill-server
rm -rf ~/.jack*    
export ANDROID_JACK_VM_ARGS="-Xmx4g -XX:+TieredCompilation -Dfile.encoding=UTF-8"
export SERVER_NB_COMPILE=2
export JACK_SERVER_VM_ARGUMENT=$ANDROID_JACK_VM_ARGS
./prebuilts/sdk/tools/jack-admin install-server prebuilts/sdk/tools/jack-launcher.jar prebuilts/sdk/tools/jack-server-4.*.ALPHA.jar
./prebuilts/sdk/tools/jack-admin start-server

非常感谢the FUCK JACK! project. Modified script with changes from issue #2 and PR 5


不是很明显的是,构建失败不是因为 MACHINE 运行 内存不足,而是因为 JACK 服务器内存不足。事实上,错误消息说...

Try increasing heap size with java option '-Xmx'

(强调我的)。

此位于 http://www.2net.co.uk/blog/jack-server.html 的网页解释...

(...) let’s look at the default heap size on two different machines. The default heap size scales according to the total amount of memory fitted, ignoring the swap space. On a 16 GiB machine, it is over 3 GiB:

$ java -XshowSettings 2>&1  | grep Heap
    Max. Heap Size (Estimated): 3.47G

But, on an 8 GiB machine, it is less than 2 GiB:

$ java -XshowSettings 2>&1  | grep Heap
    Max. Heap Size (Estimated): 1.71G

It turns out that jack-server needs about 3 GiB to build a typical AOSP Nougat target, and so it fails if the machine doesn’t have enough RAM. Increasing the swap space makes no difference.

好的。因此,使用上面的命令我能够将堆大小增加到 4GiB 并且编译没有错误。


现在...

如何强制执行 -j2 选项以确保我的 CPU 和 RAM 没有达到极限?

(https://android.googlesource.com/platform/prebuilts/sdk/+/master/tools/README-jack-server.md) 的页面说

config.properties file

It contains Jack server configuration properties. Modifications to those settings are taken into account after restarting the server. Description with default values follows:

jack.server.max-service=<number> Maximum number of simultaneous Jack tasks. Default is 4.

(...)

If your computer becomes unresponsive during compilation:

You can improve the situation by reducing the number of jack simultaneous compilations by editing your $HOME/.jack-server/config.properties and changing jack.server.max-service to a lower value and then restarting the server.


TL;DR:

jack.server.max-service=2$HOME/.jack-server/config.properties 中并重新启动 JACK 服务器。