Try to clarify the GC of. NET (1)

Source: Internet
Author: User
Tags compact

What is a GC?

GC (garbage Collection) is a mechanism for recovering unused memory in memory management, and our well-known Java and. NET have their own GC mechanism, which is part of memory management.

Why do you have a GC? Because dynamic memory allocation and distribution of the operating system is regardless of the language itself, such as C and C + + itself need to manually manage the allocated memory resources, if not manually released, it will cause the already useless memory can not be used by the operating system, known as the memory leak.

. NET GC is all happening in the heap, because this dynamic memory is allocated on the heap. Why does. NET not provide manual management of memory like C + +? Because the manual management of memory is very prone to problems, developers should not spend time on this, avoid human problems, to find a management memory of the "person" to deal with these things, so the GC appeared (say a word, before oneself is C, C + + origin, really a language affect a person's knowledge breadth and depth), We all don't have to think about these things, focus on the important things, like you find a housekeeper, but professional, can not make mistakes of the kind, worry.

What are the GC classifications?

What I learned was: Reference Count, Mark and Sweep (upgraded mark and Compact), Copy and Collection
Now that Java and. NET are using the mark and compact algorithm, the algorithm evolved from the mark and Sweep algorithm, which tells Mark and Sweep.

Mark and Sweep: Divided into two stages, the first stage marks all the objects that are now available, and the second phase clears the memory outside of the tagged object.

In. NET, the GC manages a set of roots (made up of global objects), tags the surviving objects in memory by traversing through all the child objects referenced by the root machine, and then clears the objects that are not marked as surviving. This is the mark and sweep algorithm, but this caused a problem, that is, the memory after the recovery is a sieve, this time if a large object to allocate memory, then the total amount of free memory is greater than the size of the allocated object, but can not find a contiguous size of the object to accommodate the memory, What about this time? In fact, simulating the operating system, and then do a memory management mechanism on the line, in a logical view of continuous on the line. Of course this is not the object of this discussion, and the Mark and compact solves the problem of memory discontinuity because it makes the memory a tidy (moving the non-contiguous memory to a piece, looking at the continuous)

Mark and Compact: The memory is done on the basis of Mark and sweep, because when the memory is done, the reference to the object cannot be used, the reference address will change, so, when the GC is used, the thread that uses these objects will be suspended waiting, It is also not possible to recycle memory often, otherwise the performance is worrying, because the recycling cause hangs.

What are 0-generation, 1-generation, 2-generation objects?

To explain this, we have to start with the memory recovery time, there is a hypothesis (in fact, the rule) to reclaim a memory of all pairs of time greater than the time of the memory part of the object, so the memory of the object into a few generations, 0-generation object refers to the most recently allocated memory objects, one analogy. In fact, for many generations, this is determined by the GC, in. NET the algebra in the GC is 3 generations (this value is temporarily uncertain whether it can be changed).

How does GC manage generational objects? In general, the allocated object is a 0-generation object, when allocating object memory, if the memory of the 0-generation object cannot accommodate the new object (more than the upper limit of the 0-generation object memory), after the GC is reclaimed 0 generations, this surviving object algebra plus 1 ( GC.Collect();GC.GetGeneration(obj) code validation, It is unclear whether the automatic trigger is recycled once plus 1), and if the 1-generation object exceeds the upper limit of the 1-generation memory, it will also trigger the GC to reclaim the 1-generation object. So this is what this memory recycling is about? Not necessarily, after all, Microsoft provides the ability to manually trigger GC, that is GC.Collect() , interested in this method can be turned over.

The size of the algebra, after looking at a lot of information, only found an article that,. NET in the 0 and 1 generations of the 16mb,2 memory limit is very large, specific to the framework version and other factors determined.

//验证回收一次,对象就升一代Object obj=new Object();Console.WriteLine(GC.GetGeneration(obj));GC.Collect();Console.WriteLine(GC.GetGeneration(obj));GC.Collect();Console.WriteLine(GC.GetGeneration(obj));
Finalize, what is Dispose? How to understand?

. NET has a taxonomy of managed and unmanaged resources, managed resources. NET can manage, unmanaged resources on its own, requiring a special approach, that is, when managed resources are in the GC,. NET can identify itself, but unmanaged resources, the GC is automatically released.

What is a non-managed resource? This reminds me of the time before using MFC to write Windows programs, what paint brushes, brushes, com, and so on, is unmanaged resources, as well as database connections, files, sockets and the like, oh, there are streams and so on.

These unmanaged resources, generally need to release their own resources,. NET provides a idsiposable interface, the method of implementing this interface, in which resources are freed, using a using statement to simplify the release of this unmanaged resource.

Finalize: This can also be used to release unmanaged resources, the difference between the Idsiposable interface is that the timing of its invocation is uncertain, because it is called by the GC, the GC call is really uncertain, because the invocation of a GC call will reduce program performance (previously said, memory compression causes the reference to change, And because the thread hangs), here's why it's called by the GC.

When creating an object, the object reference to the destructor (after compilation, which is the Finalize method, which is different from the destructor in C + +) is stored in a list called the finalizer queue, and in GC, if an object is useless, And there is a reference in the finalizer queue, which is not recycled, and will move the reference from the finalizer queue to the list of the freachable queue, and the freachable queue's list will start a thread after it has content. The destructor of the referenced object is then executed, and the object's reference is deleted after execution, and the object is recycled until the next GC.

So the hallmark of finalize is:

    • When to call the variable
    • This type of object requires at least two GC to be recycled.

Why at least two times, not two times, because. NET gives us a way to put object references back into the finalizer queue, GC. ReRegisterForFinalize (), if you call this code in finalize, you're not going to die.

Microsoft does not recommend using the Finalize method, as mentioned in other blogs, we can leave it behind, in case the unmanaged resource does not release, it can be the last insurance in the Finalize method (to avoid human reasons).

I did release the unmanaged resources, just don't want to implement the Finalize method, Microsoft also provides the method: GC. SuppressFinalize (this), after this method executes, removes the finalizer queue from the reference to this object.

What is a loh?

The LOH (large Object heap) is a heap specifically designed for large objects, and how many objects will be allocated to this heap? More than 85,000 bytes will be. In fact, this Loh cause large object movement is very time-consuming, rather than move, for example, 3 objects Abc,ab object takes about 80 bytes, c object occupies 10,000 bytes, if the Ab object is recycled, then in the moving phase, 10,000 bytes, moving forward 80 bytes, It's not as good as moving. This 85000 byte is also an empirical value.

Since the Loh cannot move, it must not be moved in the mark and compact (it is not clear what algorithm to use, guess is Mark and Sweep, perhaps a special case), and is recycled only if the 2-generation object is recycled.

GC mode?
    • Workstation mode: Used in a single-processor system, frequently recycled, which prevents a long-time collection of the program's suspend time.
    • Server mode: In a multiprocessor system, create a GC Heap for each processor, which is characterized by a large allocation of memory, can not be recycled without recycling, the payback time is too long.

There are concurrent GC working methods, wherein workstation mode and server mode can be configured, on a single processor set to True also does not take effect, mainly for the user thread in the GC can be most of the time and GC thread concurrency, For details, refer to: https://blogs.msdn.microsoft.com/seteplia/2017/01/05/understanding-different-gc-modes-with-concurrency-visualizer/

When do I need a manual GC?

Resources are especially stressful when, for example, a company that was interviewed earlier, the system is on Azure, the memory of what is particularly expensive, this time the manual GC may be one of the means.

At last

In fact, the most time-consuming GC is the choice of algorithms, such as the mark and compact memory merge into a continuous, this is time-consuming, if enough memory, there is no need to consider moving memory.
Or like my premise, and then in memory to do a memory-mapped management, but also to avoid memory discontinuity problem, of course, will encounter a variety of problems.

Try to clarify the GC of. NET (1)

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.