Re: Garbage Collection - Strange Results.
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Sun, 15 May 2005 17:36:23 +0200
"CodeGuru" <CodeGuru@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:602B1F08-9484-420C-B157-2652E54E046B@xxxxxxxxxxxxxxxx
> Althought I have read hundreds if not more pages on
> msdn as well other sites on IDisposable and heap
> generations. I am still a bit confused as to why
> the application I have written is acting in such
> "weird/awkward" manner.
>
> Basic outline of the application goes:
>
> Multi threaded socket application using Asynchronous
> writes/reads/connects via TCP Sockets.
>
> I have written an application that starts 100 threads
> and attaches 10 sockets from a peer to each of these
> threads; thus 1000 active sockets open.
>
> Now. the application runs fine; I calculate using ANTS profiler
> or any other profiling utility and it tells me that I have approximately
> 30 MB of Memory used.
>
> I call GC.GetTotalMemory(false) or even (true) and it gives me
> approximately
> the same values depending what stage of processing each thread
> is at.
>
> All threads are doing the same thing by the way.
>
> When I look in Windows Performance Monitor and under .NET Memory
> for that particular application the memory seems to be quite high.
>
> Several Hundred MEGABYTES.
>
> The application runs over time.. slowly but surely increasing in memory.
> as the threads complete their tasks with the sockets it closes them.
> and Disposes each and every object. I explicitly set each object to null
> as well its children.. and have inherited IDisposable on almost all
> objects.
>
> What I don't understand is why the memory keeps increasing,
> while the application keeps reporting only 30MB of usage... the memory
> keeps increasing and increasing until the system runs out of Physical
> Memory
> and causes OutOfMemoryExpections.... its quite strange. I am not doing
> anything within the application that would create anything larger then it
> should.
>
> However I do thousands of XmlSerializations using a single MemoryStream
> for each socket that is created; there are thousands of Asychronous
> callbacks
> that are happening.
>
> My Generation 0 and 1 heaps seem to be doing just fine.. but 2nd gen heap
> seems to be at hundreds of megs; its very strange to see this.
>
> I assumed that the .NET GC will keep on expanding the space required until
> it is ready for a garbage collection.. would it be because of the
> magnitute of
> ArrayLists that I am using ? or perhaps there are scattered memory
> segments.
> throughout the Heaps ? is the GC having a difficult time collecting all
> the
> resources?
>
> Any help on why this is occuring would be appreciated.
>
> I have tried my best to clean up all the resources.. but I figure that
> most of the objects created should automatically get cleaned up
> after it is out of the final scope that it is passed to.. for example.
> once a command comes in via the Async Read Callback, I process it
> and pass it to the appropriate thread DataReceive method.
> I not only dispose the object within CallBack but I also dispose
> the reference that was passed again just incase..
>
> doesn't seem to be doing anything .. and what is even more strange
> is why is .NET or all Profilers such as ANTS .NET profiler report
> that I am only using 30MB of memory ? when in Task Manager
> or Performance Monitor I see hundreds of megs like 600+.
>
> Thank you and great weekend to all.
1. Don't use taskman, use perfmon and the "CLR Memory" counters Gen0, 1 and
2 size to watch managed memory consumption.
2. Take a look at the # of pinned objects using perfmon (CLR memory
counters).
3. Use perfmon to watch your "process" privates bytes.
If the the Gen 2 counter indicates a steady growth and the number of pinned
objects is rather high (> 100) chances are that pinned objects are aging
into Gen2 and as such preventing compactation of the heap.
If that's the case you are probably holding references to socket
(read/write) buffers for the lifetime of the socket.
I would also suggest you to reduce the number of threads, 100 threads is
only a waste of resources (both memory and CPU) when using asynchronous
sockets.
Willy.
.
- Follow-Ups:
- Re: Garbage Collection - Strange Results.
- From: CodeGuru
- Re: Garbage Collection - Strange Results.
- References:
- Garbage Collection - Strange Results.
- From: CodeGuru
- Garbage Collection - Strange Results.
- Prev by Date: load unload assembly
- Next by Date: Re: load unload assembly
- Previous by thread: Garbage Collection - Strange Results.
- Next by thread: Re: Garbage Collection - Strange Results.
- Index(es):
Relevant Pages
|