Susanta
Susanta

Reputation:

Memory leakage in c# windows service multithreading application

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?

Upvotes: 0

Views: 4323

Answers (6)

Trond Ivar
Trond Ivar

Reputation:

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...)

Upvotes: 0

Susanta
Susanta

Reputation:

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.

Upvotes: 0

apiguy
apiguy

Reputation: 5362

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)

Upvotes: 0

Brian Gideon
Brian Gideon

Reputation: 48959

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.

Upvotes: 1

Rowland Shaw
Rowland Shaw

Reputation: 38128

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

Upvotes: 4

user1228
user1228

Reputation:

.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.

Upvotes: 2

Related Questions