Solved

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

Posted on 2013-01-22
7
409 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 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 30

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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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