Ownership-based memory management – Memory management – Manual memory management



Ownership-based memory management – Memory management – Manual memory management

0 0


ownership

Repo for hosting a presentation about ownership

On Github aochagavia / ownership

Ownership-based memory management

By Adolfo Ochagavía

Nieuw ontwikkeling. Paar jaar geleden taal Rust ontdekt. Fascinerend: geen garbage collection in 2015! Contributor geworden, etc.

Memory management

When is an object destroyed?

Wanneer wordt een object verwijderd

The main problem: Memory is limited

Alles leaken (geen optie). Garbage collection (C#). Manual (C en C++).

Why is this important?

  • Bugs
  • Performance
Het soort memory management dat je kiest heeft veel invloed op performance en op bugs in je systeem

Requirements

An object is destroyed
  • Only once
  • Only if it won't be used afterwards

Manual memory management

An object is destroyed when the programmer says so

Example

List<int> ints = new List<int>();
// Do stuff...
delete ints;
                        

Problems...

Programmeur heef grote verantwoordelijkheid. Bugs veroorzaken crashes en veiligheidslekken.

Dangling pointer

List<int> ints = new List<int>();
// Do stuff...
delete ints;
Console.WriteLine(ints.Count); // Oops...
                        

Memory leak

List<int> ints = new List<int>();
// Do stuff...
// Oops... forgot to use 'delete'
                        

Double free

List<int> ints = new List<int>();
// Do stuff...
delete ints;
delete ints; // Oops
                        

Garbage Collection

An object is destroyed when the garbage collector says so

Beschikbaar in de programmeertaal LISP in de jaren 60! Apart programma.

Example

List<int> ints = new List<int>();
// Do stuff...
                        

Benefits

  • Removes bugs related to memory management
  • Frees the programmer of thinking about memory

Problems

  • Two programs running instead of one
  • More CPU, memory and energy usage

More information...

The Garbage Collection Handbook: The Art of Automatic Memory Management (2011, Jones et al.)

(500 pages of algorithms and techniques)

Static Analysis

An object is destroyed when the compiler says so

De compilert genereert de deletes voor jou

Example

This code

List<int> ints = new List<int>();
// Do stuff...
                        

Becomes

List<int> ints = new List<int>();
// Do stuff...
delete ints;
                        

Benefits

  • Lightweight as manual memory management
  • Bug-free as garbage collection
Geen tweede programma die naast je eigen programma runt

How!?

Mainly two approaches

  • Regions
  • Ownership

Regions

Bijzonder: bestaande programma's, zonder aanpassingen

Example

for (int i = 0; i < 10; i++) {
    List<int> ints = new List<int>();
    // Do stuff...
}
                        

Becomes

for (int i = 0; i < 10; i++) {
    List<int> ints = new List<int>();
    // Do stuff...
    delete ints;
}
                        
Werkt goed in eenvoudige gevallen, maar...

Problems

  • Usually there is not enough information: Leaks
  • It is almost impossible to know what the compiler is doing
Te weinig informatie? Leaken! Beter dan dangling pointers.

More information...

A retrospective on region-based memory management (2004, Tofte et al.)

Na jaren onderzoek: beter om dit te combineren met garbage collection. Teveel leaks.

Ownership

The problem of regions

Not enough information

The solution of ownership

Force programs to provide more information

How?

A program must follow certain rules...

... otherwise it will be rejected by the compiler

Benefits

  • No more memory leaks or other memory-related bugs
  • As performant as manual memory management!
Impressive combination!

Problems

Many normal programs don't compile...

... and the programmer has to think about memory management

Ownership rules

The rules

Data has a unique owner Only the owner of the data may access it Ownership can be transferred

Example

List<int> ints = new List<int>();
List<int> myInts = ints;
Console.WriteLine(ints.Count); // Illegal!
Console.WriteLine(myInts.Count); // OK!
                        

But why!?

More information...

Owner niet meer in scope -> niemand anders kan het object gebruiken -> verwijderen

Example

{
    List<int> ints = new List<int>();
    // Do stuff...
    // delete ints; inserted by the compiler
}
                        
Onafhankelijk van hoe complex het programma wordt. Analyse wordt veel makkelijker.

Ergonomics

The previous rules are a little bit restrictive

Verschrikkelijk om echte programma's te schrijven met de huidige regels

Example

List<int> ints = new List<int>();
List<int> moreInts = new List<int>();
if (moreInts.Equals(ints))
    ints.Add(42); // Illegal!
                        

How to solve this?

Manier om toegang tot de data te krijgen zonder de ownership ervan over te dragen

New rules

Data has a unique owner Only the owner and the borrowers of the data may access it Ownership can be transferred if there are no active borrows to the data

Borrowing basics

List<int> ints = new List<int>();
List<int> moreInts = new List<int>();
if (moreInts.Equals(&ints)) // Notice the &
    ints.Add(42); // Legal!
                        
Nog meer aanpassingen aan de eerste ownership regels, maar gebrek aan tijd!

Limitations of Ownership

Skip this if we are short on time. Waarom gebruikt niet iedereen ownership als het zo goed is?

Old programs don't work

Wordt opgelost in de loop der tijd

Cyclic data structures

Example: Doubly linked list

Wie is de owner?

Solutions

Write tricky manual memory management code...

... Or just use garbage collection

Typish: combineren met manual memory management (niet ideaal, maar beter dan puur manual)

Ownership in Programming Languages

Skip this if we are short on time
  • C++
  • Rust
  • C++: smart pointers (2001), move semantics (C++11). Optioneel.
  • Rust: lijkt veel op C++, maar dan verplicht. Geen data-races.

Conclusion

Ownership

  • Set of rules
  • Limitations
  • Bug-free alternative to manual memory management
Als je de beste performance nodig hebt, maar niet durft te programmeren in C of C++, gebruik ownership. Veel invloed op toekomstige programmeertalen. Veel invloed in C++ op dit moment.

Questions?

1
Ownership-based memory management By Adolfo Ochagavía Nieuw ontwikkeling. Paar jaar geleden taal Rust ontdekt. Fascinerend: geen garbage collection in 2015! Contributor geworden, etc.