Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tracking memory used by application

Status
Not open for further replies.

Ramya1

Programmer
Oct 11, 2002
40
US
Hi,
My application has bunch of third party components. My app leaking. I've run Devpartner- smartcheck, but it didn't find any leak nor gave details. I've tried MemoryStatus function call. But it gives the memory details for the whole machine. Not just my application. I'd like to get the memory used by my application alone at different function calls. Can anyone suggest me the best way to do that and if possible send me code to accomplish that.

Thanks for your help in advance.
Ramya.
 
Try this routine. I didn't write it and I've forgotten where I found it but I think it may give you what you're looking for - the amount of memory used by your VB app. You could even adapt it to work on other processes but I'll leave that up to you. :)

This is my first TGML code posting so watch out for word wrap problems or other posting flubs.

Code:
Private Type SYSTEM_INFO
    dwOemID                      As Long
    dwPageSize                   As Long
    lpMinimumApplicationAddress  As Long
    lpMaximumApplicationAddress  As Long
    dwActiveProcessorMask        As Long
    dwNumberOrfProcessors        As Long
    dwProcessorType              As Long
    dwAllocationGranularity      As Long
    dwReserved                   As Long
End Type

Private Type MEMORY_BASIC_INFORMATION
    BaseAddress         As Long
    AllocationBase      As Long
    AllocationProtect   As Long
    RegionSize          As Long
    State               As Long
    Protect             As Long
    lType               As Long
End Type

Private Declare Sub GetSystemInfo Lib "Kernel32" (lpSystemInfo As SYSTEM_INFO)
Private Declare Function VirtualQuery Lib "Kernel32" (ByVal lpAddress As Long, lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Long) As Long

Public Function GetMemoryUsedByProcess() As Long
    ' Purpose:  Returns the number of bytes used by current process.
    '           Returns -1 if function fails

    Dim lngLenOfMBI         As Long                     ' Size of MBI type
    Dim lngMemoryPointer    As Long                     ' Memory pointer
    Dim lngPrivateBytes     As Long                     ' Total bytes used so far
    Dim lngReturnValue      As Long                     ' API return code
    Dim MBI                 As MEMORY_BASIC_INFORMATION ' Memory block status
    Dim SI                  As SYSTEM_INFO              ' Used to find out address range used my this process

    ' Assume failure
    GetMemoryUsedByProcess = -1

    ' Get number of bytes in MEMORY_BASIC_INFORMATION structure
    lngLenOfMBI = Len(MBI)

    ' Find the address range used by this process
    Call GetSystemInfo(SI)
    lngMemoryPointer = SI.lpMinimumApplicationAddress
    Do While lngMemoryPointer < SI.lpMaximumApplicationAddress
        MBI.RegionSize = 0
        lngReturnValue = VirtualQuery(lngMemoryPointer, MBI, lngLenOfMBI)
        If lngReturnValue = lngLenOfMBI Then
            If ((MBI.lType = MEM_PRIVATE) And (MBI.State = MEM_COMMIT)) Then
                ' This block is in use by this process
                lngPrivateBytes = lngPrivateBytes + MBI.RegionSize
            End If

            On Error GoTo Finished
            ' the only time an error can occur on the next line is an overflow
            ' error. We have got as far as we can anyway so we must have finished.
            ' advance lngMemoryPointer to the next address range.
            lngMemoryPointer = MBI.BaseAddress + MBI.RegionSize
            On Error GoTo 0 ' switch off error trapping

        Else
            ' Function failed - abort loop
            Exit Function
        End If
    Loop

Finished:

    GetMemoryUsedByProcess = lngPrivateBytes

End Function

Net_Giant

What fun is a technology if you can't crash the OS?
 
Very nice. I think I'll start using your method. Thanks for pointing it out to me.

Net_Giant

What fun is a technology if you can't crash the OS?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top