Programming Language Concepts Memory Management in Different
Total Page:16
File Type:pdf, Size:1020Kb
Programming Language Concepts Memory management in different languages Janyl Jumadinova 13 April, 2017 I Use external software, such as the Boehm-Demers-Weiser collector (a.k.a. Boehm GC), to do garbage collection in C/C++: { use Boehm instead of traditional malloc and free in C http://hboehm.info/gc/ C I Memory management is typically manual: { the standard library functions for memory management in C, malloc and free, have become almost synonymous with manual memory management. 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 manual memory management. I Use external software, such as the Boehm-Demers-Weiser collector (a.k.a. Boehm GC), to do garbage collection in C/C++: { use Boehm instead of traditional malloc and free in C http://hboehm.info/gc/ 2/16 I In addition to Boehm, we can use smart pointers as a memory management solution. C++ I The standard library functions for memory management in C++ are new and delete. I The higher abstraction level of C++ makes the bookkeeping required for manual memory management even harder than C. 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 bookkeeping required for manual memory management even harder than C. I In addition to Boehm, we can use smart pointers as a memory management solution. 3/16 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 C++ Raw pointer: MyClass *ptr = new MyClass(); ptr->doSomething(); delete ptr; // destroy the object. 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 large programs. 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 Permanent Generation. I -XX:MaxPermSize: Sets the maximum size of the Permanent Generation. java -Xmx12m -Xms3m -XX:+UseG1GC -jar c:..Java2demo.jar 6/16 Java I Serial GC: both minor and major garbage collections are done serially I Parallel GC: uses multiple threads to perform the young generation garbage collection I Concurrent Mark Sweep (CMS): collects the tenured generation, does most of the garbage collection work concurrently with the application threads I G1 Garbage Collector: available in Java 7 and is designed to be the long term replacement for the CMS collector; parallel, concurrent, and incrementally compacting. 7/16 I Memory is automatically managed { memory is allocated when an object is created, and reclaimed at some point after the object becomes unreachable. I The Mono runtime comes with two collectors: the Boehm-Demers-Weiser conservative collector and a generational copying collector. C# I C# runs on the Common Language Runtime, the virtual machine from the .NET Framework. I It also runs on the open source Mono compiler (go-mono.com and gotmono.com). 8/16 I The Mono runtime comes with two collectors: the Boehm-Demers-Weiser conservative collector and a generational copying collector. C# I C# runs on the Common Language Runtime, the virtual machine from the .NET Framework. I It also runs on the open source Mono compiler (go-mono.com and gotmono.com). I Memory is automatically managed { memory is allocated when an object is created, and reclaimed at some point after the object becomes unreachable. 8/16 C# I C# runs on the Common Language Runtime, the virtual machine from the .NET Framework. I It also runs on the open source Mono compiler (go-mono.com and gotmono.com). I Memory is automatically managed { memory is allocated when an object is created, and reclaimed at some point after the object becomes unreachable. I The Mono runtime comes with two collectors: the Boehm-Demers-Weiser conservative collector and a generational copying collector. 8/16 I In OOP, a finalizer is a special method that performs some form of cleanup: { it is executed during object destruction, prior to the object being deallocated I Weak reference is does not protect the referenced object from collection by the GC. C# I C# supports finalization (classes may have destructor functions, which are run just before the object is reclaimed by the memory manager), and weak references (via the WeakReference class). 9/16 I Weak reference is does not protect the referenced object from collection by the GC. C# I C# supports finalization (classes may have destructor functions, which are run just before the object is reclaimed by the memory manager), and weak references (via the WeakReference class). I In OOP, a finalizer is a special method that performs some form of cleanup: { it is executed during object destruction, prior to the object being deallocated 9/16 C# I C# supports finalization (classes may have destructor functions, which are run just before the object is reclaimed by the memory manager), and weak references (via the WeakReference class). I In OOP, a finalizer is a special method that performs some form of cleanup: { it is executed during object destruction, prior to the object being deallocated I Weak reference is does not protect the referenced object from collection by the GC. 9/16 Python I There are several implementations running on a variety of virtual machines: { the original CPython implementation runs on its own virtual machine { IronPython runs on the Common Language Runtime { Jython on the Java Virtual Machine 10/16 Python I CPython manages memory using a mixture of reference counting and non-moving mark-and-sweep garbage collection. I Reference counting ensures prompt deletion of objects when their reference count falls to zero, while the garbage collector reclaims cyclic data structures. I The language supports finalization (classes may have a del method, which is run just before the object is destroyed), and weak references (via the weakref module). 11/16 Reference Counting GC Method I Every value has associated with it a count of how many references 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 be restored for future reuse (garbage collected). 12/16 I Memory Leaks (accidental global variables, forgotten callbacks, closures) 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. 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 \This process, because it is entirely automatic, is more convenient for the programmer than a system in which he has to keep track of lists and erase unwanted lists." McCarthy Lisp I Lisp was invented by John McCarthy around 1958 for the manipulation of symbolic expressions. I As part of the original implementation of Lisp, he invented garbage collection. 14/16 Lisp I Lisp was invented by John McCarthy around 1958 for the manipulation of symbolic expressions. I As part of the original implementation of Lisp, he invented garbage collection. \This process, because it is entirely automatic, is more convenient for the programmer than a system in which he has to keep track of lists and erase unwanted lists." McCarthy 14/16 I Level 1 PostScript language has a simple stack-like memory management model, using save and restore operators to recycle memory. I In addition, Level 2 and 3 PostScript language also uses automatic garbage collection. PostScript I The PostScript language is an interpretive language with powerful graphics features, widely used as a page description language for printers and typesetters. 15/16 I In addition, Level 2 and 3 PostScript language also uses automatic garbage collection. PostScript I The PostScript language is an interpretive language with powerful graphics features, widely used as a page description language for printers and typesetters. I Level 1 PostScript language has a simple stack-like memory management model, using save and restore operators to recycle memory. 15/16 PostScript I The PostScript language is an interpretive language with powerful graphics features, widely used as a page description language for printers and typesetters. I Level 1 PostScript language has a simple stack-like memory management model, using save and restore operators to recycle memory. I In addition, Level 2 and 3 PostScript language also uses automatic garbage collection. 15/16 I Storage is automatically managed using a garbage collector. Prolog I A logic programming language invented by Alain Colmerauer around 1970, Prolog is popular in the AI and symbolic computation community. I It deals directly with relationships and inference rather than functions or commands. 16/16 Prolog I A logic programming language invented by Alain Colmerauer around 1970, Prolog is popular in the AI and symbolic computation community. I It deals directly with relationships and inference rather than functions or commands.