When an application is loaded into memory additional memory is reserved by the operating system for that application: the memory needed to hold the application’s data. Very simplified it looks like this in memory:
.........[application].........[stack][heap].........
The stack and the heap is where the application store its data. Each application has its own stack and heap reserved by the operating system.
Stack: If the size needed for a variable is known at compile time, and the size of such variable does not exceed the size of the registers in the CPU, meaning 32-bit or 64-bit, the variable is stored on the stack. The memory address for such a variable can be calculated at compile time and is therefore hard-coded. Very fast. Used for scalars, pointers, and most arrays of scalars and pointers.
Heap: Class instances, the Objects in Xojo terms, go into the heap. The memory needed for an object usually exceeds the 32 or 64 bits size limit as the memory needed is the sum of all memory needed for all properties of a class. So the memory cannot be reserved at compile time, it is allocated at runtime. This is done by Xojo’s New operator. All these objects need two places of storage: a pointer on the stack holding the memory address of the object data stored in the heap. All this is slow compared to stack operations.
So when the compiler detects a line like this:
Dim i As Integer = 2
… the compiler knows he must reserve 4 (on 32-bit PCs) or 8 bytes (on 64-bit PCs). They are reserved on the stack. An example:
Dim i As Integer = 2
Dim i As Integer = 7
Dim i As Integer = 4
..........[Start of Stack|2|7|4|0|0|....|0|0|End of Stack]..........
Now when the compiler comes to a line like this:
Dim obj As MyClass = New MyClass() // or its shorter version:
Dim obj As New MyClass()
… the compiler knows he must deal with an object. The compiler reserves 4 or 8 bytes for a pointer on the stack. This pointer will hold the memory address of the instance data in the heap, which uses memory allocated by New. An example:
Dim i As Integer = 2
Dim i As Integer = 7
Dim object1 As New MyClass()
Dim object2 As New MyClass()
Dim i As Integer = 4
..........[Start of Stack|2|7|pointer to object1 in heap|pointer to object2 in heap|4|0|....|0|0|End of Stack][Begin of Heap|....|object1|object2|....|0|0|End of Heap]..........