In this project, we try to implement a garbage collector for C programs which work on the principle of reachability of objects to detect memory leaks. Through this project, we understand the limitation of such a garbage collector for C like programming languages (which have direct access to underlying memory addresses, unlike Java/python) and analyze its limitations and cost for being an inbuilt feature of C-like language.
- gcc -g -c memory-leak.c -o memory-leak.o
- gcc -g -c test-app.c -o test-app.o
- gcc -g -o exe test-app.o memory-leak.o
- ./exe
Start writing your code from test-app.c file and implement your code. Compile memory-leak.c file and add memory-leak.h file where-ever needed.
-
Step 1 : Initialize a new structure database :
- struct_db_t *struct_db = calloc(1, sizeof(struct_db_t));
- mld_init_primitive_data_types_support(struct_db);
-
Step 2 : Create structure record for structure that you have declared:
- static field_info_t emp_fields[] = {
- FIELD_INFO(emp_t, emp_name, CHAR, 0),
- FIELD_INFO(emp_t, emp_id, UINT32, 0),
- FIELD_INFO(emp_t, age, UINT32, 0),
- FIELD_INFO(emp_t, mgr, OBJ_PTR, emp_t),
- FIELD_INFO(emp_t, salary, FLOAT, 0)};
-
Step 3 : Register the structure in structure database
- REGISTER_STRUCTURE(struct_db, emp_t, emp_fields);
-
Step 4 : Print structure database
- print_structure_db(struct_db);
-
Step 5 : Initialize a new Object database :
- object_db_t *object_db = calloc(1, sizeof(object_db_t));
- object_db->struct_db = struct_db;
-
Step 6 : Create objects, equivalent to standard
- Use xcalloc instead of calloc and malloc.
- void *ptr = xcalloc(object_db_t *object_db, char *struct_name, int units);
-
Step 7 :
- Choose Root objects in the hierarchy of your data structures.
- mld_set_dynamic_object_as_root(object_db, abhishek);
-
Step 8 :
- print_object_db(object database pointer);
-
Step 9 :
- run_mld_algorithm(object_db);
- report_leaked_objects(object_db);
struct emp_t{
unsigned int des;
};
struct des_t{
char name[32];
};
struct emp_t *emp = xcalloc(...);
struct des_t *des = xcalloc(...);
emp->des = (unsigned int)*des;
des = NULL;
/*
Now MLD library will report it as a leak but memory is accessible through (struct des_t*)emp->des
and JAVA DO NOT ALLOW IT.
*/
- Indirect Reference to objects is not valid.
struct emp_t{
struct list_node *node;
};
struct des_t{
char name[32];
emp_t *hod;
struct list_node node;
};
struct emp_t *emp = xcalloc(...);
struct des_t *des = xcalloc(...);
emp->node = &des->node;
des = NULL;
/*
MLD library cannot parse fields of des_t object from emp_t object.
Now on traversing graph , HOD employee will not be visited and it will be reported as a leak.
But it isn't;
*/
- Does not allow Embedded Object Declaration so you cannot have a reference pointing to Embedded objects in Java.
- This library cannot handle unions because unions don't have fixed size.
- This library is equivalent to Java Garbage Collector if we write programs in Java Style