Re: GC Handle leaks....

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Ollie Riches" <ollie.riches@xxxxxxxxxxxxxxxxx> wrote:

I need some advice on the use of 'Son Of Strike' (sos.dll) and output in
WinDbg.

Note that you can use many features of SOS in VS 2005, without having to
resort to WinDbg. Make sure you're debugging in mixed mode (i.e. enable
unmanaged debugging), and enter your commands (e.g. ".load sos") in the
Immediate window.

Over a period of time I notice in task manager that the 'handles' value had
increased from a starting value of approx. 300 to over 15,000. I use perfmon
to check the .Net memory and related GC counters and the GC Handles for the
process had indeed risen to 15,000.

The handles in Task Manager relates to operating system handles, and are
typically files, directories, registry keys, named pipes, TCP
connections, directories, processes, threads, semaphores and named
events.

Process Explorer from SysInternals.com can help you find out what you're
leaking in this respect.

GC handles are used (AFAIK, and I am not expert at all on this) for
accessing a managed class from unmanaged memory, which would presumably
imply things like:

* keeping an object alive from unmanaged memory (e.g. gcroot<>)
* pinning, to prevent GC from moving a block of memory
* weak references (i.e. the WeakReference class uses GCHandle)

!help gchandles in SOS says this:

"The most common handles are "Strong Handles," which keep the object
they point to alive until the handle is explicitly freed. "Pinned
Handles" are used to prevent the garbage collector from moving an object
during collection. These should be used sparingly, and for short periods
of time. If you don't follow that precept, the gc heap can become very
fragmented."

From the dump of this I can see that I have forgotten to call Dispose on a
System.Threading.Timer object. So I implement a fix and patch the server
accordingly. When implementing the fix I review the code and make sure that
all Dispose methods are called accordingly (using 'using' :)).

Make sure you're calling Dispose() for pretty much everything that
implements IDisposable: this include FileStreams, WaitHandles, Forms,
etc. Garbage collection is a memory manager, not a resource manager.

After monitoring the process for another couple of days I see that the
'handles' value in task manager has risen but by not as much approx 3,500
BUT using perfmon I see that the GC Handles has stayed constant and seems to
now exhibit 'normal' behaviour.

I re-ran 'Son Of Strike' with command parameter '!sos.objsize' and I am
still seeing a lot of entries System.Threading.Timer.

HANDLE(Strong):6d1cd8: sizeof(00ec194c) = 920 ( 0x398) bytes
(System.Threading.TimerCallback)
HANDLE(Strong):6d1cec: sizeof(00ebffac) = 920 ( 0x398) bytes
(System.Threading.TimerCallback)
HANDLE(Strong):6d1cf4: sizeof(00ebfde4) = 920 ( 0x398) bytes
(System.Threading.TimerCallback)
HANDLE(Strong):6d1d08: sizeof(00ebf718) = 920 ( 0x398) bytes
(System.Threading.TimerCallback)

etc....

Is this still an issues?

If it's not leaking, and the working set isn't growing, and performance
isn't degrading, then I would say that it isn't a problem.

Are the callbacks still being called? What does the tail end of

!dumpheap -stat

say? And are these objects being kept alive because of these handles? In
other words, are a significant amount of your resources being consumed
by this?

What does 'HANDLE(Strong)' mean in this respect?

It means the GCHandle doesn't permit the object to be collected. For
example, WeakReference's use of GCHandle would be weak, because the
whole purpose of the class is to keep a reference but also allow the
target object to be collected. (In fact, WeakReference uses "weak short"
handles; I don't know the difference between weak short and weak long
handles.)

Does the 'handles' value in task manager have any real meaning in .Net or
relation to .Net GC Handles?

No.

-- Barry

--
http://barrkel.blogspot.com/
.



Relevant Pages

  • Re: VIRTUAL MEMORY
    ... In task manager look at the performance tab and repost the PF Usage ... What virus was ... Hard to know if you dont know about computers I know. ... Are you using one of those memory managers which free up memory. ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Physical Memory
    ... Thanks for your help, John. ... As for the services just use the Windows Services Management Console to ... Remote Access Connection Manager ... gig Pentium 4 processor and apparently a half gig of available memory, ...
    (microsoft.public.windowsxp.general)
  • Re: Scalable memory manager for Delphi.
    ... Performance should be equal or better than any other manager on the market. ... We found performance to be generally equal to MultiMM yet offers several ... memory corruption. ... Xeon testing with it, however, the latest demo app hasn't been run on Xeons ...
    (borland.public.delphi.thirdpartytools.general)
  • RE: Memory growth halts with use of Profiler - bug in .NET service
    ... tried 3, including CLRProfiler), sos and the ntsd debugger showed me that I ... with hudnreds of megs of memory as well. ... This is basically the manual version of what CLRProfiler does; ...
    (microsoft.public.dotnet.framework.performance)
  • Re: GC.Collect can be trusted?
    ... compact the heap. ... It's not the task of the GC to return the memory to the OS, ... contain any GC heap data (managed objects and other CLR data). ... the memory manager won't ever return the default process heap segments occupied by ...
    (microsoft.public.dotnet.languages.csharp)