Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

.net - Finding memory leaks in C# application

I have an application in C#, Framework 4. Very basically, this application mainly react to events and creates objects, release them, create database connection and close them.

Now, we've been seeing that the application's process grows sometimes in very strange ways. We've got two different behaviors :

  1. The application grows until reaching up to 4 GB in RAM when usually it "should" stay at around 500 MB. Consequence -> it crashes!
  2. The application grows slowly up to 1200 MB (30 minutes) and then abruptly shrinks to 500 MB (in one second)... and this process repeats itself every now and then. Can this be Garbage Collector in action ?

Now, in order to provide us with more informations about the application, I would like to add in our log files the size of the application's process. Is it possible through the native framework ? Is it possible to know an object' size in C# ?

I've also found the application NetMemoryProfiler4 but I would prefer to use embedded logging if it's possible !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need memory profiler to debug such problems. For example:

Also see suggestions from the other question about memory leaks.

Basically, this boils down to finding the objects in memory that stay here while they shouldn't. It can be event handler holding reference to its class or some collection of objects holding references to their parents, and so on. After finding the root cause you may need to restructure your application to get rid of the unnecessary references. This can be as simple as adding forgotten event unsubscription but in non trivial cases might require applying some structural design patterns. This part is very application specific.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...