mad*_*rth 12 memory vb6 memory-management
有哪些工具可以将VB6应用程序中的内存消耗归因于它的多个组件?我可以通过观察各种计数器(专用字节,工作集等)来获取整个应用程序所消耗的内存,例如,在Process Explorer中.我希望比这更深入,并了解在运行时创建的各种组件或对象消耗了多少内存.例如,计算在运行时缓存数据的大型集合消耗了多少内存,以及它如何根据集合中的元素数量进行更改.
我最喜欢的工具必须是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
Run Code Online (Sandbox Code Playgroud)
我不确定是否有任何公开可用的(免费)工具能够将 VB6 代码分析到模块级别。有几个可用于 C/C++ 和 .NET 的内存分析器,但我看到的 VB6 上的内存分析器并不多。看起来该领域的所有老供应商(IBM Purify、Compuware Devpartner/Boundschecker)要么被收购,要么转向仅支持 .NET。
您可以尝试GlowCode。它声明了 C++ 支持,但也强调了 Win32 本机 x86 映像。
Microsoft 发布了DebugDiag,它支持 .NET 或 Win32 的内存泄漏检测,尽管我从未在 VB 中使用过它。它可能不会显示模块级别的未完成分配,但我敢打赌它至少会属性哪些库/dll 分配了最多的内存。
| 归档时间: |
|
| 查看次数: |
6806 次 |
| 最近记录: |