Why does GCC's ifstream >> double allocate so much memory?
It really doesn't. The number 59,302,487 shown by
valgrind is the sum of all allocations, and does not represent the actual memory consumption of the program.
It turns out that the libstdc++ implementation of the relevant
operator>> creates a temporary
std::string for scratch space, and reserves 32 bytes for it. This is then deallocated immediately after being used. See
num_get::do_get. With overhead, this perhaps actually allocates 56 bytes or so, which multiplied by about 1 million repetitions does mean, in a sense, that a total of 59 megabytes were allocated, and of course this is why that number scales linearly with the number of inputs. But it was the same 56 bytes being allocated and freed over and over again. This is perfectly innocent behavior by libstdc++ and isn't a leak or excessive memory consumption.
I didn't check the libc++ source, but a good bet would be that it uses scratch space on the stack instead of the heap.
As determined in comments, your real problem is that you are running this under AddressSanitizer, which delays the reuse of freed memory in order to help catch use-after-free errors. I have some thoughts about how to address that (no pun intended) and will post them on How do I exclude allocations in a tight loop from ASAN?