Skip to content

Prevent ninja starting new job when physical memory become low.#2605

Open
Marc-Pierre-Barbier wants to merge 9 commits intoninja-build:masterfrom
Marc-Pierre-Barbier:master
Open

Prevent ninja starting new job when physical memory become low.#2605
Marc-Pierre-Barbier wants to merge 9 commits intoninja-build:masterfrom
Marc-Pierre-Barbier:master

Conversation

@Marc-Pierre-Barbier
Copy link

@Marc-Pierre-Barbier Marc-Pierre-Barbier commented Jun 5, 2025

Inspired by #1571

this adds a new option: --keep-free-memory AMOUNT
and the amount can be specified with G/M/m/K/k/B units (defaulting to byte)

If the amount specified is greater than the available memory then we stop starting new build jobs.

Most importantly, this doesn't solve all memory issues you will need to swap if you don't put a great enough value, too big of a value will result in having very few core being used. This is purely here to stop the problem from getting worse and not fix it.

@Marc-Pierre-Barbier Marc-Pierre-Barbier marked this pull request as draft June 11, 2025 07:07
@Marc-Pierre-Barbier
Copy link
Author

Marked as draft until I can test on macOS so I can use sysconf.

if (config_.desired_free_ram > 0) {
long memory_capacity = GetFreeMemory() / config_.desired_free_ram;
if (memory_capacity < capacity)
capacity = memory_capacity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just do an early return instead of redefining capacity

return memory_capacity;

Copy link
Author

@Marc-Pierre-Barbier Marc-Pierre-Barbier Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this function, there is a check later on that does the following:

if (capacity == 0 && subprocs_.running_.empty())
    // Ensure that we make progress.
    capacity = 1;

That's why there is no other usage of early returns in this function, as this mechanism prevent lockups even if we have to ignore our ram condition.

Copy link

@kevin-robb kevin-robb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random person here, I've been using a build of this branch (built via ./configure.py --bootstrap) on Ubuntu 22 for a large CMake project, and this has worked wonderfully. Using --keep-free-memory 4G does the trick, making ninja usable and still faster than default Unix make. Thanks for implementing this! I'd love to see this get merged into the release version of ninja

@notable-equivalent
Copy link

This is a very useful feature. What further work needs to be done to get this merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants