473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Memory accumulation/leak in .Net Queue class

Hi All,
We are observing a serious issue with the memory usage of Queue and its very critical issue which needs to be fixed.

We have an application which runs two threads i.e. a Producer and a Consumer thread. The Producer thread (Enqueue) inserts some heavy user defined object in the Queue. The Consumer thread (Dequeue) deletes one object from the Queue and processes it.

When we monitored the memory usage of this application in Task manager, we observed that there is a continuous rise in memory usage. After thousand insertions in the Queue, the Producer thread terminates and at this point the memory usage becomes constant. After deleting all the elements from the Queue, the Consumer thread also terminates.

However we observed that the memory usage for the application doesn't drop to the start value from where it has started even after removal of all elements from Queue. I implemented Dispose () for user defined classes and called GC.Collect (), GC.WaitForPendingFinalizers () and so that garbage collector should free all the memory taken up by the Queue. However after doing this, there is no drop observed in memory usage.

We also used ANT memory profiler tool in order to detect if there are live object references present in the queue, but the tool didn't showed any such live references.

One strange behavior we observed is, when the container object which has Queue object as a data member goes out of scope, Dispose () method does not get called for container object.But just before the termination of application, Dispose method gets called. Why the memory does not gets released or collected by GC when object goes out of Scope?

Is there any known memory accumulation problem with the Collection (i.e. for list, Queue and Arraylist) classes of .Net framework?
Sep 13 '08 #1
2 4074
debasisdas
8,127 Expert 4TB
Hi Manoj

Welcome to Bytes.com (our new identity)

Feel free to post questions in the related forum only by choosing from under the blue bar on the top of your page.

Please follow the posting guidelines in every new post/reply.

Please specify your post/question clearly.

Regards
Debasis
Sep 13 '08 #2
vekipeki
229 Expert 100+
If your class implements IDisposable, you should always call Dispose() manually.

A common misunderstanding is that GC will call your Dispose() method automatically -- actually no, it will only call Dispose() when you are enclosing your code in a C# using keyword.

One other thing that might be preventing GC from collecting your object is if you aren't detaching your event handlers (this does not count for event handlers added using Windows Forms designer, but counts for all other event handlers that you add inside your code).

So, for each

Expand|Select|Wrap|Line Numbers
  1. object.SomeEvent += new EventHandler(object_SomeEvent);
you should have a

Expand|Select|Wrap|Line Numbers
  1. object.SomeEvent -= new EventHandler(object_SomeEvent);
just before calling object's Dispose method.

I am used to putting all my handlers in two private methods for all my objects, AttachHandlers and DetachHandlers. The first one is usually called in a constructor, the other one just before disposing. Every event that is attached in a first method, must be detached in the second one.

For debugging, you can also add something like:

Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine(this.ToString() + " disposed");
inside all your object's dectructors, just to check when your objects get disposed. Once you are sure that you are disposing properly, you should get rid of all forced GC.Collect() calls that you manually added and let the collector do it for you.
Sep 15 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
29
by: Tuvas | last post by:
I have a function in a program that works something like this. def load_pic_data(width,heigth,inpdat, filt=TRUE): data='' total=0 tnum=0 size=100 for y in range(0,heigth): row='' for x in...
2
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
3
by: Godzilla | last post by:
Hello, I have a program that create and pop an object off a queue, but it is experiencing some memory leakage. I have been unable to detect where the memory leakage occur. The strange thing is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.