docs/design/mono/web/memory-management.md
Most metadata structures have a lifetime which is equal to the MonoImage where they are loaded from. These structures should be allocated from the memory pool of the corresponding MonoImage. The memory pool is protected by the loader lock. Examples of metadata structures in this category:
Memory owned by these structures should be allocated from the image mempool as well. Examples include: klass->methods, klass->fields, method->signature etc.
Generics complicates things. A generic class could have many instantinations where the generic arguments are from different assemblies. Where should we allocate memory for instantinations ? We can allocate from the mempool of the image which contains the generic type definition, but that would mean that the instantinations would remain in memory even after the assemblies containing their type arguments are unloaded, leading to a memory leak. Therefore, we do the following:
* generic class definition (MonoGenericClass->container_class)
* generic method definitions
* type parameters (MonoGenericParam)
* MonoGenericClass
* MonoGenericInst
* inflated MonoMethods
Original version of this document in git.
Executable code is managed using 'code-managers', whose implementation is in utils/mono-codeman.{h,c}. These allow the allocation of memory which is suitable for storing executable code, i.e.:
Code managers also allow a certain percent of the memory they manage to be reserved for storing things like function thunks.
The runtime contains the following code managers: