CRL is used to manage the memory allocation and recovery issues of the managed heap. The garbage collection mechanism is designed.
First, the reference type allocates memory on the managed stack. CLR assumes that the heap is infinitely large. In the ApplicationProgramInitialize during loading and recycle when disabled.
However, there is a limit to the size of the heap. When the managed heap is used up, CLR starts garbage collection.
CLR stores a list Of all object addresses. It regularly checks the objects in the Table. First, assume that all objects are not referenced.
Then, check whether the object is referenced. If yes, discard it and mark it as garbage. Wait for recycling.
If it is not recycled, It is upgraded to the first generation of garbage.
CLR allocates limited space for the first generation of garbage. When the space is used up, the first generation of garbage is recycled, thus reducing the number of garbage collections.
The remaining objects are upgraded to the second generation.
There are three generations of CLR spam.
Garbage collection frees programmers from the tedious work of memory management.
The type of the corresponding hosting program does not need to be managed by the programmer. However, the local resources used by the corresponding hosting program are also non-managed resources, such as the types used by I/O operations and database connections.
Garbage collection cannot recycle unmanaged resources. Therefore, you must manually release these resources when using such resources.
For example:
Public class test: idispose
{
// Local resource, handle
Intptr res;
Public test (intprt R)
{
Res = r
}
Public void dispose ()
{
Res = intptr. zero;
}
}