?
Solved

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

Posted on 2013-01-22
7
Medium Priority
?
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 750 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 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 750 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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