Is it bad practice to depend on the .NET automated garbage collector?

后端 未结 3 1635
迷失自我
迷失自我 2020-11-30 14:03

It\'s possible to create lots of memory-intensive objects and then abandon references to them. For example, I might want to download and operate on some data from a database

3条回答
  •  渐次进展
    2020-11-30 15:05

    Ok, time to clear things up a bit (since my original post was a little muddy).

    IDisposable has nothing to do with Memory Management. IDisposable allows an object to clean up any native resources it might be holding on to. If an object implements IDisposable, you should be sure to either use a using block or call Dispose() when you're finished with it.

    As for defining memory-intensive objects and then losing the references to them, that's how the Garbage Collector works. It's a good thing. Let it happen and let the Garbage Collector do its job.

    ...so, to answer your question, No. It is not a bad practice to depend on the .NET Garbage Collector. Quite the opposite in fact.

提交回复
热议问题