0

Can we not have a GC.Collect() like this in Dispose, so that if we want our object in the class to be disposed immediately.

public void Dispose()
{
    Dispose(true);
    GC.SupressFinalize(this);
    GC.Collect();
}

I saw some other article in stackoverflow here, but could not get a clear explanation.

BeeGees
  • 451
  • 3
  • 10
  • 4
    That sounds like a really bad idea. Forcing a full garbage collection after each instance of your object is disposed will probably counteract whatever you are hoping to achieve by doing this – UnholySheep Apr 24 '20 at 11:31
  • Then can we specifically tell GC to collect only This object. Can we specify the object. – BeeGees Apr 24 '20 at 11:37
  • 1
    No, that is not how garbage collection works. What exactly are you trying to achieve here? – UnholySheep Apr 24 '20 at 11:40
  • I have opened a fileStream object in one class and i want to make sure it immediately gets disposed. – BeeGees Apr 24 '20 at 11:43
  • 1
    Disposing an object and having it be garbage collected are two separate things. That's why you have the `Dispose` method - to make sure the object doesn't hold any unmanaged resources anymore – UnholySheep Apr 24 '20 at 11:45
  • Is the _filestream object an unmanaged resource. I think an unmanaged resource is like a pointer ptr or Handle. Please shed light on it. I thought managed resource object is the one which has IDisposable implemented within it. – BeeGees Apr 24 '20 at 11:50
  • 1
    No, a filestream is not an unmanaged resource. It is a managed resource, and you are supposed to dispose it from inside of [your `Dispose` method](https://stackoverflow.com/a/898867/11683), which will close it as soon as your object is disposed. – GSerg Apr 24 '20 at 11:50
  • Ok. And if we have a _sqlConnection of SqlClient connection. Can we call a close on that in our Dispose. Is that allowed for a connection. – BeeGees Apr 24 '20 at 11:54
  • If you require something to be "immediately disposed" for some reason, you should not allocate it on a managed heap in the first place. The GC is undeterministic and there is no bulletproof way of telling it to dispose a particular object on demand. – mm8 Apr 24 '20 at 11:56
  • 1
    You should call `Dispose`. From inside of your `Dispose`. From the ["get rid of managed resources" branch](https://stackoverflow.com/a/898867/11683), if you are using the full Dispose pattern. – GSerg Apr 24 '20 at 11:57
  • How can we not allocate to managed heap. Because when we open a connection object, it automatically goes to the heap as it is a reference type object. – BeeGees Apr 24 '20 at 12:00
  • 2
    You might find [Everybody thinks about garbage collection the wrong way](https://devblogs.microsoft.com/oldnewthing/20100809-00/?p=13203) by Raymond Chen helpful. – Damien_The_Unbeliever Apr 24 '20 at 12:20
  • @BeeGees: That's my point. You shouldn't allocate instances of reference types if you don't want to put pressure on the GC. Maybe you could use an object pool to solve whatever your issue is or maybe you should simply consider not using a managed runtime at all? – mm8 Apr 24 '20 at 12:26
  • @Damien_The_Unbeliever thanx for the article. – BeeGees Apr 24 '20 at 12:32

0 Answers0