Tuesday, November 5, 2019

Heap vs. Stack for Delphi Developers

Heap vs. Stack for Delphi Developers Call the function DoStackOverflow once from your code and youll get the EStackOverflow error raised by Delphi with the message stack overflow. ​function DoStackOverflow : integer;begin result : 1 DoStackOverflow;end; What is this stack and why there is an overflow there using the code above? So, the DoStackOverflow function is recursively calling itself without an exit strategy it just keeps on spinning and never exits. A quick fix, you would do, is to clear the obvious bug you have, and ensure the function exists at some point (so your code can continue executing from where you have called the function). You move on, and you never look back, not caring about the bug/exception as it is now solved. Yet, the question remains: what is this stack and why is there an overflow? Memory in Your Delphi Applications When you start programming in Delphi, you might experience bug like the one above, you would solve it and move on. This one is related to memory allocation. Most of the time you would not care about memory allocation as long as you free what you create. As you gain more experience in Delphi, you start creating your own classes, instantiate them, care about memory management and alike. You will get to the point where you will read, in the Help, something like Local variables (declared within procedures and functions) reside in an applications stack. and also Classes are reference types, so they are not copied on assignment, they are passed by reference, and they are allocated on the heap. So, what is stack and what is heap? Stack vs. Heap Running your application on Windows, there are three areas in the memory where your application stores data: global memory, heap, and stack. Global variables (their values/data) are stored in the global memory. The memory for global variables is reserved by your application when the program starts and remains allocated until your program terminates. The memory for global variables is called data segment. Since global memory is only once allocated and freed at program termination, we do not care about it in this article. Stack and heap are where dynamic memory allocation takes place: when you create a variable for a function, when you create an instance of a class when you send parameters to a function and use/pass its result value. What Is Stack? When you declare a variable inside a function, the memory required to hold the variable is allocated from the stack. You simply write var x: integer, use x in your function, and when the function exits, you do not care about memory allocation nor freeing. When the variable goes out of scope (code exits the function), the memory which was taken on the stack is freed. The stack memory is allocated dynamically using the LIFO (last in first out) approach. In Delphi programs, stack memory is used by Local routine (method, procedure, function) variables.Routine parameters and return types.Windows API function calls.Records (this is why you do not have to explicitly create an instance of a record type). You do not have to explicitly free the memory on the stack, as the memory is auto-magically allocated for you when you, for example, declare a local variable to a function. When the function exits (sometimes even before due to Delphi compiler optimization) the memory for the variable will be auto-magically freed. Stack memory size is, by default, large enough for your (as complex as they are) Delphi programs. The Maximum Stack Size and Minimum Stack Size values on the Linker options for your project specify default values in 99.99% you would not need to alter this. Think of a stack as a pile of memory blocks. When you declare/use a local variable, Delphi memory manager will pick the block from the top, use it, and when no longer needed it will be returned back to the stack. Having local variable memory used from the stack, local variables are not initialized when declared. Declare a variable var x: integer in some function and just try reading the value when you enter the function x will have some weird non-zero value. So, always initialize (or set value) to your local variables before you read their value. Due to LIFO, stack (memory allocation) operations are fast as only a few operations (push, pop) are required to manage a stack. What Is  Heap? A heap is a region of memory in which dynamically allocated memory is stored. When you create an instance of a class, the memory is allocated from the heap. In Delphi programs, heap memory is used by/when Creating an instance of a class.Creating and resizing dynamic arrays.Explicitly allocating memory using GetMem, FreeMem, New and Dispose().Using ANSI/wide/Unicode strings, variants, interfaces (managed automatically by Delphi). Heap memory has no nice layout where there would be some order is allocating blocks of memory. Heap looks like a can of marbles. Memory allocation from the heap is random, a block from here than a block from there. Thus, heap operations are a bit slower than those on the stack. When you ask for a new memory block (i.e. create an instance of a class), Delphi memory manager will handle this for you: youll get a new memory block or a used and discarded one. The heap consists of all virtual memory (RAM and disk space). Manually Allocating Memory Now that all about memory is clear, you can safely (in most cases) ignore the above and simply continue writing Delphi programs as you did yesterday. Of course, you should be aware of when and how to manually allocate/free memory. The EStackOverflow (from the beginning of the article) was raised because with each call to DoStackOverflow a new segment of memory has been used from the stack and stack has limitations. As simple as that.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.