Before program execution enters the function to declare the array, the memory used is 384kb. When it exits the function the memory used is 1164kb. |
Maybe I am incorrect in assuming that Task Manager does not include stack space in its memory usage statistics. It's possible it does.
It's also possible that your stack is growing dynamically because you're putting an awful lot of data on it. 800K is pretty big (not counting what's already on the stack from program initialization).
From a look at the default settings in VS2012:
Default Stack Commit Size = 4 KB
Default Stack Reserve Size = 1 MB
This suggests to me that you'd start with 1 MB stack space allocated, and it would grow (if possible) in 4 KB steps beyond that. Either that or it grows in 4K steps up to 1 MB.... but that doesn't make much sense.
http://msdn.microsoft.com/en-us/library/8cxs58a6.aspx
At any rate... once stack space is reserved, it's reserved. IE: it's allocated. Stack space is not heap space, and it doesn't get allocated/released the same way heap space does.
If task manager is not suitable for this is there another program which can show me the details? |
I'm not familiar with one, as I've never had to do this kind of fine analysis.
The reason I believe you should use dynamic memory allocation is for cases when you do not now how much memory you intened to assign until runtime. |
That's one reason.
Another reason is to put memory on the heap so it doesn't suck up the limited stack space you have.
however from what I can "see" the program uses a higher average amount of memory over the lifetime of the program when I assign memory from the stack since it does not seem to release the memory no matter how long I wait after the array geos out of scope. |
This is Task Manager deceiving you. Or rather, it's illustrating memory usage in an overly simplified format.
If your program has 1 MB stack space reserved... then you can use 5 bytes of that space or you can use the full 1 MB and your program will not have to allocate any more memory.
Stack memory doesn't get released when it goes out of scope because it's on the stack.
EDIT:
Simplified stack memory overview.
Let's say your program has a 1000 byte stack space (which is unrealistically small). So when the program starts, the OS sets aside 1000 bytes of contiguous memory for it to use as its stack.
You can think of this 1000 bytes as being 'new'd or dynamically allocated if it helps.
Now let's look at a simple main:
1 2 3 4 5 6 7
|
int main()
{
int a;
int b;
someOtherFunc();
}
|
main has 2 vars on the stack. Each 4 bytes (we'll assume 1 int = 4 bytes). So the stack space looks like this:
1 2 3
|
[4 bytes] 'a' in main
[4 bytes] 'b' in main
[992 bytes] <unused>
|
When someOtherFunc is called:
1 2 3 4
|
void someOtherFunc()
{
int array[4];
}
|
So upon entry to this function... more stack space gets used:
1 2 3 4
|
[4 bytes] 'a' in main
[4 bytes] 'b' in main
[16 bytes] 'array' in someOtherFunc
[976 bytes] <unused>
|
When someOtherFunc exits... 'array' goes out of scope and dies... but the stack is still 1000 bytes:
1 2 3
|
[4 bytes] 'a' in main
[4 bytes] 'b' in main
[992 bytes] <unused>
|
The stack typically does not grow/shrink but rather stays the same size throughout the life of the program (special circumstances may change this, but this is basically how it works).
So program memory usage generally does not fluctuate with how much stack space is used... because the stack space is all allocated up front.