2007-09-17 Zoltan Varga <vargaz@gmail.com>
authorZoltan Varga <vargaz@gmail.com>
Mon, 17 Sep 2007 20:37:01 +0000 (20:37 -0000)
committerZoltan Varga <vargaz@gmail.com>
Mon, 17 Sep 2007 20:37:01 +0000 (20:37 -0000)
* docs/memory-management.txt: A new file describing metadata memory management.

svn path=/trunk/mono/; revision=85938

ChangeLog
docs/memory-management.txt [new file with mode: 0644]

index 6e44ab8f4dc091c470da043ed860dab6baf1ea81..b78e40b0454c3ea78c9d6909fb5ef21fb254d271 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2007-09-17  Zoltan Varga  <vargaz@gmail.com>
+
+       * docs/memory-management.txt: A new file describing metadata memory management.
+
 2007-09-14  Jonathan Chambers  <joncham@gmail.com>
 
        * winconfig.h: Define WINVER and _WIN32_WINNT for VS
diff --git a/docs/memory-management.txt b/docs/memory-management.txt
new file mode 100644 (file)
index 0000000..a78ab5e
--- /dev/null
@@ -0,0 +1,32 @@
+Metadata memory management
+--------------------------
+
+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:
+- MonoClass
+- MonoMethod
+- MonoType
+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:
+- data structures representing the generic definitions are allocated from the image
+  mempool as usual. These include:
+  - generic class definition (MonoGenericClass->container_class)
+  - generic method definitions
+  - type parameters (MonoGenericParam)
+- data structures representing inflated classes/images are allocated from the heap. These
+  structures are kept in a cache, indexed by type arguments of the instantinations. When
+  an assembly is unloaded, this cache is searched and all instantinations referencing
+  types from the assembly are freed. This is done by mono_metadata_clean_for_image ()
+  in metadata.c. The structures handled this way include:
+  - MonoGenericClass
+  - MonoGenericInst
+  - inflated MonoMethods