memory management in the d programming language
DESCRIPTION
Ministerul Educaţiei şi Tineretului al Republicii Moldova Universitatea Tehnică a Moldovei Facultatea de Calculatoare, Informatică şi Microelectronică Catedra Filiera Anglofonă. Memory Management in the D programming language. Student: Panteleev Vladimir, FAF-051 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/1.jpg)
MEMORY MANAGEMENT IN THE D PROGRAMMING
LANGUAGEStudent: Panteleev Vladimir, FAF-051Adviser: Lect. Sup. Radu Melnic
Ministerul Educaţiei şi Tineretului al Republicii Moldova
Universitatea Tehnică a Moldovei Facultatea de Calculatoare, Informatică şi
MicroelectronicăCatedra Filiera Anglofonă
![Page 2: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/2.jpg)
D Programming Language
Jan 1 2006 Jul 2 2006 Dec 31 2006 Jul 1 2007 Dec 30 2007Jun 29 2008Dec 28 20080
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
DRubyDelphi
Release of D version 1.0
![Page 3: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/3.jpg)
D Programming Language
• Classes• Templates
Improved Features
• Design-by-contract• Unit testing• Automatic memory
management
New features
![Page 4: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/4.jpg)
Automatic memory management
Object obj = new Object();
// ...
delete obj;
![Page 5: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/5.jpg)
Reference Tracking
Root set of
references
Heap Garbage
Garbage
Garbage
Garbage
![Page 6: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/6.jpg)
Problems solved by automatic memory management
“Classic” memory leaks Memory corruption problems
Dangling pointer bugsDouble free bugs
![Page 7: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/7.jpg)
Automatic memory management downsides
GC runs – unpredictable All threads must stop Conservative GCs may not destroy all
objects
![Page 8: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/8.jpg)
Garbage Collector Classification
Garbage Collectors
TracingReference Counting
moving / non-moving copying / mark-and-sweep
/ mark-and-don't-sweep generational /
non‑generational stop-the-world /
incremental precise / conservative
![Page 9: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/9.jpg)
Garbage Collector Classification
Garbage Collectors
TracingReference Counting
moving / non-moving copying / mark-and-sweep
/ mark-and-don't-sweep generational /
non‑generational stop-the-world /
incremental precise / conservative
![Page 10: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/10.jpg)
Memory Management in D
![Page 11: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/11.jpg)
Runtime InteractionC/C++
D
User Code libc Operating System
User Code lib
c Linux/OS XD
runtimeWindows
![Page 12: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/12.jpg)
Memory layout
Memory is divided into pools (multiples of 64kB)
Pools are divided into pages (4kB each)
Pages are classified into bins
![Page 13: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/13.jpg)
Memory layout
B_16 B_32 B_64 B_128 B_256 B_512 B_1024 B_2048 B_PAGE B_PAGEPLUS B_FREE B_UNCOMMITTED
![Page 14: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/14.jpg)
Memory layout
PAGE PAGE_PLUS
![Page 15: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/15.jpg)
Memory layout
MARK SCAN FREE FINAL NOSCAN
![Page 16: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/16.jpg)
Memory layout
MARK SCAN FREE FINAL NOSCAN
![Page 17: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/17.jpg)
Garbage collection
![Page 18: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/18.jpg)
Garbage collection
![Page 19: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/19.jpg)
Garbage collection
![Page 20: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/20.jpg)
Garbage collection
![Page 21: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/21.jpg)
Garbage collection
![Page 22: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/22.jpg)
Garbage collection
![Page 23: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/23.jpg)
Garbage collection
![Page 24: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/24.jpg)
Analyzed Problems
Poor performance Memory Leaks Memory corruption
![Page 25: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/25.jpg)
Poor Performance
![Page 26: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/26.jpg)
Poor Performance
![Page 27: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/27.jpg)
Poor Performance
![Page 28: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/28.jpg)
Poor Performance
Withou
t opti
mizatio
n
With op
timiza
tion
0.0s1.0s2.0s3.0s
Garbage collectionSplitting timeLoading time
![Page 29: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/29.jpg)
Poor Performance
![Page 30: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/30.jpg)
Memory Leaks
Very different from “conventional” leaks
Usually mean that no memory is deallocated at all
No tools to debug them
![Page 31: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/31.jpg)
Memory Leaks
… 0xC0580F1D 0x3612796C 0x8A6B3F92 0x8D6F85DB 0x4B0A8948 0x703DCE69 0xCAEB2C0E …
![Page 32: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/32.jpg)
Memory Leaks
… 0xC0580F1D 0x3612796C 0x8A6B3F92 0x8D6F85DB 0x4B0A8948 0x703DCE69 0xCAEB2C0E …
![Page 33: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/33.jpg)
Memory Leaks
M is block with pointers N is block being referenced
For sizes of 1MB (220), P(M,N) ≈ 1
![Page 34: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/34.jpg)
Memory Leaks
import std.stdio;// ...
auto result = read("file1") ~ read("file2");
![Page 35: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/35.jpg)
Diamond Memory Debugger
D module
Post-mortem log analyzer
![Page 36: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/36.jpg)
Diamond Memory Debugger
D program Diamond module
D garbage collector
Log file Log analyzer
![Page 37: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/37.jpg)
Diamond Memory Debugger
![Page 38: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/38.jpg)
Diamond Memory Debugger
![Page 39: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/39.jpg)
Diamond Memory Debugger
![Page 40: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/40.jpg)
Memory corruption
Usually happens with manual memory management
Hard to debug Symptoms manifest far from
cause Standard memory debugging
tools do not apply for D
![Page 41: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/41.jpg)
Double Free Bug
auto a = new ubyte[10];
auto b = a;
delete a;
delete b;
![Page 42: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/42.jpg)
Dangling Pointer Bug
auto a = new int[5];a[3] = 7;auto b = a;delete a;writefln(b[3]);
![Page 43: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/43.jpg)
Practical Applications
WebSafety scanner Severe memory leak Diamond reduced memory usage
tenfold Internet proxy
Severe stability problems due to memory leak
Diamond fixed memory leak, stability achieved
![Page 44: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/44.jpg)
Practical Application: WebSafety Scanner
![Page 45: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/45.jpg)
Practical Application: WebSafety Scanner
![Page 46: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/46.jpg)
Practical Application: WebSafety Scanner
![Page 47: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/47.jpg)
Practical Application: Internet Data Proxy
![Page 48: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/48.jpg)
Practical Application: Internet Data Proxy
![Page 49: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/49.jpg)
Diamond Memory Debugger
Free, open-source debugging tool Unique – no similar tools exist for
the D programming language Can be downloaded for free from
http://dsource.org/projects/diamond
![Page 50: Memory Management in the D programming language](https://reader035.vdocuments.net/reader035/viewer/2022062411/56816922550346895de054a2/html5/thumbnails/50.jpg)
Conclusion Results of research have impact on
the programming world of today Automatic memory management
in compiled languages is a relatively new area
More efficient garbage collectors can be written
There is more research to be done