How to stop garbage collector

  • Thread starter Thread starter Ray5531
  • Start date Start date
R

Ray5531

Is there a way in C# to stop garbage collector from killing our object
automatically.I'd like to kill it myself??

Is there away to to do so?
Thanks
 
I forgot to ask what happenes in this scenario.

ArrayList first = new ArrayList();

ArrayList second = first

first.Dispose()

Dose these three lines of code makes second a useless variable poiting to no
where?



Thanks
 
A couple of things to note here.

1. You're missing a semicolon at the end of the second and third lines, so
you won't compile =)
2. The ArrayList object doesn't have a Dispose method.
3. Assuming instead that you set it to null, "first" would then indeed be
null, but "second" would still be the ArrayList instance. Essentially,
you're removing the pointer from "first" to the object. The object doesn't
get destroyed/collected/whatever, because "second" now has a reference to
it.

Also, the garbage collector isn't called "the object snatcher" for a reason.
It's only going to collect garbage (space you aren't using), not stuff that
you are still using.

Hope this helps,

Dan Cox
DISCLAIMER: I didn't write it, just trying to help! =)
 
From MSDN:

In scenarios where resources must be released at a specific time, classes
can implement the IDisposable interface, which contains the
IDisposable.Dispose method that performs resource management and cleanup
tasks. Classes that implement Dispose must specify, as part of their class
contract, if and when class consumers call the method to clean up the
object. The garbage collector does not, by default, call the Dispose method;
however, implementations of the Dispose method can call methods in the GC
class to customize the finalization behavior of the garbage collector.

So, your best bet (as I understand it) is to implement IDisposable on you
class and call the Dispose method when you're ready for your object to be
collected. That WILL NOT force the Garbage Collector to collect your object.

Need more detail? Here's the best I've seen:

http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae

Hope this helps,

Dan Cox
 
Dispose allows an object to release the *non-memory* resources it *consumes* (release native handles and pointers and call Dispose on contained IDisposable objects)

Managed *memory* is only ever reclaimed by the garbage collector

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Accrding to this article :
http://msdn.microsoft.com/library/d...guide/html/cpconimplementingdisposemethod.asp

Dispose(True) ,Managed and unmanaged resources
can be disposed,dose it include the object itself(if there is no reference
to it?
 
Ray5531 said:
Accrding to this article :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu
ide/html/cpconimplementingdisposemethod.asp

Dispose(True) ,Managed and unmanaged resources
can be disposed,dose it include the object itself(if there is no reference
to it?

No. There is no way of manually collecting an object, other than by
calling GC.Collect to force a garbage collection.

Why do you think you need to do it anyway?
 
Hi Skeet.

I was pretty sure that if I raise this duiscussion you would pop up soon.I
still have the discussion about reference types and Value types and your
supper article in that regards as well.You did a great job man! I love your
article.
Jon,by the way,can u tell me why pointers in C# can not refer to reference
types where as in your article I remember that you mentioned a refernce type
can refer to another reference type which mens it simply stores the address
of the other object while its own address is kept on the heap. What's
reallly the difference between Reference types and pointers in C#? only the
fact that pointers are unsafe and reference type are safe?That's it?

Thanks Mr.Memory geek;-)

Reza
 
Ray5531 said:
I was pretty sure that if I raise this duiscussion you would pop up soon.I
still have the discussion about reference types and Value types and your
supper article in that regards as well.You did a great job man! I love your
article.

Goodo :)
Jon,by the way,can u tell me why pointers in C# can not refer to reference
types where as in your article I remember that you mentioned a refernce type
can refer to another reference type which mens it simply stores the address
of the other object while its own address is kept on the heap. What's
reallly the difference between Reference types and pointers in C#? only the
fact that pointers are unsafe and reference type are safe?That's it?

Pointers are indeed unsafe, and aren't tracked by the garbage
collector.

You also can't have a pointer to a managed type - pointers have to be
to value types which don't themselves contain any reference types
(however indirectly).

Personally I don't think I've ever used pointers in C#...
 
Back
Top