user5688956
user5688956

Reputation:

Why is visual studio Diagnostic Tools showing soo much memory usage?

I wrote this piece of code:

#include "Colour.h"

int main(int argc, char** argv) {

    //sizeof(Colour) = 3
    Colour* data = new Colour[8 * 8];

    for (int y = 0; y < 8 * 8; y++)
    {
        data[y] = Colour::WHITE;
    }
}

In this example Colour is a struct that is three bytes large

Thus I would expect this program to allocate 8 * 8 * 3 = 192 bytes. But in the diagnostic tools panel its showing a usage of 889 kB ?

enter image description here

Whats odd is when I dig a little deeper, by creating a memory snapshot, it shows a heap size of 71kB. But when looking at the source it only shows one allocation of size 192 bytes

What is happening? Is it possible that it is allocating all of that memory for the stack frame?

Upvotes: 3

Views: 2148

Answers (1)

SingerOfTheFall
SingerOfTheFall

Reputation: 29966

Well your application doesn't really float in vacuum. When you launch it, the OS also creates a window for your application output and launches it, holding a handle to it somewhere. It allocates some memory for the stack (the exact amount can vary, and I think it is possible to change it yourself).

The process itself is essentially a container which is represented by a kernel process object. It also stores some data that is required to be able to operate it. All of the above requires memory, and I suppose that's why your (almost empty) process allocates so much.

Also if you make two snapshots, one in the beginning of the execution of your app (before the first line is executed), and another one after the first line is executed, you will see that that heap size increased about exactly by the amount of memory you allocated, and most of the memory has been allocated before your own code was hit:

enter image description here

Upvotes: 2

Related Questions