Solved

How could I find out Memory leak issue in .NET?

Posted on 2013-01-22
7
405 Views
Last Modified: 2013-02-06
I aware some of the memory management techniques in C#.NET such as 'using' should be used while initiating IDisposable objects. But I would like to get answer for below questions. Even the number of questions are more they are related. Please do provide your thoughts.

How could I find out memory leak has occurred in my project?

What would happen if memory leak is more?  

How could I debug and find out memory has been leaked because of my application? Is any specific way or tool there?

How could I fix them?

Please note that I'm using .NET Windows Application.
0
Comment
Question by:Easwaran Paramasivam
7 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 38805189
Detecting memory leaks is not an easy task and there's no definitive answer to this question. You could use profiler tools such as the ANTS Memory Profiler (http://www.red-gate.com/products/ants_memory_profiler/index.htm) or dotTrace (http://www.jetbrains.com/profiler/) . You could also take a look at the CLR Profiler (http://www.microsoft.com/downloads/details.aspx?familyid=86ce6052-d7f4-4aeb-9b7a-94635beebdda&displaylang=en) from Microsoft which is free but is more difficult to use.

Comment from http://stackoverflow.com/questions/2259433/how-to-detect-memory-leaks-in-c-sharp-application
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38805224
In .NET you should never have any memory leaks because of how it handles memory.
If you, however, call into unmanaged code there could be problems there.

Note memory leaks and resource leaks are two different things - you could still leak resources in a .net application.


Is there a reason you suspect a memory leak ?
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 38806971
According to some sources, there ARE memory leaks in .NET:
http://www.dotnetfunda.com/articles/article625-best-practices-no-5-detecting-net-application-memory-leaks.aspx

Really, I met them many times. Objects are created but not destroyed - this is just common! Why it happens is a different story. .NET garbage collector is a very good and powerful thing but it is not able to do all cleaning job after a developer, if the developer does not care :).

About profilers. They are not so hard to use but of course need some experience.
Profilers like Red Gate have a trial period so you may try them for free.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 38808821
@AndyAinscow: Do you mean that .net garbage collector would take care all? we no need to close or dispose any resource? I don't think so.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38808879
Dispose is typically used to release resources eg. a database handle, unmanaged memory you object has created.

The .net garbage collection mechanism is meant to release memory taken by objects no longer in use.  When an object goes out of scope and has no references to it then the GC mechanism will flag it for disposal.  It does NOT dispose of it immediately, only when it determines there is a requirement such as the memory being needed elsewhere.
This means that you can create objects in a loop, they go out of scope, the memory in use in your system increases BUT there is in fact no memory leak.  All that has happened is that the GC has not run to reclaim the memory.
In fact calling the GC directly to run is not recommended because that takes resources - ie. slows your application unnecessarily.

You can call Dispose directly to release the resources in use - you may for instance have a limit to how many database handles you can have.


ps. Personally I don't like GC.  It means one can be lazy in writing code.  Having a memory leak means somewhere in the code there is a failure in the logic.  The GC removes that 'debugging' assistance.
As I asked earlier - do you have a specific reason to think your app is leaking memory.
0
 
LVL 40
ID: 38810280
There are usually no memory or resources leaks if you stick to the classes provided with the framework. There have been a few in the past, but they have been corrected in patches and updates.

Some will tell you that there are still problems there, but it is usually because they do not understand how the garbage collector works. It cleans up only when it needs resources. For instance, a file that was not implicitely closed in the code will eventually be closed when the finalizer of the class that was used to open it will be called by the GC. But this can happen long after the program is terminated, specially if nothing else runs over the framework. This is not a leak. Most garbage collectors are built that way in order to optimize performance and memory usage.

When you need to throw away a sheet of paper, you do not bring it to the dump site. That job is done in steps in order to optimize the processus of eliminating waste. Same with the GC, and whence its name.

However, you can easily have a leaks in a custom or third party class that is not designed properly. I meet hundred of programmers in my job, and a lot of them have learned .NET on the fly and are not aware of some of the little details. Many do not know how to properly implement Finalize and Dispose in their own classes. Many do not even know that these need to be handled in some custom classes.
0
 
LVL 16

Author Closing Comment

by:Easwaran Paramasivam
ID: 38858636
Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question