Search code examples
bazel

Decrease bazel memory usage


I'm using bazel on a computer with 4 GB RAM (to compile the tensorflow project). Bazel does however not take into account the amount of memory I have and spawns too many jobs causing my machine to swap and leading to a longer build time.

I already tried setting the ram_utilization_factor flag through the following lines in my ~/.bazelrc

build --ram_utilization_factor 30
test --ram_utilization_factor 30

but that did not help. How are these factors to be understood anyway? Should I just randomly try out some others?


Solution

  • Some other flags that might help:

    • --host_jvm_args can be used to set how much memory the JVM should use by setting -Xms and/or -Xmx, e.g., bazel --host_jvm_args=-Xmx4g --host_jvm_args=-Xms512m build //foo:bar (docs).
    • --local_resources in conjunction with the --ram_utilization_factor flag (docs).
    • --jobs=10 (or some other low number, it defaults to 200), e.g. bazel build --jobs=2 //foo:bar (docs).

    Note that --host_jvm_args is a startup option so it goes before the command (build) and --jobs is a "normal" build option so it goes after the command.