Will the array be allocated in the next procedure on the stack?
Yes, provided that the local variable is not captured by the anonymous method. Such local variables are on the heap.
What is the biggest element that can go on the stack?
It depends on how big the stack is, how much has already been used on the stack, and how many stacks are used by calls to the function itself. A stack is a fixed size defined when creating a stream. The stack overflows if it exceeds this size. On Windows, at least the default stack size is 1 MB, so I did not expect you to run into problems with a 1 KB array, as you can see here.
Is there any difference in speed between variable access on the stack and heap?
By and large, no, but again it depends. Variables in the stack are more likely to be available more often and are probably easier to cache. But for an object with a decent size, such as the 1KB array that we see here, I would not expect that there would be any difference in access time. As for the underlying memory architecture, there is no difference between the stack and the heap, it's just memory.
Now that the difference in performance is distribution. Heap allocation is more expensive than stack allocation. And especially if you have a multi-threaded application, heap distribution can be a bottleneck. In particular, the default Delphi memory manager does not scale well with multithreaded usage.
source share