Skip to content

Computed C++ linker RAM is way off reality #17368

Open

Description

@jmmv

Description of the bug:

In our C++ builds, we have observed Bazel eagerly running more than a few link actions at once. Each of these linker executions can easily consume 8GB, so combining just a handful together can easily exceed RAM limits. Using -c dbg bumps the memory consumption to 20GB per linker, which is ridiculous. We are currently using ld.

The puzzling thing was that even using something like --local_ram_resources to limit Bazel to a few GB, say 2GB, did not help at all. Bazel kept scheduling too many linkers at once. This made me think that the RAM computations for the link actions were not working correctly.

Upon looking I noticed commit 01c10e0, which mentions that the new limits were set based on data collected at Google. Thus I'm surprised that this new behavior didn't work well.

I ended up patching CppLinkAction.java to dump the resources computed in estimateResourceConsumptionLocal and found that all of our link actions are assumed to only need 50MBs, which is way off reality. I also noticed that our problematic actions only have about ~600 inputs.

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Come up with a link action that, with a small number of inputs, consumes multiple GBs. (This is what we observe but I have no idea at this point why the linker is behaving in this way.)

Craft a build file that contains multiple independent such actions.

Run the build and see Bazel trigger too many linkers at once, overwhelming the machine.

Which operating system are you running Bazel on?

Linux

What is the output of bazel info release?

release 6.0.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

There is #6477 which would help if we had it, but I would prefer to avoid crafting our own rules just to paper over a deficiency of existing native rules. A tag or other attribute on the existing rules would work better from a usability perspective.

Any other information, logs, or outputs that you want to share?

I do not think the C++ link rule can provide a local resource set that works in all cases, if only because there are different linkers and they have different memory needs, and because even the same linker can exhibit vastly different behavior depending on compilation modes (see 8GB vs. 20GB when enabling dbg mode).

There should be a way for downstream users to configure the resource sets of the C++ rules without having to modify the Bazel source code. I'd suggest supporting cpu:X and ram:X tags or similar that override whatever the rules compute; this would have been sufficient for us to workaround the problem.

Activity

  1. added
    untriaged
    team-Local-Exec
    on Feb 1, 2023
  2. removed
    untriaged
    on Feb 7, 2023
  3. self-assigned this
    on Feb 7, 2023
  4. added
    P2
    on Feb 7, 2023
  5. lm1baker commented on Mar 10, 2023

    @lm1baker

    Can report the same problem, bazel will happily start 20+ linker threads on a CI node with 108GB Ram, with each thread consuming 6GB. Setting e.g. --local-ram-resources=24576MB (about 25% of system ram) does not even help with this.
    Manually tuning with --jobs (informed by OOMkiller logs) is the only workaround, though this does slow compilation and other tasks.

  6. jmmv commented on May 22, 2023

    @jmmv
    ContributorAuthor

    Another thought that came to mind: maybe it's the toolchain definition what should be able to supply the memory model for the linker and compiler. I'm not sure if this would be feasible, but maybe there could be a user-supplied function that takes the compilation mode and details about the inputs (like the total count) as parameters and returns the computed expected memory. Expressing arbitrary formulas via flags is not a great idea, and encoding limits via tags also doesn't scale.

  7. larsrc-google commented on Jun 6, 2023

    @larsrc-google
    Contributor

    For starlark-defined rules, you can give such a function indeed. But the CppLink function is based on the internal graph, which tends to have a huge amount of relatively small targets, where your case seems to have relatively few but large inputs. So we'd want to look at the size of the inputs, but that would be a more expensive computation.

  8. kjteske commented on Jul 7, 2023

    @kjteske
    Contributor

    We've got a similar use case, but with C++ compilations in addition to links:

    Scattered in with thousands of normal-sized compiles, we have a number of large compiles (or links) where gcc takes >2GB of RAM. If enough of those get scheduled to run in parallel, we can OOM pretty easily even on beefy machines.

    We generally know which of these compiles are huge, and there aren't that many of them, so we could tag these particular actions in BUILD files or our rules, and deal with the maintenance burden of keeping that up to date.

    --experimental_action_resource_set gives Bazel a hook we could use... but I don't see a way to access that through the native cc rules, or even cc_common. If cc_common.compile() took an optional resource_set parameter that was forwarded to ctx.actions, I think that would give us the flexibility we're looking for.

  9. larsrc-google commented on Jul 10, 2023

    @larsrc-google
    Contributor

    Here's what I would suggest trying: In CppLinkAction.createSpawn(), pass the inputs instead of merely the count of inputs into estimateResourceConsumptionLocal. In there, compute the size of the inputs and use that as your estimate. Then see how close you get to reality. This will take longer to estimate due to looking at all the inputs, so benchmarking it would be crucial, but I think for linker-style actions it may be necessary.

  10. jmmv commented on Jul 17, 2023

    @jmmv
    ContributorAuthor

    That's a good idea. I modified CppLinkAction to capture the size of the inputs along with the counts, made it dump metrics to the log, and graphed the results. The size does seem to track consumption much better, at least for our builds with lld on Linux. This applies to fastbuild/dbg/opt with and without dynamic linking, as well as amd64 and arm64. Roughly, I'm observing that memory consumption is ~2x the size of the inputs.

    I've left my changes in a custom branch that you can compare against 6.2.1 here (https://github.com/bazelbuild/bazel/compare/6.2.1...Snowflake-Labs:bazel:linker-memory-estimation?expand=1) but I have not created a PR yet because the branch mixes instrumentation with memory tweaks. If you find value in the instrumentation portion alone (computing input sizes and logging them), I can split that into its own PR for inclusion. Then, you could rerun whichever analysis you previously did with your data and see what comes up. (For example, I won't be able to do this for neither macOS nor Windows.)

  11. larsrc-google commented on Jul 18, 2023

    @larsrc-google
    Contributor

    Nice that your estimates are stable across various options variations. Did you do any benchmarks with your resulting memory estimations?

  12. jmmv commented on Aug 7, 2023

    @jmmv
    ContributorAuthor

    @larsrc-google No, I did not. I do not think our build or link actions are sufficiently large to gather any meaningful performance data of this change (other than seeing no OOMs).

    What exactly would you like to see measured? A microbenchmark could help quantify the cost, but then, a microbenchmark wouldn't be realistic. In a real build, link actions are infrequent so unless their cost is massively different, they won't impact end-to-end build times. And in any case, this change only impacts local link actions, so if the majority of link actions happen remotely (as is also commonly the case for sufficiently-large builds), then this change would have no impact...

  13. larsrc-google commented on Aug 7, 2023

    @larsrc-google
    Contributor

    I would suggest using the JSON profile data on realistic builds run local-only (or local-for-link-only). That would allow you to measure realistic time differences between different estimates.

    Doing this with compile actions like @kjteske talks about would be easier, I guess, as you would be able to see whether the machine gets overloaded/too few actions get scheduled.

    It's also interesting to know if getting the file sizes takes a non-trivial amount of time, I hope those values are in memory, but stat'ing all the inputs all the time might be problematic.

    One nice thing about your change is that it defers flattening the nestedset, so the estimation implementation has more say in whether to do that.

  14. 20 remaining items

  15. removed
    untriaged
    on Dec 10, 2024
  16. kirschem-fernride-legacy commented on May 21, 2025

    @kirschem-fernride-legacy

    @wilwell Nice! But I think this is currently only accounting for cpu. Would be nice to also have this for RAM, which is more critical in my opinion due to OOMs, which might kill the whole build. CPU is "only" slowing down.
    Is it planned to also add scheduling based on the current RAM usage?

  17. wilwell commented on May 21, 2025

    @wilwell

    @kirschem-fernride I've recently left Bazel, so I am not developing this repo anymore.

    cc @meisterT @bigelephant29

  18. meisterT commented on May 21, 2025

    @meisterT
    Member

    @kirschem-fernride can you please file a feature request for this?

  19. pzembrod commented on Aug 1, 2025

    @pzembrod
    Contributor

    Also relevant: #25770 (Ability to override RAM estimate for cppcompile action)

  20. kirschem-fernride-legacy commented on Aug 19, 2025

    @kirschem-fernride-legacy
  21. keith commented on Apr 13, 2026

    @keith
    Member

    this change #29284 would at least allow overriding things per target / globally, but still only statically, so it couldn't scale with the given input set

  22. added a commit that references this issue on Apr 16, 2026
  23. added a commit that references this issue on May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2team-Local-Exectype: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions