1

In my previous post, i posted a error "Load report failed" when i do bulk pdf(more than 1000 pages) generation. Then i gave obj.Dispose() it's fixed my problem. So, can any one tell me what is the actual functionality of Dispose() and Garbage collector in dot net.

Thanks, saj

GvS
  • 52,015
  • 16
  • 101
  • 139
selvaraj
  • 889
  • 2
  • 16
  • 29
  • You can't rely on the GC for **everything**, just like you can't rely on your parents, your maid or your butler for **everything**. – BoltClock Mar 03 '11 at 12:57
  • 2
    possible duplicate of [C#: do you need to dispose of objects and set them to null?](http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null) – GvS Mar 03 '11 at 12:58

4 Answers4

1

The .NET garbage collector manages the memory of managed objects (native .NET objects) but it does not manage, nor is it directly able to clean up unmanaged resources. Managed resources are those that are cleaned up implicitly by the garbage collector. You do not have to write code to release such resources explicitly. In contrast, you must clean up unmanaged resources (file handles, database collections, etc.) explicitly in your code.

Nikhil Vaghela
  • 920
  • 2
  • 11
  • 36
  • Okay, but if I can write an object with getters/setters that get and set data in an unmanaged resource and then dispose of the resource, why can't Microsoft? – Erik Reppen Jun 21 '13 at 21:47
1

The garbage collector only works on managed objects. Dispose is used when you have unmanaged memory allocated by a class because it needs to be freed manually when the class is not in use. If you don't implement the dispose pattern (which it seems you do), then the memory is never released and you have a memory leak of the unmanaged memory. If you do but don't call it, you are dependent on the garbage collector running (and invoking Dispose) to release the unmanaged memory, this may or may not happen within the timeframe you need. It's (almost) always best to wrap objects of classes that implement IDisposable in a using statement to guarantee that the Dispose method is called when the object goes out of scope.

using (var obj = new DisposableObject())
{
    ...
}
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
0

See this question: How to dispose a class in .net?

IDisposable has nothing to do with freeing memory. IDisposable is a pattern for freeing unmanaged resources...

Community
  • 1
  • 1
rick schott
  • 21,012
  • 5
  • 52
  • 81
0

Any objects implementing IDisposable shold have Dispose() called on it whenever that object is no longer needed. This method is responsible for freeing up system resources that were allocated. This pattern provides an eager collection of the object.

It is a good practice to envolve the code block instantiating and using a disposable resource with the snippet:

using (var disposable = new Disposable())
{
    // your code goes here
}

It is not know when GC would run, that it could be keeping resources longer than necessary.

André Werlang
  • 5,839
  • 1
  • 35
  • 49