Minimizing Stack Usage

Like most programming languages, Visual Basic uses a stack to preserve your local variables and arguments during procedure calls. The maximum size of the stack is fixed and cannot be changed. Visual Basic uses some of this space for purposes such as storing intermediate values when evaluating expressions; this makes the actual size of the stack available to your code smaller.

On both Windows 95 and Windows NT platforms, stack size grows dynamically up to a limit of about 1 MB as needed. For this reason, running out of stack space occurs much less frequently than in previous versions of Windows. The remaining stack space should be adequate for most purposes. However, if your procedures call procedures that call other procedures, and you declare many large local variables and arguments in each procedure, you may run out of stack space. The following sections describe techniques for minimizing the amount of space your application uses on the stack.

Look for Unbounded Recursion

One of the most common reasons for running out of stack space is unbounded recursion—a procedure that calls itself repeatedly until the stack is exhausted. In Visual Basic, this is often the result of a cascading event (such as changing the Text property in the Change event for a text box). When you encounter an “Out of stack space” error, look for cascading events first before you try to reduce the amount of stack space your code uses. To examine the sequence of procedure calls that led to the “Out of stack space” error, use the Calls dialog box (View menu).

See Also   For more information on the Calls dialog box, see “Tracing Nested Procedures” in Chapter 7, “Debugging Visual Basic Code.”

Declare String Variables Differently

Every nonstatic local string variable uses some space on the stack. How you declare a string variable determines how much stack memory Visual Basic must allocate for it. The following table summarizes how much memory is allocated for different types of nonstatic string variables, and how that allocation is distributed between stack memory and the remaining large pool of memory referred to as heap memory.

String variable type Stack usage Heap usage
Local, fixed-length strings 64 characters or less 2 bytes for each character in the string None
Local, fixed-length strings 65 characters or more 4-byte pointer to the variable in heap memory 2 bytes for each character in the string
Local, variable-length strings 4-byte pointer to the variable in heap memory Variable, depending on the length of the string

Note   How memory is allocated for fixed-length string variables may change in later versions of Microsoft Access. As a result, you may need to reevaluate decisions made on basis of the information in this table.

If you are using a large number of fixed-length strings 64 characters in length or less, you can reduce stack usage by replacing them with local, variable-length strings or by making the fixed-length strings static.

See Also   For information on the consequences of declaring static variables, see “Declare Procedures or Local Variables with the Static Keyword” later in this section.

Another way to reduce stack memory usage is to manage string variables by using an array. Visual Basic uses only a 4-byte pointer on the stack for each array.

If you continue to run out of stack space after trying different methods of declaring local variables, consider using module-level variables instead of passing local values as arguments to procedures. However, keep in mind that this makes your code more difficult to maintain, modify, and debug.

Avoid Variant Data Types in Arguments and Local Variables

The Variant data type is larger than any of the other data types, so it uses more of the stack when you use it for local variables and arguments.

Declare Procedures or Local Variables with the Static Keyword

If you aren’t calling a procedure recursively and continue to run out of stack space, consider declaring the procedure with the Static keyword. This places all of the procedure’s local variables in regular memory rather than on the stack. However, this technique has its disadvantages. Because static variables are never discarded, they use memory in your application as long as it is running. Also, because static variables are not reset to their initial values each time the procedure is called, the code in the procedure must be carefully written to handle this. If you don’t want to declare the entire procedure with the Static keyword, consider using the Static keyword to declare some of the local variables.