How to find problems with memory and memory? FROM#

My application used 150 MB of memory not so long ago, now it is 286 MB. He rises slowly, so I must forget to dispose of something. This is not a big problem for me, since I have 4gb, but I want to send it to others who have only 1gb RAM. another, and then through the code line by line, how can I find the objects that need to be deleted or just generally large objects?

+6
performance c # memory-leaks
source share
8 answers

Extension answers JP and Reed.

I wanted to figure it out a bit. If you see a significant increase in memory, the problem is unlikely to be a problem when calling Dispose. Dispose is typically used to free unmanaged resources, such as handles. They do not take up much memory, but instead are more valuable than resources.

The increase in memory is usually associated with large objects or collections accessible from a managed object that is rooted directly or indirectly through a stack object or a strong GC handle. This is an area in which you probably want to focus your research.

+4
source share

Browse the .NET Profiler Profiler . There is a 15-day trial and it costs a license fee.

Easily identify memory leaks capture and compare snapshots. NET snapshots include distribution data for .NET instances and live instances at the time the snapshot was taken. They provide a lot of useful information and make it easy to identify potential leak memory, especially when two snapshots are compared.

+2
source share
+1
source share

You can also use WinDbg and SOS. They have the advantage of being free and very, very thorough, if a little difficult to use.

Here 's a blog post describing the process.

+1
source share

Currently, the code project has a link to an application specifically designed to search for undisclosed objects.

http://www.codeproject.com/KB/dotnet/undisposed.aspx

0
source share

Check out this link

Stephen Tub pretty much explains the various methods for this. The following are some brief points from his article.

  • By adding a finalizer for debugging purposes, you can enter a way to find out when the class was not configured correctly, if the finalizer is never called, you know that it was not named

  • To get additional information about the instance, threadIds, etc., to help narrow down which instance it was not used in, it creates the class FinalizationDebgger which your one-time class will hold, on which Dispose of the Dispose of the Instance of the FinalizationDebugger class will be called, when he is set up. If Dispose does not call your instance of the class when Finalizer starts, the finalizer for the FinalizationDebgger Instance is called, in which you can raise or throw an exception to help debug the problem.

  • Move all the tracking-related code to the base class, which your one-time class then inherits, which makes the code much cleaner. This approach may or may not work as your base class burn and if you are already inheriting from another base.

  • In the latter version, everything is exposed in a static class that your instances are calling. FinalizationDebugger becomes a static class that provides three static methods: Constructor, Dispose, and Finalizer. The idea is that you call these methods from the appropriate place in your class (dispose / finalize / constructor). This is minimally invasive in your code, since it usually involves adding only three lines of code. All of these methods are marked with a conditional attribute, so that they will only be called according to your class when you compile your class in DEBUG mode.

Stephen also explains the pros and cons of each of his approaches. The solutions present various options, and you will need to evaluate each one to find out which one is best for your situation. MUST read IMHO

Hope this helps.

0
source share

Also try ANTI Memory Profiler . There is a 14-day fully functional free trial, and I really like the user interface.

Disclosure: they are now sponsoring the Herding code, so I tried it. But I was very impressed - I profiled the application for 30 hours and received a lot of useful information from it. The user interface is really useful - it guides you through this process and it looks intimidating.

alt text http://www.red-gate.com/products/ants_memory_profiler/images/object_retention_graph.gif

0
source share

Here are a few tricks using the ANTS Memory Profiler to help you find undisclosed objects and fix leaks.

  • The ANTS profiler allows you to set a filter that only shows objects that contain the Dispose () method. Turn this on and you will be given a list of live objects that have not been posted.

  • When searching for useful objects, it is useful even more useful to know why these objects are not deleted. Finding where these non-isolated objects are created is a long way to finding the cause of the leak. If you can change the code of a leaking object, a useful trick is to enter a field to store the stacktrace and fill in this field during the construction of the object. Then, because the ANTS memory profiler allows you to check the fields of objects, you can simply read the stack as it was when the leaking objects were created. This will give you a clear idea of ​​who the owner of the leaking objects should be, and how they should access Dispose for the objects for which they are responsible.

0
source share

All Articles