Programming Language Concepts Memory management in ...

28
Programming Language Concepts Memory management in different languages Janyl Jumadinova 13 April, 2017

Transcript of Programming Language Concepts Memory management in ...

Programming Language ConceptsMemory management in different

languages

Janyl Jumadinova13 April, 2017

C

I Memory management is typically manual:– the standard library functions for memory management in C,malloc and free, have become almost synonymous with manualmemory management.

I Use external software, such as the Boehm-Demers-Weisercollector (a.k.a. Boehm GC), to do garbage collection inC/C++:– use Boehm instead of traditional malloc and free in C

http://hboehm.info/gc/

2/16

C

I Memory management is typically manual:– the standard library functions for memory management in C,malloc and free, have become almost synonymous with manualmemory management.

I Use external software, such as the Boehm-Demers-Weisercollector (a.k.a. Boehm GC), to do garbage collection inC/C++:– use Boehm instead of traditional malloc and free in C

http://hboehm.info/gc/

2/16

C++

I The standard library functions for memory management in C++are new and delete.

I The higher abstraction level of C++ makes the bookkeepingrequired for manual memory management even harder than C.

I In addition to Boehm, we can use smart pointers as a memorymanagement solution.

3/16

C++

I The standard library functions for memory management in C++are new and delete.

I The higher abstraction level of C++ makes the bookkeepingrequired for manual memory management even harder than C.

I In addition to Boehm, we can use smart pointers as a memorymanagement solution.

3/16

C++

Raw pointer:

MyClass *ptr = new MyClass();

ptr->doSomething();

delete ptr; // destroy the object.

Smart pointer:

// declare a smart pointer on stack

// and pass it the raw pointer

SomeSmartPtr<MyObject> ptr(new MyObject());

ptr->DoSomething(); // use the object in some way

// destruction of the object happens automatically

4/16

C++

Raw pointer:

MyClass *ptr = new MyClass();

ptr->doSomething();

delete ptr; // destroy the object.

Smart pointer:

// declare a smart pointer on stack

// and pass it the raw pointer

SomeSmartPtr<MyObject> ptr(new MyObject());

ptr->DoSomething(); // use the object in some way

// destruction of the object happens automatically

4/16

Java

I Java is garbage-collected.

I Early JVMs had simple collectors that didn’t scale well for largeprograms.

5/16

Java

Common Heap Related Switches

I -Xms: Sets the initial heap size for when the JVM starts.

I -Xmx: Sets the maximum heap size.

I -Xmn: Sets the size of the Young Generation.

I -XX:PermSize: Sets the starting size of the PermanentGeneration.

I -XX:MaxPermSize: Sets the maximum size of the PermanentGeneration.

java -Xmx12m -Xms3m -XX:+UseG1GC -jar

c:..Java2demo.jar

6/16

Java

I Serial GC: both minor and major garbage collections are doneserially

I Parallel GC: uses multiple threads to perform the younggeneration garbage collection

I Concurrent Mark Sweep (CMS): collects the tenuredgeneration, does most of the garbage collection workconcurrently with the application threads

I G1 Garbage Collector: available in Java 7 and is designed tobe the long term replacement for the CMS collector; parallel,concurrent, and incrementally compacting.

7/16

C#

I C# runs on the Common Language Runtime, the virtualmachine from the .NET Framework.

I It also runs on the open source Mono compiler (go-mono.comand gotmono.com).

I Memory is automatically managed– memory is allocated when an object is created, and reclaimedat some point after the object becomes unreachable.

I The Mono runtime comes with two collectors: theBoehm-Demers-Weiser conservative collector and a generationalcopying collector.

8/16

C#

I C# runs on the Common Language Runtime, the virtualmachine from the .NET Framework.

I It also runs on the open source Mono compiler (go-mono.comand gotmono.com).

I Memory is automatically managed– memory is allocated when an object is created, and reclaimedat some point after the object becomes unreachable.

I The Mono runtime comes with two collectors: theBoehm-Demers-Weiser conservative collector and a generationalcopying collector.

8/16

C#

I C# runs on the Common Language Runtime, the virtualmachine from the .NET Framework.

I It also runs on the open source Mono compiler (go-mono.comand gotmono.com).

I Memory is automatically managed– memory is allocated when an object is created, and reclaimedat some point after the object becomes unreachable.

I The Mono runtime comes with two collectors: theBoehm-Demers-Weiser conservative collector and a generationalcopying collector.

8/16

C#

I C# supports finalization (classes may have destructorfunctions, which are run just before the object is reclaimed bythe memory manager), and weak references (via theWeakReference class).

I In OOP, a finalizer is a special method that performs some formof cleanup:– it is executed during object destruction, prior to the objectbeing deallocated

I Weak reference is does not protect the referenced object fromcollection by the GC.

9/16

C#

I C# supports finalization (classes may have destructorfunctions, which are run just before the object is reclaimed bythe memory manager), and weak references (via theWeakReference class).

I In OOP, a finalizer is a special method that performs some formof cleanup:– it is executed during object destruction, prior to the objectbeing deallocated

I Weak reference is does not protect the referenced object fromcollection by the GC.

9/16

C#

I C# supports finalization (classes may have destructorfunctions, which are run just before the object is reclaimed bythe memory manager), and weak references (via theWeakReference class).

I In OOP, a finalizer is a special method that performs some formof cleanup:– it is executed during object destruction, prior to the objectbeing deallocated

I Weak reference is does not protect the referenced object fromcollection by the GC.

9/16

Python

I There are several implementations running on a variety ofvirtual machines:– the original CPython implementation runs on its own virtualmachine– IronPython runs on the Common Language Runtime– Jython on the Java Virtual Machine

10/16

Python

I CPython manages memory using a mixture of referencecounting and non-moving mark-and-sweep garbage collection.

I Reference counting ensures prompt deletion of objects whentheir reference count falls to zero, while the garbage collectorreclaims cyclic data structures.

I The language supports finalization (classes may have a del

method, which is run just before the object is destroyed), andweak references (via the weakref module).

11/16

Reference Counting GC Method

I Every value has associated with it a count of how manyreferences it has.

I It is incremented each time a reference to the object is shared.

I It is decremented whenever a pointer to the object disappears.

I When the count reaches zero, the value’s space can safely berestored for future reuse (garbage collected).

12/16

JavaScript

I JavaScript is a scripting language used by web browsers.

I Despite the C++-like syntax (with new and delete

operators), JavaScript is garbage-collected.

I Mark and Sweep algorithm is used for GC.

I Memory Leaks (accidental global variables, forgotten callbacks,closures)

13/16

JavaScript

I JavaScript is a scripting language used by web browsers.

I Despite the C++-like syntax (with new and delete

operators), JavaScript is garbage-collected.

I Mark and Sweep algorithm is used for GC.

I Memory Leaks (accidental global variables, forgotten callbacks,closures)

13/16

Lisp

I Lisp was invented by John McCarthy around 1958 for themanipulation of symbolic expressions.

I As part of the original implementation of Lisp, he inventedgarbage collection.

“This process, because it is entirely automatic, ismore convenient for the programmer than a system inwhich he has to keep track of lists and erase unwantedlists.” McCarthy

14/16

Lisp

I Lisp was invented by John McCarthy around 1958 for themanipulation of symbolic expressions.

I As part of the original implementation of Lisp, he inventedgarbage collection.

“This process, because it is entirely automatic, ismore convenient for the programmer than a system inwhich he has to keep track of lists and erase unwantedlists.” McCarthy

14/16

PostScript

I The PostScript language is an interpretive language withpowerful graphics features, widely used as a page descriptionlanguage for printers and typesetters.

I Level 1 PostScript language has a simple stack-like memorymanagement model, using save and restore operators torecycle memory.

I In addition, Level 2 and 3 PostScript language also usesautomatic garbage collection.

15/16

PostScript

I The PostScript language is an interpretive language withpowerful graphics features, widely used as a page descriptionlanguage for printers and typesetters.

I Level 1 PostScript language has a simple stack-like memorymanagement model, using save and restore operators torecycle memory.

I In addition, Level 2 and 3 PostScript language also usesautomatic garbage collection.

15/16

PostScript

I The PostScript language is an interpretive language withpowerful graphics features, widely used as a page descriptionlanguage for printers and typesetters.

I Level 1 PostScript language has a simple stack-like memorymanagement model, using save and restore operators torecycle memory.

I In addition, Level 2 and 3 PostScript language also usesautomatic garbage collection.

15/16

Prolog

I A logic programming language invented by Alain Colmeraueraround 1970, Prolog is popular in the AI and symboliccomputation community.

I It deals directly with relationships and inference rather thanfunctions or commands.

I Storage is automatically managed using a garbage collector.

16/16

Prolog

I A logic programming language invented by Alain Colmeraueraround 1970, Prolog is popular in the AI and symboliccomputation community.

I It deals directly with relationships and inference rather thanfunctions or commands.

I Storage is automatically managed using a garbage collector.

16/16