Virtual Expo 2025

C12 - Optiheap

Envision CompSoc

 

OptiHeap: A Custom Dynamic Memory Manager and Garbage Collector in C

OptiHeap is a custom memory management system written in C that provides dynamic memory allocation, deallocation, and automatic garbage collection using reference counting.

gmeet link :https://meet.google.com/pma-iwhf-kmh
 

Aim

To design and implement a memory management system in C that improves manual memory handling by offering custom malloc and free functionalities along with an automated garbage collector based on reference counting.

Introduction

Memory management in C is manually handled using standard functions like malloc, calloc, and free. However, these can often lead to issues such as memory leaks, dangling pointers, and fragmentation. OptiHeap addresses these challenges by introducing a custom allocator and a basic garbage collector using reference counting, aiming to improve memory safety and simplify memory operations.

Literature Survey and Technologies Used

Technologies Used:

  • Language: C
  • Compiler: GCC
  • Debugging Tools: Valgrind, GDB
  • Platform: Linux

Relevant Literature:

  • Reference Counting Garbage Collection (John McCarthy, 1960s)
  • Memory Allocation Techniques (First-fit, Best-fit, Buddy System)
  • Research papers and open-source implementations of malloc (e.g., dlmalloc, jemalloc)
  • These sources provided insight into efficient memory management strategies, challenges in C programming, and inspired the hybrid approach of combining manual control with automatic cleanup.

Methodology

Research:

  • Studied traditional allocation algorithms like first-fit and segregated lists.
  • Investigated garbage collection techniques: reference counting, mark-and-sweep, and generational GC.

Design & Procedure:

1. Custom Allocator:

  • Implemented a memory pool with bookkeeping metadata.
  • Memory blocks include headers for size, allocation status, and reference count.
  • Custom malloc() and free() manage allocation and deallocation.

2. Reference Counting GC:

  • Each allocated object tracks how many references point to it.
  • On free(), reference count is decremented.
  • If count reaches zero, memory is reclaimed.
  • Handles cyclic references using developer intervention or heuristics.

3. Memory Checker:

Optional debugging layer to track memory leaks and dangling pointers using logs.

Results

  • Successfully implemented a working prototype of malloc() and free() with reference counting.
  • Integrated a lightweight garbage collector that reduced manual deallocation overhead.
  • Demonstrated memory usage optimization and detection of leaks using test cases.
  • Verified with Valgrind to show reduced memory leaks compared to manual allocation.

Conclusions and Future Scope

Conclusions:

  • OptiHeap enhances memory safety in C through structured allocation and reference-based garbage collection.
  • The project proves that reference counting, while limited, is a viable strategy in embedded or low-level environments lacking native GC.

Future Scope:

  • Extend GC to handle cyclic references using mark-and-sweep or weak references.
  • Implement thread safety with locks or atomic operations.
  • Optimize allocation with slab or buddy allocator.
  • Create a memory visualization tool to inspect memory states in real-time.

References and Links

  • https://github.com/GutsJSN/OptiHeap
  • Wikipedia - Garbage Collection (Computer Science)
  • Valgrind - Memory Debugger
  • Memory Management in C - GeeksforGeeks

Mentors

  • Sanjay S Bhat
  • Dev Prajapati
  • Raunil Singh

Mentees

  • Sindhu
  • Shrushti

Report Information

Explore More Projects

View All 2025 Projects