开发者

C# Application performance deterioration due to garbage collection?

My application's performance deteriorate as it continues to run through the day.

I suspect it is garbage collector, how can I verify this? Is there a way to find out which object/function is causing garbage collection overhead?

Is there a way to manually perform garbage collection programatically to clear memory of leakages?

Thanks,

edit On one end of the application it receives call back from a unmanaged api to accept data, processes it and then send messages out of socket on the second end. From the second end it then gets back follow up data on the messages it sent out. The application opens 5-6 sockets to send and receive data from the second end. It constantly logs lots of data to windows file system on a separate thread.

My measurements include timestamping (queryperformance counter) just before I send data out and the timestampinga again when I receive the followup from another process back on the socket. I noticed out of multiple sockets I open, the performance deterioration is happening on one socket connection only.

The processing between the timestamping and sending.receiving data over socket includes iterating through 2 arraylist that has no more than 5-6 objects and couple of callbacks.

The memory usage from Task MAnager window does not go up considerably. From 96MB to 100MB after 6-7 hours run.

Following are some observations from running perfmon.

"finanlization survivors" and "promoted finalization memory from Gen 0" gradually increase with time

"Gen 0 collections" going from 1819 at the start to 6000 after 4 hours. "Gen 1 Collections" is 10%-12% of Gen 0 collection and "Gen 2 collection" is 1% or less. COnsidering Gen 0 collection nu开发者_运维知识库mbers are cumulative, it is probably not abig concern.

GC handles" went up from 850ish to 4000.


It's far more likely that you have a memory leak, and invoking the GC manually will not help that: it can't dispose of objects if your code hasn't released them.

Edit

Since your GC handles are increasing, this page suggests that there are non-managed resources that are not getting freed. I've had this happen with bitmaps, for example, but you might have to tell us a lot more about your application to get a more specific suggestion.

Here's a thread that may give you some useful insight.


You can call GC.Collect() to force the garbage collection, but this will not fix memory leaks. Try a memory profiler like ANTS memory profiler to find memory leaks.

ANTS memory profiler


It may not necessarily be a memory problem. Use the Windows Performance Monitor (look in administrative tools) to monitor your app's CPU, Memory, GDI object count, handle count, and see if any of these appear to be climbing throughout the life of your app.

Often, you'll find that you're using something from System.Drawing and not calling Dispose() on it, causing a handle leak. I've found that handle leaks tend to eat performance much faster than memory leaks. And handle leaks cause no GC pressure, meaning you could be leaking handles like a sieve and the GC wouldn't ever know the difference.

So, long story short: measure, measure, measure. Then you'll know what to fix.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜