"Turn" chat with the interviewer. NET garbage collection, straight stab interviewer G-spot

Source: Internet
Author: User

A loaded interviewer and a loaded programmer

When I interview people, I often follow this path to interview:

Look at your resume and interview questions, find some technical points from your resume and interview questions, and talk to your candidate.

When talking about a technical point, the candidate's answer will involve other technical points, and then I'll write it down and pick up some technical points I'm interested in and continue to talk to him.

Sometimes the candidate will involve a lot of technical points in order to install, he may just know a name and say it.

In this way, it is easy to find the level of candidates, but also to know how much water he provides for the interview information.

---------------------

However, sometimes there are technical points that I'm not familiar with (such as algorithms, graphics, WF),

Then I'll try not to talk about this, or directly say, this I do not understand,

However, there are a lot of interviewers chatting with candidates, in order to install B, will be unrestrained to chat out a lot of their own are not very familiar with the technical points.

In this way the candidates will be able to know very clearly, in the future, he is followed by a kind of boss, in a kind of team mixed.

----------------------

Probably at the beginning of this year, I interviewed a friend, all aspects are also good,

He took the initiative to mention the technical point of garbage collection, so we talked about it on this piece of content.

However, I think the content of his answer, and I want to listen to the content, although the intersection, but most of it is far away (I believe he is more clear on this piece)

It's like a girl who keeps on trying to make you feel bad, and has been writing this article since then.

(Later, my interview advice was to accept the offer, but eventually did not join our team, it is estimated that the personnel or salary is stuck.) )

Can you briefly talk about how garbage collection works?

Run. NET application, the objects created by the program are tracked by the CLR,

Which objects are also used (there is a referential relationship), which objects are no longer used (there are no referential relationships), and the CLR is documented.

The CLR organizes objects that are no longer being used, destroys some of the objects at the right time, and releases the memory occupied by those objects.

-----------------

The above paragraph involves a lot of technical points: (The following technical points, briefly speaking, not in-depth discussion)

How does the CLR Record object reference relationships?

The CLR makes object relationships a "tree", marking their reference relationships

How does the CLR release the memory of an object?

The key technique is that the CLR moves the useless objects together so that the memory is contiguous, and the newly allocated objects are created on this contiguous piece of memory to reduce memory fragmentation (the CLR does not move large objects)

What rules does the garbage collector collect for garbage objects?

The CLR collects objects by the length of time the object has survived in memory.

The shortest time is assigned to the No. 0 generation, the longest is assigned to the 2nd generation, altogether 3 generations.

The general No. 0 loan object is a smaller object, and the 2nd generation object is a larger object

The No. 0 generation of Objects GC collection time is the shortest (millisecond level), the 2nd generation of Objects GC collection time is the longest.

When the program needs memory (or when the program is idle), the GC collects the No. 0 generation of objects first,

When the collected memory is still insufficient, the GC collects the 1th and 2nd generation objects. (The general situation is collected in this order)

If the GC runs past and the memory space is still not enough, then the OutOfMemoryException exception is thrown.

After the GC has run a few times, the No. 0 generation of objects still exists, so the CLR will move these objects to the 1th generation, and the objects of the 1th generation.

Now that you have a garbage collector, why dispose of methods and destructors?

Because of the CLR, the GC can only release managed resources and not dispose of unmanaged resources (database links, file streams, etc.)

So how do you release unmanaged resources?

In general, we will choose to implement the IDispose interface for the class and write a Dispose method.

Let the caller call the Dispose method of this class manually (or call the Dispose method with a using statement block)

This is a good choice because the caller knows when to release the resources.

When this method executes, the destructor and the garbage collector do not begin to handle the release of the object.

-------------------------

Sometimes, we don't want to implement the Dispose method for a type,

We want him to automatically release the unmanaged resources. Then we need to use the destructor.

Destructors are a very strange function, and the caller cannot invoke the destructor of the object, and the destructor is called by the GC.

You cannot predict when the destructor will be called, so try not to manipulate the managed resources that might be reclaimed here, and the destructor is used only to release the unmanaged resources

The GC releases the object containing the destructor, which is troublesome (two dry times to kill her),

The CLR will first let the destructor execute and then collect the memory it occupies.

Do we need to do garbage collection manually? What kind of scenario does this do?

When GC performs garbage collection is a very complex algorithm (policy)

Can probably be described as:

If the GC finds that many objects were collected last time, it frees up a lot of memory,

Then it will perform the second recovery as soon as possible,

If it recycles frequently, but does not release much memory,

Then it slows down the recycling rate.

Therefore, try not to invoke Gc.collect () as this will break the GC's existing execution strategy.

You can manually intervene in the garbage collector's work unless you know very well about the memory usage of your application and when you have a lot of garbage.

I have a big object, and I'm afraid it's going to be a long time before the GC collects him.

Simply talk about the relationship between weak and garbage collection?

Suppose there is a large object that, when used up, does not have a reference relationship (this sentence has changed), it is possible for the GC to collect it at any time and free the memory he occupies

But because it is a larger object, it is likely that in the 3rd generation, it is estimated that GC 1:30 will not collect it.

This object is already in the garbage heap, but I still want to use it, how to do? How do you get it back from the rubbish heap?

This is the time to use a weak reference, to see the following code:

            var BSS = new Bsctl (browsercontainer);            var VBSS = new weakreference<bsctl> (BSS);            BSS = null;            Bsctl OK;                        Vbss. Trygettarget (out OK);            If no garbage collection is done OK will not be null if            (ok = null)            {                //If garbage collection has been done, this code will be executed                OK = new Bsctl (Browsercontainer);            }

Garbage collection can collect BSS objects at any time,

If it is collected, it goes into the IF statement block, and if it is not collected, it does not enter the IF statement block, and Trygettarget (out OK) successfully fished the BSS back from the garbage heap.

Note: here only short weak references, no reference to long weak references, I think long weak references use less scenes (thanks to Shaikanyu imfunny reminders)

The benefits of the garbage collector

Many interviewers love to ask this question, but I never ask,

(In fact, I seldom ask anything about garbage collection, unless the candidate talks about it himself)

Because I don't have a lot of experience in C + + programming,

If you want to talk about the benefits of the garbage collector, it is bound to be compared with lower-level languages such as C + +.

The candidate can probably say that

Reduce memory usage bugs and improve programming efficiency issues

Other

This article refers to the CLR VIA C # Third edition, but also refers to the blog park a friend blog, but the address has not been found

Transferred from: http://www.cnblogs.com/liulun/p/3145351.html

"Turn" chat with the interviewer. NET garbage collection, straight stab interviewer G-spot

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.