Last Updated:

What is Managed Code

In the Serialize example, the second instance of the Customer object was assigned to the same variable that was previously assigned to the first instance. However, the destructor was not called to remove the first instance of the class from memory. In this example, none of the dedicated memory areas were ever released. To free memory from objects that are instances of classes declared with the _g c (garbage collector) keyword, .NET uses automated garbage collection. If memory allocated in a managed dynamically allocated memory area becomes hanging or falls out of scope, it is listed in the list of memory areas to be freed. Periodically, the system initiates the garbage collection process. The resulting memory is returned to a dynamically allocated area of memory.

Automatic memory management eliminates memory leakage in the system. A memory leak is one of the most common mistakes when programming in C and C++. In most cases, through the use of automatic garbage collection, the allocation of memory in a dynamically allocated memory area is significantly faster compared to classical schemes. Note that these KaKpcust and plist variables are managed pointers to objects, not self-contained objects. It is because of this that garbage collection is possible.

Garbage collection is one of several services provided by the common language runtime (CLR) to programs running on .NET [Technical metadata, Common Type System (CTS), Common Type System (CLS), and Virtual Execution System (VES) ) are also part of the common language runtime (CLR). The adjective "common language" means that the runtime is common to all languages. The VES Virtual Execution System loads and executes . NET programs and support dynamic linking. Refer to the Common Language Infrastructure (CLI) documents, namely the "Partition I: Concepts and Architecture" section on concepts and architecture. These documents have been submitted for further consideration by the European Computer Manufacturers Association (ESMA). These documents are downloaded with the .NET Framework SDK.].

The data involved in the garbage collection process initiated by the common language runtime (CLR) is called managed data. Managed code is code that can use the services provided by the common language runtime (CLR). . NET compilers that generate Microsoft intermediate language (MSIL) code can generate managed code.

Managed code does not automatically meet typical security requirements. The C++ language can serve as a classic example of this. If you want a class to participate in garbage collection (that is, it is managed), you must use the _g c (garbage collector) attribute when declaring it. The compiler for C++ prohibits the use of pointer arithmetic operations in such classes. However, C++ code cannot be reliably tested for typical security because the C and C++ libraries are used.

Code is checked for typical security before it is compiled. Such verification is optional and may be skipped if the code is certified. One of the most significant differences between verifiable code and unverifiable code is that pointers cannot be used in verifiable code. Code that uses pointers can destroy the general CTS type system and its translation can result in code that does not satisfy the typical security.

managed code

Code that satisfies typical security cannot be easily broken. For example, overwriting a buffer cannot damage other data structures or programs. You can apply a security policy to code that meets typical security requirements. For example, you can allow or deny access to certain files or UI elements. You can prevent code from sources unknown to you from running. To prevent the security of the .NET platform from being destroyed, you can prevent access to unmanaged code. With standard security, you can also isolate executing .NET code branches from one another [Application Domains are discussed in Chapter 8, ".NET Framework Wireframe Classes."].