How to find dispose and memory issues? C#

2019-03-20 13:09发布

My app was using 150mb of memory not to long ago, now it is at 286mb. It slowly rises so i must be forgetting to dispose something. This isnt much of a problem for me since i have 4gb but i want to send this to others who have only 1gb of ram. other then going through the code line by line how can i find objects that need to be disposed of or just generally large objects?

8条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-20 13:56

Here are couple of tricks using the ANTS Memory Profiler to help find undisposed objects and fix the leaks.

  1. The ANTS Memory profiler allows a filter to be set that show just objects that contain a Dispose() method. Turn this on, and you'll be given a list of live objects that have not been disposed.

  2. While finding undisposed objects is useful, even more useful is knowing why these objects are not being disposed. Finding where these undisposed objects are created goes a long way to finding the reason for the leak. If you are able to change the code of the leaking object, a useful trick is to introduce a field to hold a stacktrace, and populate this field at the time of object construction. Then because the ANTS memory profiler allows you to inspect fields of objects, you can simply read off the stacktrace as it was at the time the leaking objects were created. This will give you a strong clue as to who the owner of the leaking objects should be, and how they should go about calling Dispose on the objects they are responsible for.

查看更多
登录 后发表回答