'Why does compiling a header file with constexpr array use so much memory?

I have a header file with an array of unsigned chars that holds some raw binary data:

#include <string>
#include <array>
extern __declspec(selectany) inline constexpr std::string_view bin_name = std::string_view("test.bin");
extern __declspec(selectany) inline constexpr int bin_size = 10812406;
extern __declspec(selectany) inline constexpr std::array<unsigned char, 10812406> bin_data = {...}

I have another header file that takes that array and writes it to a file:

#include "_.h"
#include <fstream>
void write_file(std::string const outputDir = ".") {
    std::ofstream file;
    file.open(outputDir + "/" + bin_name.data(), std::ios::out | std::ios::binary);
    file.write((const char*)& bin_data[0], bin_size);
    file.close();
}

And finally in main I just call the function to write the file:

#include "_.h"

int main()
{
    write_file();
}

Trying to compile the above in Visual Studio, or directly using cl (cl /Os /std:c++17 /Zc:externConstexpr main.cpp) results in the compiler using more than 12 GB of memory!

Compiler memory usage in Task Manager

The bin_data is only around 10 MB in size, so the >12GB allocation by the compiler seems an overkill. More importantly, the actual size of the binary data I wanted to use is around 50 MB, however as you might have guessed, compiling that exceeds my systems 32GB of ram, thus I can't do that.

So my questions are:

  • Why does the compiler need so much ram?
  • What can I do to reduce that?
  • Is this a bug?
  • Is this behavior unique to vc++, or does the same happen in gcc/clang?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source