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