Executing Environment: Garbage
Collection, Virtual Machines
(実行環境、ゴミ集め、仮想計算機、動的コンパイル)
15th lecture, July 15, 2016
Language Theory and Compilers
http://www.sw.it.aoyama.ac.jp/2016/Compiler/lecture15.html
Martin J. Dürst
© 2005-16 Martin
J. Dürst 青山学院大学
Today's Schedule
- Remaining schedule
- Summary of last time
- Executing environment
- Garbage collection
- Virtual machines
- Dynamic compilation
Remaining Schedule
- July 15: Executing environment: virtual machines, garbage
collection,...
- [July 22: Tuesday lectures]
- July 29, 11:10-12:35: Term final exam
Logical Structure of a Compiler
- [preprocessor]
- Lexical analysis
- Parsing (syntax analysis)
- Semantic analysis
- Optimization (or 5)
- Code generation (or 4)
- [assembler]
- [linker, loader]
Relocatable Program
Investigation of Relocatable Programs
- On Unix/Linux/cygwin, use the
nm
(name) command
- Meaning of symbol types:
- Lower case: Local to file
- Upper case: Global across files
- B/b: BSS: Uninitialized data
- D/d: Data: Initialized data
- R/r: Read only data
- T/t: Text: Code (program "text")
- U: Undefined
- Can also be used on executables
Execution Environment
Has to be provided together with the compiler
- Processing of command-line arguments
- Processing of environment variables
- Exception processing
- Functions/libraries for input/output,...
- Memory management
Traditional Dynamic Memory Management
- Dynamic memory in C:
- Obtain "raw" memory with
malloc
- Give memory back with
free
- Dynamic memory in C++:
- Create a new object with
new
(includes memory
allocation)
- Finalize an object with
delete
(includes freeing
memory)
- Problem:
- Locations for allocation and freeing differ
- Memory may be given back too early or never (memory leak)
- Solition:
Garbage Collection
- Manual dynamic memory management is手間が多く and error-prone
- Allocating memory when creating new objects is easy
(
new
,...)
- Automatically collect unused (unusable) memory
- This is called garbage collection or GC
How Garbage Collection Works
- In programming languages that do not use raw pointers, the type of each
data item is fixed
- This allows to detect all references to other objects
- The memory of objects that are never referenced is no longer used
⇒ It can be collected and reused
- GC demo: garbage.rb (observe memory usage during
execution with the task manager)
Advantages and Disadvantages of GC
- Advantages of GC:
- Programmers do not have to think about memory management
- Programming efficiency increases
- Memory leaks are (mostly) avoided
- Problems of GC:
- GC needs time
- GC cannot run in parallel to other operations
⇒ program may seem to stop for a while
- Currently, most programming languages (except C, C++, ObjectiveC,...) use
GC
Methods for GC
- Reference count(ing) GC
- Mark and Sweep GC
- Copying GC
- Generational GC
- Combinations of the above methods,...
Reference Count(ing) GC
- For each object, count the number of times it is referenced (reference
count)
- If a reference is copied (assigned), increment the count
- If a reference is overwritten, decrement the count
- If the count becomes 0, the memory can be reclaimed
- Advantage: Distribution of overhead (no long pauses for GC)
- Disadvantages:
- Overhead during normal operation
- Objects with cyclic references cannot be garbage collected
Mark and Sweep GC
(mark and sweep GC)
Copying GC
(usually used together with mark and sweep)
- Prepare two areas for dynamic memory
- Use one area for program execution
- Recursively follow references and copy objects to second area
(references need to be rewritten)
- Advantage: Compaction of dynamic memory
- Problem: More memory needed, long pauses during execution
Generational GC
- Some data lives long, others only a short time
- Split dynamic memory into two or more "generations"
- Garbage-collect young(er) generation(s) often
- Promote objects in young(er) generation(s) after they survive GC a
certain number of times
- Garbage-collect old(er) generation(s) only rarely
- Problem: References from older to newer generations need special care
Virtual Machine
- Goal: Increase portability, simplify compiler implementation
- Mechanism:
- Compiler produces code for a machine that does not exist
physically
- This code is interpreted and executed by a program (emulator)
- Examples: Pascal, JVM: Java
Virtual Machine, Ruby 1.9~ (YARV), ...
MacIntosh: Emulating Motorola 680x0 on IBM PowerPC, emulating PowerPC on
Intel i386
- Problem: Slower than machine code (in general, between 3 and 10 times
slower)
- Similar: cygwin, wine, VMWare, Virtual
PC, VirtualBox,...
(only the OS and libraries, not the machine itself is emulated)
Dynamic Compilation
- Goal: Improve efficiency of virtual machines and emulators
- Mechanism (during program execution):
- Check execution count of functions and blocks
⇒ Compile frequently used functions/blocks to hardware code
- Check use of frequent parameter values (e.g. 0, 1) or types (for
object-oriented and dynamic languages)
⇒ Recompile to optimize frequent special cases
- Problems:
- Advanced technology needed
- Overhead may be greater than savings
- Examples: Java
HotSpot, LLVM (used by Apple), Chrome V8 (for JavaScript)
Summary
- In most programs, analysis of input is more difficult than production of
output
- Acceptance/rejection of input can be modelled by formal language
theory
- Formal language theory is the basis for the front-end of compilers:
Lexical analysis and parsing
- Formal language theory and knowledge about lexical analysis and parsing
is useful in many other areas
- Design and implementation of programming languages is still a very
interesting and important area of research
Glossary
- relocatable program
- 再配置可能プログラム
- environment variable
- 環境変数
- exception
- 例外
- garbage collection
- ゴミ集め
- reference count(ing) GC
- 参照カウント GC
- mark and sweep GC
- 印掃式 GC
- copying GC
- 複写式 GC
- generational GC
- 世代別 GC
- generation
- 世代
- virtual machine
- 仮想計算機、仮想マシーン
- portability
- 移植性
- emulate
- エミュレート