用于识别VB6应用程序中的内存占用的工具

时间:2009-04-13 00:03:24

标签: memory vb6 memory-management

有哪些工具可以将VB6应用程序中的内存消耗归因于它的多个组件? 我可以通过观察各种计数器(专用字节,工作集等)来获取整个应用程序所消耗的内存,例如,在Process Explorer中。我希望比这更深入,并了解在运行时创建的各种组件或对象消耗了多少内存。例如,计算在运行时缓存数据的大型集合消耗了多少内存,以及它如何根据集合中的元素数量进行更改。

4 个答案:

答案 0 :(得分:5)

我最喜欢的工具必须是DevPartner,虽然价格为1,500英镑但并不便宜。它确实比内存泄漏检查还要多得多,但如果这就是你所需要的,那么你可能就是地毯式炸弹蚂蚁。

如果您想查看您的应用是否正确释放资源,请使用我编写的函数将内存转储到给定位置。我会先缓存每个变量的地址,然后在关机时调用DumpVariableMemory传入对这些位置的引用,看看它们是否已经被释放。

如果你还没有,你也需要添加一个声明fopr CopyMemory:)

    Public Function DumpVariableMemory(ByVal lngVariablePointer&, _
                                   ByVal lngBufferSizeInBytes&) As String
    '' * Object Name:   DumpVariableMemory
    '' * Type:          Function
    '' * Purpose:       Returns a memory dump of the variable or pointer location
    '' * Created:       21/08/2006 - 17:41:32
    '' * Coder:         Richard Pashley - NUPUK00008148
    '' * Build Machine: W-XPRP-77
    '' * Encapsulation: Full
    '' * Parameters:    lngVariablePointer      -   Long    -   Pointer to the data to dump
    '' *                lngBufferSizeInBytes    -   Long    -   Size of the dump to ouput in bytes
    '' * Returns:       -                       -   String  -   Memory dump output as a string
    '' *                This will dump the memory location starting at the pointer address and
    '' *                ending at the address plus the offset (lngBufferSizeInBytes).
    '' *                You can use LenB to determine the size of the variable for the
    '' *                lngBufferSizeInBytes parameter if required.
    '' *                Example: DebugPrint DumpVariableMemory(VarPtr(lngMyLongValue),LenB(lngMyLongValue)
    '' * Modified By:   [Name]
    '' * Date:          [Date]
    '' * Reason:        [NUPUKxxxxxxxxx]
    '' Declare locals
    Dim lngBufferIterator&                  '' Buffer iterator
    Dim lngBufferInnerIterator&             '' Buffer loop inner iterator
    Dim bytHexDumpArray() As Byte           '' Received output buffer
    Dim strDumpBuffer$                      '' Formatted hex dump construction buffer
    Dim lngValidatedBufferSize&             '' Validated passed buffer size
    '' Turn on error handling
    On Error GoTo DumpVariableMemory_Err
    '' Resize output buffer
    ReDim bytHexDumpArray(0 To lngBufferSizeInBytes - 1) As Byte
    '' Retrieve memory contents from supplied pointer
    Call CopyMemory(bytHexDumpArray(0), _
       ByVal lngVariablePointer, _
       lngBufferSizeInBytes)
    '' Format dump header
    strDumpBuffer = String(81, "=") & vbCrLf & _
       "Pointer Address = &h" & Hex$(lngVariablePointer) & _
       "   Ouput Buffer Size = " & FormatBytes(lngBufferSizeInBytes)
    '' Add header seperator
    strDumpBuffer = strDumpBuffer & _
       vbCrLf & String(81, Chr$(61))
    '' Validate buffer dimensions
    If lngBufferSizeInBytes Mod 16 = 0 Then
        '' Validated ok so assign
        lngValidatedBufferSize = lngBufferSizeInBytes
    Else
        '' Refactor to base 16
        lngValidatedBufferSize = _
           ((lngBufferSizeInBytes \ 16) + 1) * 16
    End If
    '' Iterate through buffer contents
    For lngBufferIterator = 0 To (lngValidatedBufferSize - 1)
        '' Determine if first row
        If (lngBufferIterator Mod 16) = 0 Then
            '' Format dump output row
            strDumpBuffer = strDumpBuffer & vbCrLf & Right$(String(8, Chr$(48)) _
               & Hex$(lngVariablePointer + lngBufferIterator), 8) & Space(2) & _
               Right$(String(4, Chr$(48)) & Hex$(lngBufferIterator), 4) & Space(2)
        End If
        '' Determine required dump buffer padding
        If lngBufferIterator < lngBufferSizeInBytes Then
            '' Pad dump buffer
            strDumpBuffer = strDumpBuffer & Right$(Chr$(48) & _
               Hex(bytHexDumpArray(lngBufferIterator)), 2)
        Else
            '' Pad dump buffer
            strDumpBuffer = strDumpBuffer & Space(2)
        End If
        '' Determine required dump buffer padding
        If (lngBufferIterator Mod 16) = 15 Then
            '' Pad dump buffer
            strDumpBuffer = strDumpBuffer & Space(2)
            '' Iterate through buffer row
            For lngBufferInnerIterator = (lngBufferIterator - 15) To lngBufferIterator
                '' Validate row width
                If lngBufferInnerIterator < lngBufferSizeInBytes Then
                    '' Validate buffer constraints
                    If bytHexDumpArray(lngBufferInnerIterator) >= 32 And _
                       bytHexDumpArray(lngBufferInnerIterator) <= 126 Then
                        '' Ouput data to dump buffer row
                        strDumpBuffer = strDumpBuffer & _
                           Chr$(bytHexDumpArray(lngBufferInnerIterator))
                    Else
                        '' Pad dump buffer
                        strDumpBuffer = strDumpBuffer & Chr$(45)
                    End If
                End If
            Next
            '' Determine required dump buffer padding
        ElseIf (lngBufferIterator Mod 8) = 7 Then
            '' Pad dump buffer
            strDumpBuffer = strDumpBuffer & Chr$(45)
        Else
            '' Pad dump buffer
            strDumpBuffer = strDumpBuffer & Space(1)
        End If
    Next
    '' Assign result to function output
    DumpVariableMemory = strDumpBuffer & _
       vbCrLf & String(81, Chr$(61)) & vbCrLf
Exit_Point:
    Exit Function
    '' Error Handling
DumpVariableMemory_Err:
    LogError "modNYFixLibrary.DumpVariableMemory", Err.Number, Err.Description
    DumpVariableMemory = String(81, Chr$(61)) & vbCrLf & _
       "DumpFailed!" & vbCrLf & String(81, Chr$(61))
    GoTo Exit_Point
    Resume
End Function

答案 1 :(得分:2)

我不确定任何公开的(免费)工具会将VB6代码分析到模块级别。有几种内存分析器可用于C / C ++和.NET,但在我看到的VB6上并不多。看起来这个领域的所有旧供应商(IBM Purify,Compuware Devpartner / Boundschecker)都已被买断,或仅转移到.NET支持。

您可以尝试GlowCode。它声明了C ++支持,但也强调了Win32原生x86映像。

Microsoft发布DebugDiag,它支持.NET或Win32的内存泄漏检测,但我从未在VB中使用过它。它可能没有显示出对模块级别的未完成分配,但我敢打赌它至少会归因于哪些库/ dll分配了最多的内存。

答案 2 :(得分:2)

Memory Validator可以告诉你在VB6程序(以及C ++,C,Delphi,Fortran 95 ......)中分配(和泄露)内存的位置。

答案 3 :(得分:1)

MS站点上有另一个名为processmonitor.exe的工具。它会报告每个请求调用,您可以使用其过滤功能仅监视应用程序的进程请求。