Memory Leak Detector (MLD) is a lightweight tool designed to detect memory leaks in C programs. It helps developers identify dynamically allocated memory that is not freed before program termination. This tool is implemented in C and provides a detailed report of memory leaks, making debugging more efficient.
-
Detects memory leaks in C programs.
-
Supports two database structures for tracking allocations:
-
Linked Lists: Stores allocation records in a linked list.
-
Hash Maps: Uses a hashmap for faster lookups.
-
-
Tracks allocated memory blocks, including file name, line number, size, and status.
-
Reports leaked memory upon program termination.
-
Lightweight with minimal performance overhead.
- GCC compiler (
gcc)
To compile the project, follow these steps:
-
For Linked List Implementation:
cd mld/mld_dbs_as_linkedlists gcc -o exe appn.c mld.c -
For Hash Map Implementation:
cd mld/mld_dbs_as_hashmaps gcc -o exe appn.c mld.c
-
Run the compiled executable:
./exe -
The program will execute and track memory allocations dynamically.
-
If memory leaks are detected, a report will be displayed at the end of execution.
-
malloc()andcalloc()allocations are tracked using a custom implementation. -
Each allocation is recorded in either a linked list or hashmap.
-
Upon calling
free(), the record is removed from the tracking structure. -
When the program terminates, the tool reports any memory that was allocated but not freed.
To further enhance the Memory Leak Detector (MLD) and make it more robust, the following improvements can be implemented:
-
Maintain an automatic structure and object database for tracking allocations.
-
Store metadata for allocated structures without explicit user intervention.
-
Eliminate manual registration of structures by automatically detecting them at compile-time or runtime.
-
Use macro-based reflection or preprocessor-based code analysis to identify memory allocations dynamically.
-
Automatically mark specific objects as root objects in the memory tracking system.
-
Prevent tracking of short-lived objects by focusing on root objects.
-
Override
malloc(),calloc(), andfree()with custom tracking functions (xcalloc,xmalloc,xfree). -
The user will only register root objects, and all child allocations will be tracked automatically.
-
Allow dynamic control over memory tracking using preprocessor directives.
-
This allows users to compile their program with or without memory tracking based on requirements.
-
Implement a parser or AST-based tool to detect structure definitions and auto-register them.
-
Example: Use Clang AST (Abstract Syntax Tree) or Lex/Yacc-based parsing.
-
Move memory leak detection into a separate thread for asynchronous monitoring.
-
Reduce runtime overhead by performing expensive memory analysis in a background worker thread.
-
Feel free to fork and improve the project.
-
Open an issue if you find bugs or have suggestions.
