I often crash my raspberry pi when compiling esphome sensors and stuff, and noticied that it might be caused by the number of compilation threads which is 4 by default.
Is there any configuration item in the esphome addon to set the number of threads to a lower value ?
This is uncalled for. @nickrout has provided you the first step towards a likely answer. The PlatformIO options allow you to control the compilation. Look at the link inside that section.
From memory, I checked and found that the number of threads is forced/equal to number of CPU Cores, that could be improved with some prarameter to specify the number of compliation threads (-j parameter)
There appears to be a command-line parameter to pio-j that you can use, however that’s not exposed via the platformio.ini and thus not available through platformio_options.
If you want to reduce the number of CPUs used (ie to do other things) but not reduce the number of jobs you can use taskset -c 0-3 esphome compile your.yaml (for example to only use the first 4 CPUs). HOWEVER this is counter-productive if you are trying to reduce RAM usage - you’ll at best have no impact.
As discussed increasing swap file is a way to not get outright crashes, at the cost of enormous slowdowns, but it doesn’t do what is needed reduce the load to a level that will make efficient use of the hardware in use.