简体   繁体   中英

Is there a way to display or to view the memory allocated or usage of an array in Excel-VBA?

I want to see the memory usage of a specific array in my code. It seems that this Global array causes lots of usage in my memory. It also causes our code to have Run Time Error. The RTE problem was solved when we erase that specific array. On the other hand, we want to monitor the memory usage of this array for documentation.

We try to monitor the memory, but it is for the whole application, by using task manager.

I expect to get or display the allocated memory for that specific array for monitoring.

Sample Simplified Code:

Global Declaration:

Public array_double(1 to 12) as New Double

Inside Local Function

For s = 1 To 8
    For i = 1 To 12
            array_double(i) = 0#
    Next i

    For r = 1 To 64
        For i = 1 To 12
                array_double(i) = array_double(i) + some_value_double
        Next i
    Next r     
Next s

What is the array storing - objects, ordinal types or strings? I think the following question and possibly answers might be of help: VBA Equivalent of Sizeof()?

This is the code I test with in Excel 2003:

Public array_double(1 To 12) As Double

Sub test()
  For s = 1 To 65535
    For i = 1 To 12
      array_double(i) = 0#
    Next i

    For r = 1 To 64
      For i = 1 To 12
        array_double(i) = array_double(i) + 1
      Next i
    Next r
  Next s
  Debug.Print s
End Sub

After several runs my Mem Usage column in task manager for the Excel.exe process stays at 24MB.

My assumption is the leak the OP is detecting is somewhere else not with the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM