# Memory Management

| Managed Code                                                                 | Unmanaged Code                                                                                                                                                                                                                                     |
| ---------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Managed Code is developed within the .NET framework.                         | Unmanaged code is any code developed outside the .NET framework.                                                                                                                                                                                   |
| CLR directly executes such code by using managed code execution.             | Unmanaged applications are not executed by CLR, but by OS or wrapper classes such as **CCW** (COM Callable Wrapper) and **RCW** (Runtime Callable Wrapper).                                                                                        |
| Any language written in the .NET framework is considered to be managed code. | Some languages like C++ can write unmanaged applications such as an application for accessing the low-level functions of the operating system. Some examples of unmanaged code include background compatibility with the code of VB, ASP, and COM. |

## what is managed code ?

To put it simply, managed code is just that: code whose execution is managed by a runtime. In this case, the runtime in question is called the **Common Language Runtime** or CLR, regardless of the implementation (for example, Mono, .NET Framework, or .NET Core/.NET 5+). The CLR is in charge of taking the managed code, compiling it into machine code and then executing it. On top of that, the runtime provides several important services such as automatic memory management, security boundaries, and type safety.

## what is unmanaged code ?

Contrast this to the way you would run a C/C++ program, also called "unmanaged code". In the unmanaged world, the programmer is in charge of pretty much everything. The actual program is, essentially, a binary that the operating system (OS) loads into memory and starts. Everything else, from memory management to security considerations are a burden of the programmer.

## clean up unmanaged resources !

when you create objects that include unmanaged resources, you **must** explicitly release those resources when you finish using them.&#x20;

The most common types of unmanaged resources are objects that wrap operating system resources, such as _files_, _windows_, _network connections_, or _database connections_.&#x20;

Although the garbage collector is able to track the lifetime of an object that encapsulates an unmanaged resource, it doesn't know how to release and clean up the unmanaged resource.

### how to clean up these unmanaged resources ?

* Implement the **dispose pattern**. This requires that you provide an `IDisposable.Dispose` implementation to enable the deterministic release of unmanaged resources. A consumer of your type calls Dispose when the object (and the resources it uses) are no longer needed. The `Dispose()` method immediately releases the unmanaged resources.
