views:

698

answers:

7

I have a windows service multithreaded application for indexing purpose which have six threads. It is working fine except memory leakage. Actually when the service is started, then the service is consuming 12,584kb memory, after some time it is taking memory of 61,584 kb. But after indexing process is complete it is not releasing memory. I need it come back to its previous position after the indexing is complete, that is it should take the memory with which it started e.g. 12,584kb in this case. I have used garbage collection but it is not doing what I want.

Can anyone please help me?

Thanks in advanced Susanta

+2  A: 

.NET doesn't release memory to satisfy people staring at Task Manager. Allocating memory is expensive. The CLR is designed to do so sparingly, and to hold onto any memory it allocates as long as possible. Think of it this way--what's the point of having 4gb of memory when you're not using half of it?

Unless you actually KNOW that you have a memory leak (as in, your app crashes after two days of uptime), let me give you a piece of advice... Close Task Manager. Don't optimize for memory before you know you need to. Relax, guy. Everything is fine.

Will
+1 for the CRL behavior. However, just waiting for the crash does not seem like the way to go. A .NET memory profiler should be able to show whether the memory is just not released to the system, or if there are really objects leaking (most common case, in my experience at least: forgetting to clear some collection)
gimpf
There's nothing wrong with waiting for an app to crash before determining something is wrong. Your memory profiler will make it easier to get some sleep, however.
Will
+2  A: 

First advice would be to throw a memory profiler at it. I've always been happy with Red Gate's ANTS profiler, which will help identify which objects are leaking, if any.

Do bear in mind that there may be first time initialisation happening all over the place, so you probably want to track it over time

Rowland Shaw
+1  A: 

61 MB does not sound out of the ordinary. You will need to let the service run for awhile and monitor its memory usage for a rising trend. If you see the application leveling out at a certain value then there is probably nothing to worry about.

Brian Gideon
I agree. A high memory consumption is not the same as a memory leak.
Pete
A: 

I agree with Will completely - however I offer 2 small pieces of advice:

  1. Don't use GC.Collect. Trust me you are not going to improve the CLR's Garbage Collection algorithm by doing this. In fact objects that are ready to be collected but can't be for some reason will be moved to Tier 2, where they will have to wait even longer before being collected (thereby potentially making any leak you may have worse!)
  2. Check everywhere in your code that you have created some type of stream (usually MemoryStream) and verify that the streams are being closed properly (preferably in a "finally" block)
free-dom
A: 

Here I am using "log4net-1.2.10" for handling exception, "Lucene.net-2.1.0.3" for indexing, here is one "IndexWriter" class for add document in index or delete documents from index. lock (object) {
indexWriter.AddDocument(document);// indexWriter object of "IndexWriter" class.
}. Also we use microsoft message queue for retrieving messages.

A: 

@ Brian Gideon

It is rising trend.

A: 

I had the same problem until I changed the applcation from STA to MTA:

[MTAThread] public static void Main()

instead of [STAThread] public static void Main()

(I've used Red Gate's ANTS profiler...)

Trond Ivar