views:

247

answers:

2

I've written an app that spins a few threads each of which read several MB of memory. Each thread then connects to the Internet and uploads the data. this occurs thousands of times and each upload takes some time

I'm seeing a situation where (verified with windbg/sos and !dumpheap) that the Byte[] are not getting collected automatically, causing 100/150MB of memory to be reported in task manager

if I call System.GC.Collect() i'm seeing a huge drop in memory, a drop of over 100MB

I dont like calling System.GC.Collect() and my PC has tons of free memory. however if anyone looks at TaskManager they're going to be concerned, thinking my app is leaking horribly.

tips?

+11  A: 

The runtime is taking advantage of a large amount of free memory to reduce the impact of the garbage collector on the performance of your application. The "problem" here is a general lack of understanding (e.g. your users) that when a system has more memory, it's ok to use it (otherwise what's the point?).

280Z28
@Chris, if you read more about GC, you will see such memory footprint is expected. It was not yet time for GC to free up the Byte[] when you captured the dumps.
Lex Li
@Chris: 280z28 and Lex are right. As long as you are not holding on to your byte[] references any longer than necessarily, there is nothing to worry about. .NET applications tend to use more memory than native applications, because the GC will just clean up when needed.
Steven
I with ya... preacher/choir thing... I've echoed the same guidance as above MANY times. until now I've always thought I was correct the problem I have is if you've got tons of RAM and unless you poke around with windbg/sos all signs point to a misbehaving app. my users tend to be a little savy, but not programmers... they have all tracked and fixed misbehaving apps by looking for tons of RAM use...I thing I've got to eat some crow on this... when my app has done the bulk of its heavy lifting I should call Collect(), else (as I can measure) my app gets killed in task mgr
stuck
given my app is being killed in taskmgr (which stinks, I have to unroll transactions on restart). how best to call Collect() such that I dont screw up the collector?
stuck
+3  A: 

Problem is, some users do check the memory and cpu usage, and reports your application is consuming too much even when there is no problem with their performance. I think they do this to show their boss/girlfriend/fellows how much they know about computers.

A carefully placed CG.Collect in your app will probably reduce this human (not computer) problem. I think you should do it.

Daniel Dolz