[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-01-22
7
Medium Priority
?
421 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 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 45

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 45

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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 video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 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