Using the garbage collection?

I want to know what action is performed when we call the Dispose () method. An object quickly frees all resources by calling Dispose () or Dispose (), which Object is ready to collect garbage from. And what happened when we set the Object reference to NULL. Actually, I have a Windows Form application in .NET 2.0. And I want to call the garbage collector after a certain time has passed (Example after 5 minutes) to collect all objects without links.

+7
garbage-collection c # dispose
source share
9 answers

There is nothing magical about the Dispose method, like any other method. Calling the Dispose method does not perform any cleanup in the background or does not change the state of the object; it simply does what you put in the method. The peculiarity of this is that it is defined in the IDisposable interface, so it is a standard way to report an object in order to clear its resources.

In the Dispose method, the object must take care of all unmanaged resources, such as database connections and Font objects.

When you free an object, you do not need to worry about any managed resources. A structure like an array of bytes is completely handled by the garbage collector, and you can just leave it in the object when you release it. You do not need to set null references, if you no longer use the object, the garbage collector will find the best time to delete it and any objects to which it refers.

The garbage collector usually works best when you leave it alone; there is no need to tell it when it should collect an unused object. He alone will figure out when to do this, and usually it does it better than you, because he has access to a lot of information about the state of memory and the state of the machine that your code does not have.

You may feel that you should try to maintain low memory usage, but there is no advantage. The computer does not work better because it has more free memory. On the contrary, if your code tries to perform a cleanup or causes the garbage collector to perform any operations, it performs the cleanup when it needs to do something more important. The garbage collector will clean an unused object, if necessary.

+7
source share

Dispose typically frees unmanaged resources belonging to an object . Calling Dispose does not cause garbage collection; your object is collected when there are no more references to it, just as if you had never called Dispose .

Setting the object reference to null simply means that the reference no longer points to this object; you usually do not need to do this (for example, you almost do not need to set local variables to null ).

You almost never need to start garbage collection yourself. You see a problem that suggests that you need to start garbage collection every 5 minutes, and not at the moment when the runtime chooses?

+9
source share

An object is intelligent for the GC if it has no references to it. If he has links held against him for a certain period of time (managed by the garbage collector), he begins to advance through the so-called โ€œgenerationsโ€.

The Dispose method is just a template (and not a mechanism for deleting an object in the language) to say that the object can now clean up any unmanaged resources, close any connections, etc.

The actions performed by the Dispose method are completely controlled by the programmer, since you know that nothing can be done.

The garbage collector is not interested in Dispose methods, but through Finalizer its likelihood that the Dispose object will be called in any case is people following the IDisposable template that implement the finalizer as the last ditch ", you forgot to call Dispose so I will do it right before how the GC will kill me with a "way to clean resources."

Note that all managed resources will eventually be GC'd (if links are not saved) if Dispose is not called. However, unmanaged resources will be restored only after the completion of the entire application process (as well as with any managed resources with saved links).

+2
source share

Other people here have answered enough "how IDisposable interact with the garbage collector" and "when should I call GC.Collect " part of the question.

I have a blog post detailing when you should set the variables to null to help the garbage collector (third part of the question). The short answer is "almost never, unless it is a static variable."

+1
source share

The point of automatic garbage collection is that you do not need to worry about freeing objects . Do not try to โ€œhelpโ€ the garbage collector.

Objects collect garbage in the end after they go beyond. It is not possible to "free" an object manually or mark it for faster garbage collection. Just keep in mind which objects are in scope (or refer to objects in a scope).

The Dispose method is just a method. It has nothing to do with garbage collection. "Dispose" is the name of the method invoked by the using statement:

 using (var x = expr) { ... } 

basically equivalent

 var x = expr; try { ... } finally { x.Dispose(); } 
0
source share

For class objects, you determine what Dispose will do. You implement an IDisposable interface that contains the Dispose method and is implementation dependent. But usually Dispose goal is to free resources (managed / unmanaged) and create an object as a candidate for the GC .

As for setting null , we can say that it is useless. It simply notes that there is no reference to the object, so it becomes a candidate for the GC , but again, there is no need to set it to null , because the GC can detect that there are no references to the object without it.

It is not recommended to call Collect (until you have an extreme need and arguments for this), since the GC is optimized to find out what time is right for Collection .

0
source share

Dispose, if implemented correctly, will dispose of any managed resources that it has that implement IDisposable and immediately release any unmanaged resources. The object itself will be marked for collection if there are no links on it. Usually this object is used in using the block, and the Dispose method is called at the end of the block automatically, while the object goes out of scope and at that time had the right to collect.

In the general case, you will cause more problems than you solve by interacting directly with the garbage collector. If you feel that you must do this, it is rather a signal that you need to reorganize your decision, i.e. You have too many connections between your classes, as a result of which the object remains alive longer than necessary. A cleaner architecture can lead to better management of the life cycle of an object by the system.

See MSDN for a discussion of how to implement the IDisposable template .

0
source share

An object is marked only for garbage collection when Dispose () is called. <- Update: this is wrong. Dispose () does nothing if it is not called by you or the compiler (when used in the "use" of the construct).

From MSDN -

In most cases, the garbage collector can determine the best time to run the collection, and you must let it work independently. There are rare situations where forced builds can improve your application performance. In these cases, you can induce garbage collection using the Collect method to force garbage collection.

See this article - http://msdn.microsoft.com/en-us/library/bb384155.aspx

-one
source share

If you really want to force garbage collection:

 GC.Collect(); 

Please see this post for reasons why you should not use it:

What is wrong with using GC.Collect ()?

-one
source share

All Articles