C # GC. Collect ()

Source: Internet
Author: User

In C # Write a use of the Ice Component interface Communication Service program, the program runs very normal, but when the client calls the ice interface, there is a large amount of data loss, and occasionally communication is not, the most obvious service side is the Telnet service communication port (CMD window flashed over), After a lot of time of the tracking test, and finally only through the history of TFS on the record to restore the restoration, the final problem is located in the Gc.collect (); This code: Most of the interface is the existence of this code for memory recycling, and this sentence off, the communication and data transmission are normal!

MSDN explanation for forced garbage collection :

GC is provided by the garbage collection GC class. Collect method, you can use this method to give your application a certain degree of direct control over the garbage collector. In general, you should avoid invoking any recycling methods to let the garbage collector run independently. In most cases, the garbage collector has an advantage in determining the best time to perform a collection. However, in some infrequently occurring situations, forced recycling can improve the performance of your application. GC is used in this case when there is a significant reduction in the amount of memory used on a certain point in the application code. The Collect method may be more appropriate. For example, an application might use a document that references a large number of unmanaged resources. When your application closes the document, you are fully aware that the resources used by the document are no longer needed. For performance reasons, it is meaningful to release these resources all at once. For more information, see GC. Collect method.

before the garbage collector performs a collection, it suspends all threads that are currently executing . If the GC is called multiple times unnecessarily. Collect, this can cause performance problems. You should also be careful not to call the GC. The Collect code is placed in the program where the user can frequently invoke the point. This may weaken the role of the optimization engine in the garbage collector, and the garbage collector can determine the best time to run a garbage collection.

Also do some extended reading:

How GC Works

First we need to know when the objects in managed code are recycled (unless you use Gc.collect to force GC recycling, which is not recommended, as explained later). The GC performs a collection when it is "happy" (for many reasons, such as insufficient memory.) This is done to increase the efficiency of memory allocation and recycling. So what if we use destructor? Also not, because. The concept of destructor in net is no longer there, it becomes finalizer, which will be mentioned later. For now, remember that an object can be recycled only if there is no reference to it. To illustrate this, take a look at the following code: [C #]

Object Obja = new Object ();

Object OBJB = Obja;

Obja = null;

Forced recycling.

Gc. Collect ();

Objb.tostring ();

[Visual Basic]

Dim Obja as New Object ()

Dim objb as Object = Obja

Obja = Nothing

' Forced recycling.

Gc. Collect () objb.tostring ()

The object referenced here Obja is not recycled because there is another reference to this object, OBJB. Objects are recycled after they have no references. When GC is recycled, it does the following steps: Make sure that the object has no references. Checks whether the object has records on the finalizer table. If there is a record on the finalizer table, move the record to a different table, where we call it Finalizer2. If there is no record on the Finalizer2 table, then free memory. The finalizer of an object on a Finalizer2 table is removed from the table after it is executed on another low-priority thread. When the object is created, the GC checks to see if the object has finalizer, and if so, adds a record to the finalizer table. The record we are talking about here is actually a pointer. If we take a closer look at these steps, we will find that the finalizer object will not be recycled for the first time, that is, the finalizer object will be recycled more than once collect, so it's a slow step. So the author recommends that unless it is absolutely necessary not to create finalizer. In order to prove that the GC does work this way rather than the author's nonsense, we will give an example in the Resurrection chapter of the object, seeing is believing, and hearing is false! ^_^ GC uses the concept of generation in order to improve the efficiency of recycling, so the principle is that the object created before the first collection belongs to generation 0, and after that, the generation number is moved backwards each time it is recycled, which means The second time the original generation 0 becomes Generation 1, and the object created after the first collection and before the second collection belongs to generation 0. The GC will first try to recycle objects that belong to generation 0, because these are the most recent, so it is most likely to be recycled, for example, in some functions where local variables are not referenced when exiting a function (can be recycled). If enough memory is reclaimed in generation 0, then the GC will no longer be recycled, and if it is not enough, the GC will try to recycle in Generation 1 if it is not enough to recycle in Generation 2, and so on. Generation also has a maximum limit, depending on the framework version and can be obtained with gc.maxgeneration. After the memory is reclaimed, the GC will rearrange the memory so that there are no spaces between the data, because the CLR allocates memory sequentially, so there is no free memory between the memory. Now we know that every time we recycle, we waste a certain amount of CPU time, which is what I'm talking about. Don't manually gc.collect the reason (unless you're like me, write some examples of GC! ^_^).

C # GC. Collect ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.