4

This should fill my memory every second with ca 100 MB. I track the memoryusage with gnome-systemmonitor and htop. But somehow it does not. Why?

#include "unistd.h"
#include <iostream>

int main(int argc, char *argv[])
{
    while (true){
        std::cout << "New one" << std::endl;
        new char[100000000];
        sleep(1);
    }
    return 0;
}

To run:

g++ -std=c++11 -O0  main.cpp; ./a.out  
ManuelSchneid3r
  • 15,850
  • 12
  • 65
  • 103
  • 2
    Lazy allocation, most likely. Try writing something to the allocated array. – Paul R Apr 29 '15 at 12:59
  • 1
    I originally thought it was related to [this](http://stackoverflow.com/q/25668420/1708801) but it seems even with `5.1` gcc does not do the same optimization with new that clang does and [godbolt shows gcc does call new](http://goo.gl/zXaTlQ) ... so Paul is likely correct ... clang on the other hand will optimize the call to new away at `-O1` and greater. – Shafik Yaghmour Apr 29 '15 at 13:01
  • You are aware the 100MB would be char[104857600]? I guess that the new command gets optimized out because it has no reason to be there... – Nidhoegger Apr 29 '15 at 13:01
  • @ShafikYaghmour Thats why I used -O0 – ManuelSchneid3r Apr 29 '15 at 13:15
  • @Nidhoegger yes, thats why I wrote circa. – ManuelSchneid3r Apr 29 '15 at 13:15
  • 2
    You are not allocating any memory. On any modern demand-paged virtual memory operating system like OSX, Linux, Windows you are only allocating address space. A 64-bit OS has as much as 256 Terabytes available. So you are probably just not waiting long enough, it takes 256E12 / 1E8 * sleep(1) = 256E4 msec = 2560 seconds. Almost 3/4 of an hour. Removing the sleep(1) call makes it a lot quicker :) – Hans Passant Apr 29 '15 at 13:20

2 Answers2

8

Because you're not using it, Linux does lazy allocation so it will not actually map any memory page until you use it.

If you put some code like:

char* test = new char[100000000];
test[0] = 'a';
test[4096] = 'b';
...

You should see it's actually consuming your system memory.

Mine
  • 4,123
  • 1
  • 25
  • 46
2

I have only seen clang optimize away calls to new and in general a compiler will not perform such aggressive optimizations when you are using -O0.

We can see from godbolt that gcc indeed does not optimize away the call to new in very similar code:

.L2:
 movl   $100000000, %edi
 call   operator new[](unsigned long)
 movl   $1, %edi
 call   sleep
 jmp    .L2

So most likely Paul is correct and lazy allocation is involved and therefore once you write to the allocated memory you will see it being used.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740