Skip to content

MM120-i/GarbageCollector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mark and Sweep Garbage Collector

Description

This project is a simple implementation of a Mark and Sweep Garbage Collector in C. It demonstrates how memory management and automatic garbage collection can be handled in a custom virtual machine environment. The collector supports basic object types such as integers, floats, strings, arrays, and vector3 objects, and provides a clear step-by-step output of the garbage collection process.

Why?

Manual memory management in C is error-prone and can lead to memory leaks or dangling pointers. This project aims to:

  • Demonstrate the principles of garbage collection using the mark-and-sweep algorithm.
  • Provide a learning resource.
  • Solve the problem of automatic memory reallocation in environments where objects are dynamically created and referenced.

Reference Counting

As a foundation, this project also includes a Reference Counting (RefCount) algorithm. RefCount is a very basic form of garbage collection. It helps illustrate the concept of tracking object references, but it is not enough for real-world use (e.g., it cannot handle cyclic references). The Mark and Sweep implementation builds upon this base to provide a more practical and complete solution.

Quick Start

Prerequisites

  • CMake installed and added to your system PATH.
  • MinGW (or another C compiler) installed and added to your system PATH.

Build Instructions

  1. Clone the repository and navigate to the project directory:

    git clone <your-repo-url>
    cd GarbageCollector/Mark_and_Sweep
  2. Create and enter the build directory:

    mkdir build
    cd build
  3. Generate build files and compile:

    cmake -G "MinGW Makefiles" ..
    cmake --build .
  4. Run the executable:

    GarbageCollector.exe

Usage

  • The main entry point is testCases.c, which runs several test cases to demonstrate garbage collection:

    • Creating and collecting integer, array, vector3, and string objects.
    • Removing references and triggering garbage collection.
    • Step-by-step output of the mark, trace, and sweep phases.
  • Example output:

    === [GC] Starting garbage collection ===
    [GC] Mark phase...
    [GC][Mark] Marking root references in all frames...
    ...
    [GC] Trace phase...
    ...
    [GC] Sweep phase...
    ...
    === [GC] Garbage collection complete ===
    
  • You can modify testCases.c to add your own objects and test scenarios.


About

Garbage collector

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors