none
FInd out 'private bytes' RRS feed

  • Question

  • I have an application that I suspect is leaking memory. I further suspect that it is leaking unmanaged memory. I can look at the perf counters for private bytes. I would like to know if there is a managed function to get the amount of unmanaged memory allocated by a particuliar process (private bytes?).

    Thank you.

    Kevin
    Tuesday, November 10, 2009 7:00 PM

Answers

All replies

  • Process.PrivateMemorySize should do the trick
    Tuesday, November 10, 2009 7:58 PM
  • Yes Private Bytes in perfmon for that telltale sign. See this article Identify And Prevent Memory Leaks In Managed Code to begin that process.

    One other item to look for is on the Processes tab of the Windows Task Manager. ( View + Select Columns,) check Handles, GDI Objects and USER Objects. Observe these values for your program. If there is t a handle leak, you'll see one of these steadily climbing if you do. GDI in all likelihood under those scenarios.

    In general when the OS runs an application it tries to determine how much memory is being used. It will allocate more memory for the application that what the application currently needs. The reason for that is, to allocate/deallocate memory is a cpu intensive operation. Why parallel what the app needs when it can reside in a pool of memory which will allow it, the app, to expand and contract with no interference from the operating system. Saves on cycles.

    What the developer sees is a high water mark for memory. If the system is not stressed, the system will not reclaim any memory and keep the high water mark. There are many posts in this forum where the users say, processing done, memory cleaned up but the OS still shows my app at memory point X, when it should be memory point M (lower). Winform users report the same but if one minimizes the app, suddenly the mem usage reported drops to that M level. That is done by design. Minimizing suggests that an app doesn't need the memory for it will not be interacting with the user and the OS will reclaim the point. If its not a winform and the OS is not stressed, the app stays at the high water mark of X.


    More articles of interest:

    CLR Inside Out: Investigating Memory Issues -- MSDN Magazine, November 2006
    Debug Leaky Apps: Identify And Prevent Memory Leaks In Managed Code -- MSDN Magazine, January 2007
    Download details: Debug Diagnostic Tool v1.1
    Joe Duffy's Weblog (Dispose, Finalization, and Resource Management)

    HTH GL
    William Wegerson (www.OmegaCoder.Com )
    Tuesday, November 10, 2009 8:29 PM
    Moderator
  • Hello

    You can also get the memory information using .NET class: PerformanceCounter . You can use the PerformanceCounter class to query the current process's Memory Performance Counters. Here is a code example.

    If you have any other questions, please feel free to post here.
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, November 11, 2009 1:27 PM
    Moderator
  • Since PrivateMemorySize is marked as "Obsolete" I am using PrivateMemorySize64. I am trying to track a memory problem and I find that PrivateMemorySize64 seems to return a cached value. The reason that I suspect this is that I make this call and record the value then run some instructions and then make the call again. The first and second call are always the same even though the performance counter value continually goes up. Based on my understanding of the code this means one of two things, 1) This call indeed returns a cached value, 2) The climb in private bytes is outside of the begin/end section of code. I just want to eliminate 1) as a possibility.

    Kevin
    Friday, November 13, 2009 3:59 PM
  • Hello

    You can also get the memory information using .NET class: PerformanceCounter . You can use the PerformanceCounter class to query the current process's Memory Performance Counters . Here is a code example .
    *sigh* I was hoping that you would use my blog article as an example. ;-)

    Monitor Threads, Handles and Bytes on the Status Strip

    Back to the subject though, if you feel there is a memory leak...does the object(s) in question have a dispose and release the references and events correctly? Note simply setting events to null will not properly release resources and cause a memory leak. One has to unhook them with the -= operator.

    ???

    William Wegerson (www.OmegaCoder.Com)
    • Marked as answer by KevinBurton Wednesday, November 18, 2009 3:55 PM
    Tuesday, November 17, 2009 8:48 PM
    Moderator
  • Great blog! I have subscribed your RSS feed. :)
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, November 18, 2009 11:05 AM
    Moderator