Solved

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

Posted on 2013-01-22
7
400 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now