tags:

views:

84

answers:

4

Hi All, I have a problem of memory leaks in my application when its running. The Application Uses CPU Memory like this.

Minimum Percentage is 6% Maximum Percentage is 35% Maximum Peak Memory is 90MB

I have used ANTS Memory Profiler for Analyzing Memory Leaks in the Application. But I don't know how to reduce the memory usage of Application while running.

Please Any one can give me solution as soon as possible.

Thanks and Regards Ramesh N

+3  A: 

How do you know you have memory leaks? Bear in mind that the GC may not run if there's no memory pressure on the system, so it may look like memory is being allocated and not released - the GC will deal with it if necessary.

thecoop
A: 

Why do you think your application is leaking? If it stays at a consistent 90MB usage then this isn't a leak - it's just showing more memory use than you think. If it's a genuine memory leak then over time it would creep higher through usage. If you can't get it to 100MB then it's not really leaking...

.NET applications often show higher memory usage (especially in certain views of task manager) than you'd expect. Is this actually a problem for you, or are you perceiving it as a problem because it's higher than you think?

Dan Puzey
A: 

Do you experience any problems from the memory use? Otherwise it doesn't seem like there is a problem at all.

Unless there is any actual memory leeks (but I suppose there isn't, as you have profiles the code), an application using several megabytes or memory, or even growing constantly to a certain point, is no problem.

It's a common misconception that a computer should have as much free memory as possible, but there is no performance benefit from that. Having unused memory doesn't make the applications run any faster in any way.

It's normal for a .NET application to allocate more memory as it runs. As long as there is free memory, this is by far more efficient than running throrough garbage collections to try to free up memory. The application will clean up the memory when needed.

The system can send a signal to an application to make it free up as much memory as possible. If you minimise an application, this signal is sent to it, so you can use that to find out approximately how much memory your application uses more than the absolute minimum.

Guffa
A: 

First, put in some TEMPORARY code that calls GC.GetTotalMemory(true) at regular intervals and logs it.

Run the application for some time.

THEN TAKE OUT THE TEMPORARY CODE. Since this method really does hurt memory usage, but it will give you some useful details in doing so. Remember, this is purely an investigatory step, not something to use in 99% of production code.

Now, see if the figures it's returning are steadily climbing. If they're not (and that includes climbing a bit and then dropping again), you've no problem. End of solution.

If you do, then you need to look at direct or indirect use of unmanaged resources, which either are unmanaged memory, or use it. These will split into too cases.

The first is where you yourself are using unmanaged resources. Make sure that you are wrapping them in some safe-handle based wrapper and that they are disposed on each use along with having a finaliser. Don't mix direct use of managed and unmanaged resources in the same class (and then avoid the Dispose(bool) pattern, as it's really part of the anti-pattern of mixing these).

The second is where you use something that in turn uses unmanaged resources. A class might be such if it implements IDisposable. Make sure these are always disposed.

Make sure you are not interning strings needlessly. Interning strings is a useful memory-saving technique, but only if you know that the string value in question will be used regularly throughout the lifetime of the project (or at the very least, you will add few which won't be used again throughout that lifetime). If you intern strings that aren't regularly used, you've hit on one of the best ways to push memory into a tight spot with managed code (GC can happen on the intern pool now, but it often doesn't).

There are also techniques to reduce memory use rather than avoid leaks, but since you're only using a very small amount of memory (90MB) these aren't worth considering here.

Incidentally, what size paging file do you have? 90MB being 35% means a total memory of 256MB. Unless you've got 64MB of physical RAM, that's a bit low. Current advice puts page files at about 100% or less of physical RAM, but that's based on the tendency toward larger RAM sizes these days. If you've got 128MB in that thing, I'd at least double up that page file to give a total memory of around 390MB.

Jon Hanna