3

I have recently needed to perform calculations with rather large arrays in VBA Excel 2010. However, to my dismay, both my computers at work and home (one is a PC and the other a Mac (The PC has 8GB of RAM and the Mac has 4GB)), keep throwing the same Out of Memory error when I try running my program. This led me to running some tests to get to the bottom of the issue.

Sub Test1()
    Dim arr1() As Byte, arr2() As Long

    ReDim arr1(1 To 268000000)  ''These work
    ReDim arr2(1 To 268000000)
End Sub

Sub Test2()
    Dim arr3() As Byte, arr4() As Long

    ReDim arr3(1 To 269000000)  '' Both of these fail
    ReDim arr4(1 To 269000000)
End Sub

Now, according to MSDN, the index of each dimension is limited by (2 ^ 31) - 1. As you can see above, I'm nowhere near that threshold and eerily close to 2^28. According to some crude calculations the amount of memory of each array above are:

arr1:  268,000,000 * 1 = 268,000,000 bytes of memory
arr2:  268,000,000 * 8 = 2,144,000,000 bytes of memory
arr3:  269,000,000 * 1 = 269,000,000 bytes of memory
arr4:  269,000,000 * 8 = 2,152,000,000 bytes of memory

You will notice that arr3 is roughly 1/8th the size of arr2, yet when trying to Redim arr3, we get the out of memory error while Redim arr2 works perfectly fine. What's going on here??? It should be noted that this post, although similar, does not apply as I have plenty of memory on both machines, and is asking simply about memory and not how many elements one can put in an array.

7
  • Both arr2 and arr4 fail for me. When I step through the code, arr1 and arr3 both execute the ReDim without raising the error. Commented Oct 3, 2016 at 14:13
  • 1
    MSDN also notes that: "If you attempt to initialize an array that exceeds the amount of available RAM, the common language runtime throws an OutOfMemoryException exception." and further that: "It is not safe to make any assumptions regarding how an array is stored in memory." Commented Oct 3, 2016 at 14:16
  • For me, for both routines the second redim bombs (XL 2010, 32 bit, 16 Gb of Ram) Commented Oct 3, 2016 at 14:25
  • 1
    Are you using 32 or 64 bit version of Excel? That would make a difference. See: support.microsoft.com/en-ca/kb/3066990 for a discussion and some tips to reduce the memory usage of your WorkBook. Everything works for me, however I'm running Excel 2013 64 bit, with 16GB of memory. Commented Oct 3, 2016 at 14:27
  • 1
    When I try again with 64 bit Excel 2010, both routines work just fine. Commented Oct 3, 2016 at 14:31

0

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.