If I use make -j2 , it builds fine, but underuses the CPU:

If I use make -j4 , it builds quickly, but for some specific files with a large number of templates, it consumes a lot of memory, slowing down the entire system and the build process due to replacing to the hard drive:

How can I automatically limit the number of parallel tasks based on memory, for example:

so he builds the project at maximum speed, but slows down in some places to avoid getting on the memory wall?
Ideas:
- Use
-l and artificially adjust the average load if the memory is busy (average load grows naturally when the system is already in trouble). - Creates memory allocation system calls (for example, sbrk (2) or mmap (2)) or page errors that continue to hang the process until memory is restored by completed jobs instead of replacing other processes. Sorry, Deadlock-prone ...
parallel-processing memory build makefile
Vi.
source share