WeakReference memory usage goes up

Use this forum to discuss subjects that don't belong in the other forums.

Moderator: SciTech Software

WeakReference memory usage goes up

Postby WeakReference » Thu Jun 09, 2005 6:21 pm

Hi I am using the profiler to track memory usage in my app. I basically execute a set of instructions in my app for opening and closing multiple customer objects. which each subsequent open and close I observe the managed heap and the Data in Generation #2. I notice that it stays constant except with each open and close the number of WeakReference class instances go up by 48 bytes (total of 3 instances per open and close). I look at the class details for the WeakReference and find that they all have the Target field set to NULL. But however over time the number of WeakReference instances continues to grow so by the time I have openend and closed 500 customers sequentially I have about 240k bytes that are tied up in this.

Can you explain what to do about this? What is the WeakReference class and why does it not destroy itself if the Target is null? Thanks.
WeakReference
 

Postby Andreas Suurkuusk » Fri Jun 10, 2005 12:39 pm

An instance of the WeakReference class contains a reference to another instance, but this reference does not prevent the instance from being garbage collected. When the referenced instance is GCed, the Target of the WeakReference is set to null. The WeakReference instance itself is not affected by this; as long as another instance or root is referencing the WeakReference, it will stay in memory. You need to make sure that the WeakReference instances are not reachable from any root, in order for them to be eligible for GC.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 995
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden

Postby Guest » Mon Jun 13, 2005 2:45 pm

Thanks for the reply. I noticed that the WeakReference is referenced by <root>. There is no other information. when I looked at the stack I found that it usually gets created when the SqlConnection.set_Reader method is called or some other things related to SqlConnection. How do I figure out what is referencing the WeakReference? Its definitely not any code I have written? It seems to be happening in the .NET level somewhere? How can I clear the references? I know I am closing out all datareaders and connections in my code.
Guest
 

Postby Guest » Tue Jun 14, 2005 4:24 pm

To add another note I never explicitly created these WeakReferences in my code. I am not sure where they are coming from and who is creating them? So how do I know which root is refering them?
Guest
 

Postby Guest » Wed Jun 15, 2005 6:41 pm

It's possible that the <root> that references your WeakReferences is the finalization queue. There is an issue when working with SqlConnections, where the SqlConnection hangs when running the finalizer. It hangs because it tries to marshal the call to the thread that created the SqlConnection. What you can try to do is to set the [MTAThread] attribute on your Main method and see if that helps. If I remember it correctly, calling GC.GetTotalMemory from the thread that created the SqlConnection might also help, since it pumps the Windows message queue, allowing the finalizer to run.

Best regards,

Andreas Suurkuusk
SciTech Software AB
Guest
 


Return to General

Who is online

Users browsing this forum: No registered users and 3 guests

SciTech Software logo

© Copyright 2001-2016. SciTech Software AB
All rights reserved.


SciTech Software AB
Kartvägen 21
SE-175 46 Järfälla
Sweden


E-mail: mail@scitech.se

Telephone: +46-706868081

cron