none
Algorithm Design: Monitoring Memory Usage RRS feed

  • Question

  • Hi

    I am playing around in designing algorithms in C#, and I want to determine the peak memory usage during an algorithm's execution (stack and heap).

    I am of limited financial means, and I using VS 2008 C# Express; so, I want to build my own tools and instead of purchase.

    So, I am looking at members of System.Diagnostics.Process that pertain to memory usage, and I am not certain this is the correct way to go, because these member functions are not granular enough to address memory usage of an algorthm.

    What are other recommended "programmatic" means could I monitor memory usage (stack and heap) of specific function?

    Thanks

    Jeff in Seattle

    Monday, August 17, 2009 7:54 AM

Answers

  • Hi again:

    The CRL Profiler tool for .NET is very helpful, for program memory allocation analysis, etc.

    If you look at the Figure A, below, then you will understand, why this powerful tool should be used by .NET developers and programmers.


    Figure A: The CRL Profiler program, displaying a memory allocation (the heap) mapped graph.

    The CRL can also help you to make more effecient programs with .NET.
    And by effecient I mean, memory usage & allocation, including effectivity, like using one for-loop instead of two.

    I hope this information was helpful…

    Have a nice day…

    Best regards,
    Fisnik


    Coder24.com
    Monday, August 17, 2009 1:39 PM

All replies

  • Hi - I think my information can help.

    Well, I'm not sure when it comes to "programmatically" finding the usage (stack & heap) of a function.
    However, there is a tool available called: CRL Profiler, download from,
    http://www.microsoft.com/downloads/details.aspx?familyid=A362781C-3870-43BE-8926-862B40AA0CD0&displaylang=en 

    The CRL Profiler tool, helps the programmer to find memory usage information,
    like how much memory that function is using? etc. It can also allow you to analyze,
    and see how the application is using it's memory.

    Further reading:
    http://msdn.microsoft.com/en-us/library/ms979205.aspx

    http://www.programmersheaven.com/2/CLR-Profiler

    I hope this information was helpful...

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Monday, August 17, 2009 8:44 AM
  • Hi again:

    The CRL Profiler tool for .NET is very helpful, for program memory allocation analysis, etc.

    If you look at the Figure A, below, then you will understand, why this powerful tool should be used by .NET developers and programmers.


    Figure A: The CRL Profiler program, displaying a memory allocation (the heap) mapped graph.

    The CRL can also help you to make more effecient programs with .NET.
    And by effecient I mean, memory usage & allocation, including effectivity, like using one for-loop instead of two.

    I hope this information was helpful…

    Have a nice day…

    Best regards,
    Fisnik


    Coder24.com
    Monday, August 17, 2009 1:39 PM
  • Thanks CRL Profiler is cool, but I was thinking of implementing some sort of wrapper that I could place around a function call that would capture all memory usage during its execution.

    So, I am considering creating my wrapper by DllImport-ing the function GlobalMemoryStatusEx from kernel32.dll which retrieves information about the system's current usage of both physical and virtual memory.

    Is this a valid approach when monitoring memory usage within a C# application?

    Jeff in Seattle

    Monday, August 31, 2009 6:17 AM
  • Hi Jeff,

    Sure, Windows APIs are valid in C# application through PInvoke. There is a tool named "P/Invoke Interop Assistant" that can help us generate the wrapper,

    In your scenario,

    GlobalMemoryStatusEx's C# declaration looks like,

    Partial Public Class NativeMethods
       
        '''Return Type: BOOL->int
        '''lpBuffer: LPMEMORYSTATUSEX->_MEMORYSTATUSEX*
        <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="GlobalMemoryStatusEx")>  _
        Public Shared Function GlobalMemoryStatusEx(<System.Runtime.InteropServices.OutAttribute()> ByRef lpBuffer As MEMORYSTATUSEX) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
        End Function
    End Class


    And the LPMEMORYSTATUSEX structure GlobalMemoryStatusEx method should be,

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure MEMORYSTATUSEX
       
        '''DWORD->unsigned int
        Public dwLength As UInteger
       
        '''DWORD->unsigned int
        Public dwMemoryLoad As UInteger
       
        '''DWORDLONG->ULONGLONG->unsigned __int64
        Public ullTotalPhys As ULong
       
        '''DWORDLONG->ULONGLONG->unsigned __int64
        Public ullAvailPhys As ULong
       
        '''DWORDLONG->ULONGLONG->unsigned __int64
        Public ullTotalPageFile As ULong
       
        '''DWORDLONG->ULONGLONG->unsigned __int64
        Public ullAvailPageFile As ULong
       
        '''DWORDLONG->ULONGLONG->unsigned __int64
        Public ullTotalVirtual As ULong
       
        '''DWORDLONG->ULONGLONG->unsigned __int64
        Public ullAvailVirtual As ULong
       
        '''DWORDLONG->ULONGLONG->unsigned __int64
        Public ullAvailExtendedVirtual As ULong
    End Structure


    More information about the tool, you can refer
    http://blogs.msdn.com/bclteam/archive/2008/06/23/p-invoke-interop-assistant-justin-van-patten.aspx


    Best regards,
    Colbert Zhou


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, September 2, 2009 8:12 AM
    Moderator