All Projects → crazybie → tgc

crazybie / tgc

Licence: MIT license
A Tiny, portable, precise, incremental mark-sweep GC designed for C++.

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to tgc

mmtk-core
Memory Management ToolKit
Stars: ✭ 205 (+469.44%)
Mutual labels:  garbage-collection, gc
on-the-fly-gc
Concurrent mark-sweep garbage collector for accurate garbage collection of language runtimes in C++ 1x.
Stars: ✭ 25 (-30.56%)
Mutual labels:  garbage-collection, gc
core.horse64.org
THIS IS A MIRROR, CHECK https://codeberg.org/Horse64/core.horse64.org
Stars: ✭ 3 (-91.67%)
Mutual labels:  portable, garbage-collection
Singularity
Singularity: Application containers for Linux
Stars: ✭ 2,290 (+6261.11%)
Mutual labels:  portable
Usb Disk Ejector
A program that allows you to quickly remove drives in Windows. It can eject USB disks, Firewire disks and memory cards. It is a quick, flexible, portable alternative to using Windows' "Safely Remove Hardware" dialog.
Stars: ✭ 175 (+386.11%)
Mutual labels:  portable
Libagar
Cross-Platform GUI Toolkit (stable)
Stars: ✭ 212 (+488.89%)
Mutual labels:  portable
cactusref
🌵 Cycle-Aware Reference Counting in Rust
Stars: ✭ 129 (+258.33%)
Mutual labels:  garbage-collection
Facenet mtcnn to mobile
convert facenet and mtcnn models from tensorflow to tensorflow lite and coreml (使用 TFLite 将 FaceNet 和 MTCNN 移植到移动端)
Stars: ✭ 166 (+361.11%)
Mutual labels:  portable
Postgresql Portable
Portable version of the PostgreSQL Database Server, for Windows
Stars: ✭ 237 (+558.33%)
Mutual labels:  portable
Messenger For Desktop
This is not an official Facebook product, and is not affiliated with, or sponsored or endorsed by, Facebook.
Stars: ✭ 2,180 (+5955.56%)
Mutual labels:  portable
Wagon
免安裝可攜的 Laravel 開發環境
Stars: ✭ 189 (+425%)
Mutual labels:  portable
Baulk
baulk - Minimal Package Manager for Windows
Stars: ✭ 177 (+391.67%)
Mutual labels:  portable
Sftpgo
Fully featured and highly configurable SFTP server with optional HTTP, FTP/S and WebDAV support - S3, Google Cloud Storage, Azure Blob
Stars: ✭ 3,534 (+9716.67%)
Mutual labels:  portable
Doomsday Engine
A portable, enhanced source port of Doom, Heretic and Hexen.
Stars: ✭ 175 (+386.11%)
Mutual labels:  portable
Boost.simd
Boost SIMD
Stars: ✭ 238 (+561.11%)
Mutual labels:  portable
Div Games Studio
Complete cross platform games development package, originally for DOS but now available on modern platforms.
Stars: ✭ 168 (+366.67%)
Mutual labels:  portable
Mlibc
Portable C standard library
Stars: ✭ 225 (+525%)
Mutual labels:  portable
Atom Portable
Portable version of the Atom text editor
Stars: ✭ 187 (+419.44%)
Mutual labels:  portable
Fast Ide
🕺Fast Integrated Development Environment 😻
Stars: ✭ 181 (+402.78%)
Mutual labels:  portable
Pockint
A portable OSINT Swiss Army Knife for DFIR/OSINT professionals 🕵️ 🕵️ 🕵️
Stars: ✭ 196 (+444.44%)
Mutual labels:  portable

TGC

A Tiny, precise, incremental, mark & sweep, Garbage Collector for C++.

参考请注明出处,谢谢。

Motivation

  • Scenarios that shared_ptr can't solve, e.g. object dependencies are dynamically constructed with no chance to recognize the usage of shared & weak pointers.
  • Try to make things simpler compared to shared_ptr and Oilpan, e.g. networking programs using callbacks for async io operations heavily.
  • A very good experiment to design a GC dedicated to the C++ language and see how the language features can help.

Highlights

  • Non-intrusive
    • Use like shared_ptr.
    • Do not need to replace the global new operator.
    • Do not need to inherit from a common base.
    • Can even work with shared_ptr.
  • Incremental marking and sweeping
    • Won't stop the world.
    • Can specify the number of steps used for each collecting.
    • Can manually delete the object to control the destruction order.
  • Super lightweight
    • Only one header & CPP file, easier to integrate.
    • No extra threads to collect garbage.
  • Support most of the containers of STL.
  • Cross-platform, no other dependencies, only dependent on STL.
  • Support multi-threads.
  • Customization
    • It can work with other memory allocators and pool.
    • Provide hooks to redirect memory allocation.
    • It can be extended to use your custom containers.
  • Precise.
    • Ensure no memory leaks as long as objects are correctly traced.

Comparison

  • Pros over shared_ptr:

    • no need for weak_ptr to break the circular references.
    • no shared_from_this is needed for GC pointer.
    • gc_from(this) works in the constructor where shared_ptr is not.
    • construct GC pointer from the raw pointer is safe to call many times where shared_ptr is not because it will reset the ref counter
    • can't predicate the number of objects destructed in complex scenarios when clear a shared_ptr, but not for GC pointers as you can control the collection steps to run.
  • Pros over Oilpan GC:

    • Easier to use, only one kind of GC pointer to be used.
    • More general, suitable for wider scenarios.

Internals

  • This collector uses the triple color, mark & sweep algorithm internally.
  • Pointers are constructed as roots by default unless detected as children of other object.
  • A GC pointer is with the size of 3-pointers:
    • one flag determin whether it's root or not.
    • an index for fast unregistering from collector.
    • one raw pointer to the object and another one raw pointer to the correspoinding meta-object, this is to support:
      • multiple inheritance.
      • pointer to fields of other object, aka internal pointer.
  • Every class has a global meta-object keeping the necessary meta-information (e.g. class size and offsets of member pointers) used by GC, so programs using lambdas heavily may have some memory overhead. Besides, as the initialization order of global objects is not well defined, you should not use GC pointers as global variables too (there is an assert checking it).
  • Construct & copy & modify GC pointers are slower than shared_ptr, much slower than raw pointers(Boehm GC).
    • Every GC pointer must register itself to the collector and unregister on destruction as well.
    • Since C++ does not support ref-qualified constructors, the gc_new returns a temporary GC pointer bringing in some meaningless overhead. Instead, using gc_new_meta can bypass the construction of the temporary making things a bit faster.
    • Member pointers offsets of one class are calculated and recorded at the first time of creating the instance of that class.
    • Modifying a GC pointer will trigger a GC color adjustment which may not be cheap as well.
  • Each allocation has a few extra space overhead (size of two pointers at most), which is used for memory tracing.
  • Marking & swapping should be much faster than Boehm GC, due to the deterministic pointer management, no scanning inside the memories at all, just iterating pointers registered in the GC.
  • To make objects in proper tracing chain, you must use GC wrappers of STL containers instead, otherwise, memory leaks may occur.
  • gc_vector stores pointers of elements making its storage not continuous as a standard vector, this is necessary for the GC. All wrapped containers of STL stores GC pointers as elements.
  • You can manually call gc_delete to trigger the destructor of an object and let the GC claim the memory automatically. Besides, double free is also safe.
  • For the multi-threaded version, the collection function should be invoked from the main thread therefore the destructors can be triggered in the main thread as well.

Performance Advice

  • Performance is not the first goal of this library.
    • Results from tests, a simple allocation of an integer is about 8~10 slower than standard new(see test), so benchmark your program if GC pointers are heavily used in the performance-critical parts(e.g. VM of another language).
    • Use the references to GC pointers as much as possible. (e.g. function parameters, see internals section)
    • Use gc_new_array to get a collectible continuous array for better performance in some special cases (see internals section).
    • Continuous efforts will be put to optimize the performance at a later time.
    • Languages with GC built-in prefer to create a huge number of heap objects which will give large pressure to the GC, some languages even use pointer escaping analyzing algorithm to increase the recycling efficiency, but it's not a serious problem to C++ as it has RAII and does not use heap objects everywhere. So the throughput of this triple-color GC should be efficient enough.
  • For real-time applications:
    • Static strategy: just call gc_collect with a suitable step count regularly in each frame of the event loop.
    • Dynamic strategy: you can specify a small step count(default is 255) for one collecting call and time it to see if still has time left to collect again, otherwise do collecting at the next time.
  • As memories are managed by GC, you can not release them immediately. If you want to get rid of the risk of OOM on some resource-limited system, memories guaranteed to have no pointers in it can be managed by shared_ptrs or raw pointers.
  • The single-threaded version(by default) should be much faster than the multi-threaded version because no locks are required at all. Please define TGC_MULTI_THREADED to enable the multi-threaded version.

Usage

Please see the tests in 'test.cpp'. Another small demo here: https://github.com/crazybie/AsioTest.git

Refs

TODO

  • move & compact sine we manages raw pointers in pointer object, we can move the object and adjust the raw pointers internally.
  • use generatioinal algorithm no need to scan all objects every time when do garbage collecting.

License

The MIT License

Copyright (C) 2018 [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].