Running Bazel with Limited RAM
In certain situations, you may want Bazel to use minimal memory. You can set the maximum heap via the startup flag--host_jvm_args,
like --host_jvm_args=-Xmx2g.
Trade incremental build speeds for memory
If your builds are too big, Bazel may throw anOutOfMemoryError (OOM) when
it doesn’t have enough memory. You can make Bazel use less memory, at the cost
of slower incremental builds, by passing the following command flags:
--discard_analysis_cache,
--nokeep_state_after_build,
and
--notrack_incremental_state.
These flags will minimize the memory that Bazel uses in a build, at the cost of
making future builds slower than a standard incremental build would be.
You can also pass any one of these flags individually:
--discard_analysis_cachewill reduce the memory used during execution (not analysis). Incremental builds will not have to redo package loading, but will have to redo analysis and execution (although the on-disk action cache can prevent most re-execution).--notrack_incremental_statewill not store any edges in Bazel’s internal dependency graph, so that it is unusable for incremental builds. The next build will discard that data, but it is preserved until then, for internal debugging, unless--nokeep_state_after_buildis specified.--nokeep_state_after_buildwill discard all data after the build, so that incremental builds have to build from scratch (except for the on-disk action cache). Alone, it does not affect the high-water mark of the current build.
Trade build flexibility for memory with Skyfocus (Experimental)
If you want to make Bazel use less memory and retain incremental build speeds, you can tell Bazel the working set of files that you will be modifying, and Bazel will only keep state needed to correctly incrementally rebuild changes to those files. This feature is called Skyfocus. To use Skyfocus, pass the--experimental_enable_skyfocus flag:
//pkg will be kept in the working set, and
changes to files outside of the working set will be disallowed, until you issue
bazel clean or restart the Bazel server.
If you want to specify an exact set of files or directories, use the
--experimental_working_set flag, like so:
--experimental_skyfocus_dump_post_gc_stats to show the
memory reduction amount:
Putting it altogether, you should see something like this:
dir1, dir2, and
dir3/subdir will retain their fast speeds, with the tradeoff that Bazel cannot
rebuild changed files outside of these directories.