memory-management.txt 1.7 KB

1234567891011121314151617181920212223242526272829303132
  1. Metadata memory management
  2. --------------------------
  3. Most metadata structures have a lifetime which is equal to the MonoImage where they are
  4. loaded from. These structures should be allocated from the memory pool of the
  5. corresponding MonoImage. The memory pool is protected by the loader lock.
  6. Examples of metadata structures in this category:
  7. - MonoClass
  8. - MonoMethod
  9. - MonoType
  10. Memory owned by these structures should be allocated from the image mempool as well.
  11. Examples include: klass->methods, klass->fields, method->signature etc.
  12. Generics complicates things. A generic class could have many instantinations where the
  13. generic arguments are from different assemblies. Where should we allocate memory for
  14. instantinations ? We can allocate from the mempool of the image which contains the
  15. generic type definition, but that would mean that the instantinations would remain in
  16. memory even after the assemblies containing their type arguments are unloaded, leading
  17. to a memory leak. Therefore, we do the following:
  18. - data structures representing the generic definitions are allocated from the image
  19. mempool as usual. These include:
  20. - generic class definition (MonoGenericClass->container_class)
  21. - generic method definitions
  22. - type parameters (MonoGenericParam)
  23. - data structures representing inflated classes/images are allocated from the heap. These
  24. structures are kept in a cache, indexed by type arguments of the instantinations. When
  25. an assembly is unloaded, this cache is searched and all instantinations referencing
  26. types from the assembly are freed. This is done by mono_metadata_clean_for_image ()
  27. in metadata.c. The structures handled this way include:
  28. - MonoGenericClass
  29. - MonoGenericInst
  30. - inflated MonoMethods