2

I know this is quite strange to ask for a language that is far from a low-level language such as C and C++, but it caught my attention that for example if I do this:

Dim tempArray(0 To 2) As Integer

Obviously I will be using the stack memory for a lot of reasons, but lets stick with just the idea that this is a predefined sized array, so it is static, and not dynamic. But let`s say I do this:

Dim tempArray() As Integer
Dim arraySize As int
Dim inputData As String

inputData = InputBox("Please give me the size of your array")

arraySize = Val(inputData)

Redim tempArray(0 To arraySize)

And suppose the user does give us a valid number, you can all agree that this dynamic allocation because it is done at runtime, so, when I am programming in VBA I will have to keep in mind that using redim is slower because not only I am doing a reallocation, but also because I am going to use the heap, correct? Again I know if I am so concerned to the point of knowing what kind of memory is being used, I should be using other kind of language, but it is just that I really want to learn everything about the Redim proccess and I could not find much insight about my question, so any answer would be very helpful.

1
  • 1
    AFAIK all arrays are created on the heap, but the pointer to it is stored on the stack. Your example isn't really doing a reallocation either as the initial declaration Dim tempArray() As Integer hasn't allocated any space. Redim Preserve is the real time killer Commented May 29, 2024 at 8:24

1 Answer 1

2

When you use ReDim in VBA (Visual Basic for Applications), you are indeed performing a dynamic memory allocation at runtime, which involves reallocating memory.
Here's a detailed explanation of the process and its implications:

Static vs Dynamic Allocation

1- Static Allocation:

Dim tempArray(0 To 2) As Integer

Memory for tempArray is allocated on the stack at compile time.
The size of the array is fixed and determined at compile time.
Stack allocation is typically faster and more efficient since it involves a simple pointer adjustment.

2- Dynamic Allocation:

Dim tempArray() As Integer
Dim arraySize As Integer
Dim inputData As String

inputData = InputBox("Please give me the size of your array")
arraySize = Val(inputData)
ReDim tempArray(0 To arraySize)  

Memory for tempArray is allocated on the heap at runtime.
The size of the array is determined at runtime based on user input.
Heap allocation is slower compared to stack allocation because it involves more complex memory management operations, such as finding a suitable block of memory and potentially moving existing data.

The ReDim Process

When you use ReDim in VBA:

1- Memory Allocation:

VBA allocates a new block of memory on the heap to accommodate the new size of the array.
The old memory block is deallocated (freed), which involves updating internal memory management structures to mark it as available.

2- Data Movement (if ReDim Preserve is used):
If you use ReDim Preserve, VBA needs to copy the existing elements of the array to the newly allocated memory block.
This adds overhead as it involves iterating through the existing elements and copying them one by one to the new memory location.

Performance Considerations:

Speed:

Dynamic allocation (ReDim) is generally slower than static allocation due to the overhead of heap memory management and, potentially, data movement.
The difference in speed may be negligible for small arrays but can become significant for larger arrays or in performance-critical applications.

Memory Usage:

Heap allocation allows for more flexible memory usage, enabling dynamic resizing of arrays based on runtime requirements.
However, frequent resizing with ReDim can lead to memory fragmentation and increased memory usage.

Best Practices:

Minimize the Use of ReDim:
Try to minimize the number of ReDim operations, especially inside performance-critical loops.
If you need to resize an array multiple times, consider resizing in larger increments to reduce the number of allocations.

Avoid Unnecessary ReDim Preserve:
Use ReDim Preserve only when you need to retain existing data.
If the data does not need to be preserved, a simple ReDim without Preserve can be more efficient.

Conclusion:

In summary, using ReDim in VBA involves dynamic memory allocation on the heap, which is slower compared to static allocation on the stack due to the overhead of memory management and potential data movement.
While VBA abstracts much of this complexity, understanding these underlying processes can help you write more efficient code, especially when working with large datasets or in performance-critical scenarios.

Sign up to request clarification or add additional context in comments.

1 Comment

I see, but even in languages such as Java I can only store references into the stack (see <stackoverflow.com/questions/10953806/…), so when you do 'Dim tempArray(0 To 2) As Integer' you are putting the actual array inside the stack and not a reference to the heap like Java? Does that mean VBA handles stack memory better than Java? (I did not imagine others high-level languages handled memory management that way until I saw @CHill60 's comment and now I am confused).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.