【问题标题】:VBA memory size of Arrays and ArraylistArrays 和 Arraylist 的 VBA 内存大小
【发布时间】:2013-12-11 17:47:04
【问题描述】:

我已尝试加载 6.000.000 (6 mio) 个长度为 64 个字符的字符串,以便在 VBA 中对它们进行排序。 我注意到的是: 1.当我使用数组时,占用的内存大约是 916 MB 2. 当我使用 ArrayList 时,占用的内存达到 1.105 MB 它们对我来说都不合理,因为字符串大小约为 380 MB。 我做错了什么? 随着字符串数量的迅速增长,我很快就会面临“内存不足”的问题。 欢迎任何想法。

距离

【问题讨论】:

  • VBA 没有ArrayList - 你说的是 VB.NET 吗?
  • 我正在 Excel 中使用 VBA 进行开发。我使用的语句是:'Set AL = CreateObject("System.Collections.ArrayList")' 它是 'Dim AL As Object' 声明的。

标签: arrays vba memory arraylist


【解决方案1】:

大部分问题在于 VBA 本身使用 BSTRs,这是 Unicode 字符串。我假设您对 ~380 mb 的计算是基于 600 万 * 64 个字符 @ 每个 1 字节。实际上,数学结果是这样的:

  • VBA 字符串是 Unicode,在这种情况下意味着每个字符是 2 字节。

  • VBA 中的字符串是 4 个字节,用于内部存储之前的长度 字符串,字符串末尾的 unicode Null 的 2 个字节,以及 每个字符 2 个字节。

  • 计算结果为 4 + (64 * 2) + 2 = 每 64 个字符 134 个字节
    字符串。

  • String 数组中的每个条目实际上都是指向 String 的指针,
    所以每个槽还有 4 个字节,到目前为止总共 138 个。

  • 假设这些字符串中有 600 万个,即 828,000,000 字节(使用 逗号美式),depending upon your definition of mb,是 789.6 或 828 mb。

我不确定其余的开销,也许是垃圾收集器引用计数器?

无论如何,我建议您使用 64 槽字节数组来加载和存储您的字符串,假设它是 ASCII 字符。您消除 (4 + 64 + 2) * 6,000,000 字节,您的代码可能会运行得更快,因为它不需要比较那么多字节。您可以通过一次比较一个 Word(32 位或 64 位,具体取决于您的处理器)而不是逐个字符来优化您的排序。

更新

我认为我的计算有点错误。字节数组是SAFEARRAYs,它们本身有相当多的开销,大约 20 个字节。因此节省的费用将接近 (4 + 64 + 2 - 20) * 6,000,000。


原始 ASCII 字符串排序示例

在查看此示例之前,请请采纳我的建议,然后将您的文本导入 Access 以进行排序。总共 380 mb 的 600 万个字符串完全在 Access' limits 之内,而且 Access 可以(据我了解)对它们进行排序,而无需同时将所有字符串加载到内存中

使用以下文本创建一个名为“data.txt”的文本文件:

This
Is
A
File
Of
Strings
To
Sort

在添加代码模块并将其命名为“mdlQuickSort”并添加以下代码。我没有发表太多评论,但如果您对它的工作原理感到好奇,可以阅读 Wikipedia's article on QuickSort 或让我知道,我会添加更好的 cmets。

Option Explicit

Public Sub QuickSortInPlace(ByRef arrArray() As Variant)
    If UBound(arrArray) <= 1 Then
        Exit Sub
    End If
    qSort arrArray, 0, UBound(arrArray)
End Sub

Private Sub qSort(ByRef arrArray() As Variant, left As Long, right As Long)
    Dim pivot As Long
    Dim newPivotIndex As Long
    If left < right Then
        pivot = MedianOf3(arrArray, left, right)
        newPivotIndex = partition(arrArray, left, right, pivot)
        qSort arrArray, left, newPivotIndex - 1
        qSort arrArray, newPivotIndex + 1, right
    End If
End Sub

Private Function partition(ByRef arrArray() As Variant, left As Long, right As Long, pivot As Long) As Long
    Dim pivotValue As Variant
    pivotValue = arrArray(pivot)
    Swap arrArray, pivot, right
    Dim storeIndex As Long
    storeIndex = left
    Dim i As Long
    For i = left To right - 1
        If CompareFunc(arrArray(i), pivotValue) = -1 Then
            Swap arrArray, i, storeIndex
            storeIndex = storeIndex + 1
        End If
    Next
    Swap arrArray, storeIndex, right
    partition = storeIndex
End Function

Private Sub Swap(ByRef arrArray() As Variant, indexA As Long, indexB As Long)
    Dim temp As Variant
    temp = arrArray(indexA)
    arrArray(indexA) = arrArray(indexB)
    arrArray(indexB) = temp
End Sub

Private Function MedianOf3(ByRef arrArray() As Variant, left As Long, right As Long) As Long
    Dim a As Variant, b As Variant, c As Variant
    Dim indexA As Long, indexB As Long, indexC As Long
    Dim ab As Long
    Dim bc As Long
    Dim ac As Long
    indexA = left
    indexB = (left + right) \ 2
    indexC = right
    a = arrArray(indexA)
    b = arrArray(indexB)
    c = arrArray(indexC)

    ab = CompareFunc(a, b)
    bc = CompareFunc(b, c)
    ac = CompareFunc(a, c)

    If ab = -1 Then
        If ac = -1 Then
            If bc = -1 Or bc = 0 Then
                'a b c
                'Already in B
            Else
                'a c b
                Swap arrArray, indexB, indexC
            End If
        Else
            'c a b
            Swap arrArray, indexA, indexB
        End If
    Else
        If bc = -1 Then
            If ac = -1 Then
                'b a c
                Swap arrArray, indexA, indexB
            Else
                'b c a
                Swap arrArray, indexB, indexC
            End If
        Else
            'c b a
            'Already in B
        End If
    End If
    MedianOf3 = indexB
End Function

Private Function CompareFunc(str_a As Variant, str_b As Variant) As Long
    Dim a As Byte
    Dim b As Byte
    Dim i As Long

    For i = 0 To 63
        a = str_a(i)
        b = str_b(i)
        If a <> b Then
            Exit For
        End If
    Next
    If i <= 63 Then
        If a < b Then
            CompareFunc = -1
        Else
            CompareFunc = 1
        End If
    Else
        CompareFunc = 0
    End If

End Function

最后,添加一个名为“mdlMain”的模块。这是加载字符串的地方。代码如下:

Option Explicit

Public Sub Main()
    Dim arrStrings() As Variant
    Dim i As Long

    'Get the strings from the file
    FillArrStringsInPlace arrStrings

    'Print the unsorted list
    Debug.Print "Unsorted Strings" & vbCrLf & "---------------------"
    For i = 0 To UBound(arrStrings)
        Debug.Print StrConv(arrStrings(i), vbUnicode)
    Next

    'Sort in place
    QuickSortInPlace arrStrings

    'Print the sorted list
    Debug.Print vbCrLf & vbCrLf & "Sorted Strings" & vbCrLf & "---------------------"
    For i = 0 To UBound(arrStrings)
        Debug.Print StrConv(arrStrings(i), vbUnicode)
    Next
End Sub

Public Sub FillArrStringsInPlace(ByRef arr() As Variant)
    Dim iFile As Integer
    Dim strInput As String
    Dim lineCount As Long
    Dim arrBytes() As Byte

    'Open a file in the same folder as this Access db called "data.txt"
    iFile = FreeFile
    Open ActiveWorkbook.Path & "\data.txt" For Input As iFile

    'Since I already know how many strings there are, I'm assigning it here.
    'The alternatives would be to either "dynamically resize" the array, which
    'is equivalent to copying the entire thing everytime you add a new string,
    'Or to count the number of newlines in the file and dimensioning the array
    'to that size before reading in the strings line by line.  Neither is as
    'efficient as just defining it before-hand.
    ReDim arr(0 To 7)

    While Not EOF(iFile)
        Line Input #iFile, strInput
        arrBytes = StrConv(strInput, vbFromUnicode)
        ReDim Preserve arrBytes(0 To 63)
        arr(lineCount) = arrBytes
        lineCount = lineCount + 1
    Wend

    Close iFile
End Sub

我已经在里面放了一些代码来尝试使用 CopyMemory 进行优化,但这有点危险,所以我决定不使用它。

【讨论】:

  • 真的是一个答案!!!非常感谢。我也会尝试一下你使用字节数组的建议。
  • Blackhawk 亲爱的,你建议的过程对我来说有点复杂。如果可以,请给我任何提示或链接以供查看。
  • @user3092241 你从哪里加载字符串?它是一个ASCII文本文件吗?
  • 是的,这是真的。我从 ASCII 文件加载字符串。
  • @user3092241 如果字符串的数量真的要“快速增长”(并且您将使用 VBA 排序而不是 Access),我建议将字符串集拆分为可管理的每个大约 100 mb 的块,分别对每个块进行排序,然后最后 merge 将它们重新组合在一起。这将防止内存不足错误的发生,因为您的内存在任何时候都不会超过几百 mb。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 2018-07-27
  • 2010-10-21
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2015-11-30
相关资源
最近更新 更多