Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. C/C++
  3. tgc

tgc

NOASSERTIONC

A tiny, conservative mark-and-sweep garbage collector for C written in ~500 lines of code.

GitHubGitHub
1.1k stars70 forks0 contributors

What is tgc?

TGC is a tiny garbage collector for the C programming language that automatically manages memory allocations. It uses a conservative mark-and-sweep algorithm to free memory when it becomes unreachable, helping developers avoid memory leaks without manual deallocation.

Target Audience

C developers working on applications where manual memory management is error-prone, such as long-running programs, embedded systems, or projects where simplicity and reduced memory leaks are priorities.

Value Proposition

TGC offers an extremely lightweight and portable garbage collection solution with minimal code footprint (~500 lines), making it easy to integrate into existing C projects without complex dependencies or runtime overhead.

Overview

A Tiny Garbage Collector for C

Use Cases

Best For

  • Adding automatic memory management to existing C applications
  • Reducing memory leaks in long-running C programs
  • Simplifying memory management in embedded C projects
  • Educational purposes to learn garbage collection implementation
  • Prototyping C applications without manual memory management
  • Integrating garbage collection into C libraries with minimal footprint

Not Ideal For

  • Multi-threaded applications requiring concurrent garbage collection
  • Projects with heavy pointer arithmetic or algorithms that increment pointers
  • Systems where compiler optimizations like inlining cannot be easily controlled
  • Programs relying extensively on setjmp and longjmp for control flow

Pros & Cons

Pros

Minimal Codebase

At only ~500 lines of code, tgc is incredibly lightweight and easy to audit or modify, making it ideal for embedding in projects without bloating the codebase.

Portable Design

Tested on Linux, Windows, and OSX, tgc avoids platform-specific tricks and relies on basic assumptions about the stack, ensuring wide compatibility for most architectures.

Destructor Support

Allows registering cleanup functions for resources like file handles, enabling automatic resource management alongside memory freeing, as shown in the tgc_set_dtor function.

Manual Control Flexibility

Provides functions to manually free memory, pause/resume collection, and mark allocations as roots or leaves, offering fine-grained performance tuning and management.

Cons

Single-Threaded Only

Designed for thread-local use, making it unsuitable for multi-threaded applications without significant modifications, as pointers from other threads are not considered reachable.

Pointer Arithmetic Limitations

Requires pointers to always point to the start of allocations; incrementing pointers can cause premature freeing, complicating algorithms that use pointer arithmetic, as admitted in the README's F.A.Q.

Compiler Optimization Sensitive

Performance and correctness can be affected by compiler optimizations like inlining, requiring workarounds such as volatile function pointers, which adds complexity to integration.

Conservative and Incomplete

Does not handle pointers in the static data segment or from non-tgc allocations, limiting its reachability analysis and potentially leaving memory leaks in edge cases, following its 'Worse Is Better' philosophy.

Frequently Asked Questions

Quick Stats

Stars1,098
Forks70
Contributors0
Open Issues5
Last commit3 years ago
CreatedSince 2016

Tags

#systems-programming#memory-management#embedded#c#low-level#garbage-collection

Built With

C
C++

Included in

C/C++70.6k
Auto-fetched 10 hours ago

Related Projects

mimallocmimalloc

mimalloc is a compact general purpose allocator with excellent performance.

Stars13,358
Forks1,174
Last commit1 day ago
jemallocjemalloc

General purpose malloc(3) implementation that emphasizes fragmentation avoidance and scalable concurrency support. [BSD] website

Stars11,095
Forks1,640
Last commit5 days ago
TCMallocTCMalloc

TCMalloc is Google's customized implementation of C's `malloc()` and C++'s `operator new` for memory allocation within C and C++ code. It provides a high-performance alternative to standard memory allocators, optimized for multi-threaded applications where allocation speed and scalability are critical. ## Key Features - **Fast Allocation** — Optimized for speed with efficient memory management algorithms - **Multi-threaded Performance** — Designed to scale well in concurrent environments - **Customizable Configuration** — Tunable parameters for different workload requirements - **Comprehensive API** — Full support for C and C++ memory allocation interfaces - **Platform Support** — Available across multiple operating systems and architectures ## Philosophy TCMalloc prioritizes performance and scalability in multi-threaded environments while maintaining compatibility with standard memory allocation interfaces, making it suitable for production systems with demanding memory allocation patterns.

Stars5,341
Forks569
Last commit11 hours ago
Boehm GCBoehm GC

The Boehm-Demers-Weiser conservative C/C++ Garbage Collector (bdwgc, also known as bdw-gc, boehm-gc, libgc)

Stars3,536
Forks445
Last commit5 days ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub