int varA = 1;
int varB = 2;
int result = varA + varB;
Console.WriteLine(result);
Let’s assume the variable varA takes 1MB of memory, varB takes 1MB of memory and result takes 1MB of memory. Let’s also assume all these variables live on the stack.
Does it mean the program allocate 1MB + 1MB + 1MB = 3MB of memory at the beginning when I run the program and release 3MB of memory when I exit the program?
Or the program allocate 3MB of memory at the beginning when I run the program and release 2MB of memory when I sum the varA and varB because then I don’t need anymore the varA and varB?
2