C #

xiaoxiao2021-03-06  79

Whether it is a type of variable or a class type variable, the storage unit is allocated in the stack, and the only difference is that the variable of the class type is actually stored in the pointer of the object, which is equivalent to ctype in VC6. * , Just shield the concept of the pointer in the language of the .NET platform. We all know a major feature of the stack is LIFO (then first out), this is just right with the characteristics of the role domain (in the nested level of the scope, the deeper the role of the scope, the higher the priority of its variables) . Therefore, for the problem of the landlord, after "}", whether it is a value type or a class type variable (object pointer) will be released immediately (notably: the object in the hosted stack indicted by the pointer is not Released, waiting for GC's recycling, this may be the landlord to ask). The stack space in .NET is not managed by GC, GC only manages the hosted stack.

The problem of the landlord actually mentioned that the fundamental difference between the Net and the previous VS language is mainly due to the GC exists. I want to explain my understanding:

1. GC only collects objects in the hosted stack.

2. All value types are allocated in the stack, and immediately release the stack space immediately after the scope, this is completely complete with VC6

same.

3. Different types of variables and classes of class types, this is two different concepts. Class type variables are actually the pair

Pointer variable of icon. Such as defined in C # CType MyType; definition in VC6 CType * mytype; is complete

Sample, just .NET language hides the * sign. Similarly to VC6, you must construct an object with the new keyword.

Such as (c #): ctype mytype = new ctype (); actually this statement has two memory allocation, once is classified

Type variable Mytype allocates space in the stack (the space occupied by the type of pointer, 32-bit system, 64-bit

The system allocates 64 bits. In the same system, all the memory spaces occupied by all pointer types are the same.

Regardless of the type of object pointing to this type of pointer), the other is in the hosted stack (managed by GC)

A pile is constructed to construct a CType type object and assign the start address of the object to the variable mytype. Because

This has caused the variable of class types declared in the same scope and the living life of the type of object.

4. The survival of the type of variable is related to the scope defined, that is, "}", it

Engraving smoke elimination. The survival period of the object does not have any inevitable links with the scope of the traditional sense.

Objects defined in any level in the domain nested level (accurately said that it should be constructed) can be student

Any time for the contract is recycled by GC.

5. Objects have been recovered must meet two conditions: First, there is no strong reference to this object (strong quote here

Not detailed); the other is that the GC recycling operation is triggered. These two conditions must be met at the same time, and the object will be recycled.

Normally, the object is recovered by this object after it is used up for a long time. So, .NET

There is no destructor, only the finalize function. The role of the Dispose () function is just to release some resources (such as

Say that open files), and does not destructure objects. And, the use of Dispose () must be by the programmer himself

control.

6. In any case, the object cannot be explicitly recycled. Even if you explicitly call GC, GC is often not so listening.

Recycling of recycling operations. The trigger of the GC recycling operation is based on a certain strategy.

7. It should be said that GC recovery is safe and stable. But its overhead is also very large, the efficiency is not high, especially occupied

There are many memory, although Microsoft has used many optimized measures. Its overhead is only reflected in the GC's garbage collection.

转载请注明原文地址:https://www.9cbs.com/read-92737.html

New Post(0)