CSharp/C# >> garbage collect--is a bug?

by Michael C » Sun, 31 Aug 2008 09:25:24 GMT

"chinasprit" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
> static tc gto;
>
> public class tc
> {
> public int a=99;
>
> ~tc()
> {
> a=-1; //set breakpoint 1
> gto=this;
> }
> }
>
> private void button1_Click(object sender,EventArgs e)
> {
> tc to=new tc();
> GC.Collect();
> GC.WaitForPendingFinalizers();
> return; // set breakpoint 2
> }
>
> two problems:
> click button once,it seem the ~tc() not execute at all.and it will excute
> at
> the second time clicking.why?

I remember reading something about the GC disposing objects on the first run
(if they have IDispose) and destroying them on the second run. If that is
the case then your second point makes sense because you're looking at 2
different objects and the second object has not been disposed yet.

> the second clicking,breakpoint 2, "gto" and "to", gto.a=-1,while
> to.a=99,why?




CSharp/C# >> garbage collect--is a bug?

by Jon Skeet [C# MVP] » Sun, 31 Aug 2008 14:19:35 GMT



The garbage collector itself doesn't know about IDisposable at all. It
calls finalizers, which may in turn call Dispose, but it doesn't call
Dispose directly itself.

--
Jon Skeet - < XXXX@XXXXX.COM >
Web site: http://www.pobox.com/ ~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

CSharp/C# >> garbage collect--is a bug?

by Gan Andersson » Sun, 31 Aug 2008 17:16:23 GMT


Not exactly.

If an object that is unreachable has a finalizer, it will be placed on
the freachable queue, where a background thread will execute the
finalizer. After the finalizer has been run, the object can be collected
just like any other object.



--
G鰎an Andersson
_____
http://www.guffa.com

Similar Threads

1. ?garbage collect??is a bug? - CSharp/C#

2. WIll GC.Collect() immediately collect garbage ?

WIll GC.Collect() immediately start a GC thread and clear all unreferenced 
objects from the heap ?

Regards,

Chak. 


3. callback on a garbage collected delegate error - CSharp/C#

4. Garbage Collecting and real-time processing.

Hello, can someone please give me a tip or two on the following problem:

I have a system of linked objects which stores properties of on-screen drawn objects. When i select each object on the screen the application slowly over 10-20 seconds grows slower, and the computer runs out of memory so the disk starts spinning and the garbage collector does its thing, problem is that the applicatoin requires a bit more real-time processing and the user gets a frozen screen for 2-3 seconds at a time if not more.

Should i call the following code before someone clicks on my on-screen object so that i can release any previous allocated memory? 

GC.Collect();
GC.WaitForPendingFinilizers();
GC.Collect();

Is this the only way to "Hope/pray" for a speed up?

5. Does this code Force Garbage Collector to Collect - CSharp/C#

6. Object referenced by events can be garbage collected !

I have created a sample project where i have referenced an object only with 
an event :
textBox.VisibleChanged += new EventHandler(this.textBox_VisibleChanged);

When i call GC.Collect(), the object is disposed and garbage collected ! I 
thought that event references are strong references and not weak references. 
Does the .NET 2.0 garbage collector manage differently the events references ?

7. Objects don't get Garbage Collected - CSharp/C#

8. How to garbage collect an object?

In my quest to migrate a vb.net app to C# I encountered 
the following problem:

session.Initialize("pwrd");        
NotesDatabase NotesDB;
NotesDB = session.GetDatabase(server, filename, false);
...
//done with data pulling/processing
NotesDB = Nothing  //from vb.net - not working for C#

What should I do about NotesDB = Nothing?  If I set 

NotesDB = null;

C# doesn't complain, but is this the way to do it?

Thanks,
Ron