GithubHelp home page GithubHelp logo

ogc's Introduction

OGC: Ordinary Garbage Collector

About

OGC implements the typical "mark and sweep" algorithm. It works as follows:

  • there is an internal struct representing a block of memory
  • there is a list of blocks representing the allocated memory, and another list representing the local stack of memory
  • once a new object is created,
    • its memory is allocated and its meta data is added to both lists
    • there is a threshold for the number of objects to allocate before cleaning up the memory
    • when the threshold is met:
      • call "mark" with the local stack of memory ("reachable" memory)
      • mark recurses through the list and set a "mark" flag for every block reachable from the inital list of reachable memory
      • we then call "sweep" with the global allocated memory list
      • every block that is not marked is not used, and can be freed and popped from given list

How it works

For a basic mark and sweep garbage collector, two things are required:

  • a list of all of the allocations made by the program;
  • a list of all the allocations in use by the program at any given time;

With these two things the algorithm is simple - compare the two lists and free any allocations which are in the first list, but not in the second - exactly those allocations which are no longer in use.

To get a list of all the allocations made by the progam is relatively simple. We make the programmer use a special function we have prepared (in this case gc_alloc) which allocates memory, and then adds a pointer to that memory to an internal list. If at any point this allocation is freed (such as by gc_free), it is removed from the list.

Function list

  • gc_alloc()

    • Allocates a space in memory of the size which the user requests
    • Ensures the user entered a number > 0
    • Attempts to find a freed block of memory to reuse.
    • On success, returns the address of the newly allocated memory. Otherwise, returns NULL.
  • gc_free()

    • Marks the object as free in the list of objects
    • Ensures the pointer isn't NULL or already free before marking it
  • gc_run()

    • Recursively marks all reachable objects in the list of objects
    • Recursively sweeps (frees) all unmarked (unreachable) objects

ogc's People

Contributors

jserv avatar hexrabbit avatar metalheadken avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.