gc-issues 2.0 KB

12345678910111213141516171819202122232425262728293031
  1. * GC issues you need to be careful about when hacking on the mono runtime.
  2. mono currently uses the Boehm garbage collection library. This is a conservative
  3. GC package: this means that the memory is searched for possible memory references
  4. to chunks allocated with the GC. Not all the memory is searched, but only the memory
  5. allocated with the GC itself (unless you use the 'atomic' variant of the allocation
  6. routines), the stack and global variables. Also, if the last reference to an object
  7. is stored in an area of themory that is not searched, the object may be freed resulting
  8. in memory corruption ind segfaults.
  9. In particular, memory allocated with system's malloc() is not searched, so you need to be
  10. careful NOT to store object references in memory allocated with malloc() (unless you are sure
  11. that the object reference will be live at the same time in some other area under the control
  12. of the GC (on the stack or in a global variable, for example). Since g_malloc()
  13. ultimately calls system malloc() the data structures in GLib are not safe to
  14. use to store object references.
  15. Occasionally, you'll need to store some object reference from C code: in this case,
  16. you must make sure that the store location is searched by the GC. You can safely
  17. use thread local storage areas, global variables, stack variables. If you need a more
  18. complicated data structure, you can use a hash table: MonoGHashTable.
  19. This hash table has the same interface as GHashTable from GLib, just stick the "mono_"
  20. prefix in function calls and the "Mono" prefix in the hash table type name.
  21. This hash table ensures that object references stored in it are tracked by the GC, as long
  22. as the pointer to the hash is tracked itself.
  23. Other data structures that are allocated with the GC and are safe to use to store pointers
  24. to GC-allocated memory, are MonoDomain (keeps track of many domain specfic objects)
  25. and MonoVTable (referenced by MonoDomain: keeps track of the static data of a type
  26. since that can hold references to objects).