Skip to content

Memory model

Jiří Fatka edited this page Aug 23, 2017 · 2 revisions

(WIP) The Shard language have own model how to handle variable allocation and deallocation.

Introduction

Managing memory is big problem in all languages. There are diffent approaches to this problem. In some languages the developer is responsible for memory allocation and deallocation (C). Some adds helpers to easily manage allocations and deallocations (smart pointers in C++).

On the other side there are languages which uses automatic memory management using a garbage collector. There are two types of garbage collectors: Reference counting (JavaScript, PHP) and Mark & Sweep (Java).

Reference counting

The reference counting works in a way of increasing and decreasing some counter. When the counter reaches zero, object is destroyed. Predictable and deterministic garbage collector, but a big issue are loops.

Mark and Sweep

The Java's Mark and Sweep manages all objects and stores references to those objects. Once a time it runs a cleanup procedure when checks all object references and when noone references to the object, objects is destroyed. The main problem is with cleanup procedure because it needs to suspend the main program to perform cleanup. This cleanup procedure is not predictable and can be executed anytime.

Ownership

The Rust language have interesting idea of object ownership and borrowing. There is only one object owner and ownership can be transfered but the original owner lost the ownership. The borrowing allows the others to borrow temporary the ownership.

Shard memory model

All memory models have own positives and negatives and using only one is not a good idea. The best solution is take the best from them and create a hybrid. Using one specific memory management/GC can be an extension and programmer must explicitly specify which one he wants to use.

TODO

Clone this wiki locally