C + + Essay: GC exploration of. NET CoreCLR (2)

Source: Internet
Author: User

First of all, thank @dudu and @ Zhang Shanyu the 2 great gods can subscribe to me, I was writing this series before, I have been writing some core and the bottom of the knowledge skeptical, why am I skeptical? Because the general writing high-level language people 99% will not touch the bottom, in fact, I did not look at these things, just because I feel interested in C + +, simply disorderly write something, if there is not well written place, also please above 2 big God pointed out.

I'm actually writing C + + now, but I'm going to merge some of the base class libraries of C + + and. NET in the back, and I find that writing the CLR is especially rare, and I don't know why. Anyway, not much nonsense, start today's writing, today is still focused on the GC above.

First of all, to share with you a funny thing I met this time, is the following things, the following things I see the first time, but I can be sure that the GC operating mode has the following. Here are the comments, I will be an unknown, but to tell you one of my little discovery: I guess, the following code only run under the 64-bit GC, this sentence sounds very awkward, but how did I get this conclusion?

// !!!!!!!!!!!!!!!!!!!!!!! Make sure-the DEF in Bcl\system\gc.cs//If your change this!//the meaning of the above is that if you are changing the value in this, you must first alter the BC1\SYSTEM\GC.C s inside the definition//collector run mode enum collection_mode{    collection_non_blocking = 0x00000001,//non-locking mode    collection_blocking = 0x00000002,  //Lock mode    collection_optimized = 0x00000004,//optimize mode    collection_compacting = 0x00000008//Minimum adaptation mode #ifdef STRESS_HEAP//If STRESS_HEAP is defined, I translate it here as a pressure heap, which is "into the heap"    , collection_gcstress = 0x80000000// Here I think it should be a condition that the GC can get into the heap, though I don't know what this means. #endif//Stress_heap};

  

Although I'm not entirely sure I'm right, but one thing is certain that it generates x64, a file that ends with I in the Windows kernel, we can interpret it as a temporary file, and the relationship between GC and Windows memory is like a homestay relationship, A GC is something that relies on the Windows kernel to survive (it's all a personal guess).

So, what is the role of these ENMU types of fixed values in GC? I randomly looked for a few variables, because these fixed values, in fact, in the GC, you can understand that it is ubiquitous, the equivalent of the usual code to write if else as ordinary, you know the usefulness of these variables on the line, do not have to tangle with the code is what the meaning, because later I will take you slowly study.

If defined as a background GC
#ifdef BACKGROUND_GC If (recursive_gc_sync::background_running_p ()) {//If mode (which can be understood as a variable temporarily)//collection_ Non_blocking value is 0x00000001, that is to say mode&collection_non_blocking in fact, as long as the right one is OK, that is, mode=1 or 4 if (mode = = collection_optimized) | | (Mode & collection_non_blocking)) { return S_OK; } if (Mode & collection_blocking) { pgengcheap->background_gc_wait (); if (Mode & collection_optimized) { return S_OK; } } }

Today I do not want to waste too much time, I am going to pick a representative code snippet to take everyone to understand the GC, so-called GC,GC is what, then it must have a collect method; Well, let's take this method of surgery. First we must know a few knowledge points, for non-C + + programmers, also need to learn a little something, size_t explanation, here is a simple point is long unsigned int, why I deliberately put long red it? Because our GC uses a method that runs in a 64-bit environment, if it is 32-bit, then it is unsigned int.

Because this code is very long, so I intend to: first of all, and then the rest of the parts, or even if you look at the comments, will be dazzled.

//function returns a value. If this function is returned, it will contain meaningful data, and if returned immediately contains the status information-send success or not-from Baidu Hresult//generation (generation, such as you and your parents are 2 generations)//gcheap:: Garbagecollect (int generation, BOOL low_memory_p, int mode) {//If you have defined a 64-bit ...    That is, the CLR of this GC is the # if defined (BIT64) running on the 64-bit runtime///If the weak storage pointer PS is defined: I don't know how to translate, simply translate low to weak. if (low_memory_p) {//1. Initialize: All allocated space size_t total_allocated = 0;//All "Want" allocated space size_t total_desired = 0;// If you define a complex heap (my own translation) #ifdef multiple_heaps int HN = 0;//n_heaps This definition I did not find, but I can be sure that this must be related to the multiple_heaps//You can interpret this as Mul Tiple_heaps.            Length for (hn = 0; hn < gc_heap::n_heaps; hn++) {//The first is to define a "temporary" pointer to HP, so that each item in the GC heap points to the variable.            gc_heap* hp = gc_heap::g_heaps [hn];//add up each piece of the quantity that you want to allocate. Total_desired + = dd_desired_allocation (hp->dynamic_data_of (0));//Total allocation = Want to allocate-new assigned total_allocated + = Dd_des        Ired_allocation (hp->dynamic_data_of (0))-dd_new_allocation (hp->dynamic_data_of (0)); }

There are a few basic points that you need to learn from the code above:

One is the Dynamic_data_of method, which returns a pointer variable for the Dynamic_data class, and we victory to see this method and class.

    Per_heap    dynamic_data* dynamic_data_of (int gen_number);

The first is the method:

To return to Dynamic_data_table's first table, you can compare datasetinlinedynamic_data* gc_heap in. NET::d ynamic_data_of (int gen_number) {    return &dynamic_data_table [Gen_number];}

And then we'll take a look at this dynamic_data_table,

    Per_heap    dynamic_data dynamic_data_table [numbergenerations+1];

The following look at this picture, suddenly I realized that the so-called DataTable, is the dataset we use each time, in fact, the bottom layer is placed on the stack above, in a complex heap of the way to store, and each DataTable, in fact, is the small composition of the stack, not only I understand right.

The following 2 methods in the above code are related to Dynamic_data. The ptrdiff_t type variable is typically used to hold the result of two pointer subtraction operations, which is also required. NET programmers pay attention to, after all, engage. NET is not dealing with pointers.

inlinesize_t& dd_desired_allocation (dynamic_data* inst) {  return inst->desired_allocation;}
inlineptrdiff_t& dd_new_allocation (dynamic_data* inst) {  return inst->new_allocation;}

We should also pay attention to the small details, that is, + = is actually the first + +, minus the newly allocated space.

So much for today, because I have to do other things, and finally sell a xiaoguanzi, those who say. NET no technical content of people, in fact, is not understand. NET principle of the people. The following is a more complex class, which defines the super-many friend class, that is, the GC surface is a piece of code, in fact, this is a heavy aircraft carrier, we need to explore its structure.

Einstein once said a famous saying: The pursuit of truth is more valuable than the possession of truth. What we're going to do is not just a screw.

C + + Essay: GC exploration of. NET CoreCLR (2)

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.