Implementing Garbage Collection forActive Objects on Top of ErlangSigmund HansenMaster thesis autumn 2014
Problem
I ABS is a modeling language for distributed systemsI ABS has an Erlang back endI Back end cannot collect garbage processes
GoalsThe garbage collector should be:
I FastIt should not considerably slow down simulations
I ComprehensiveIt should collect most or all garbage
I CorrectIt should not collect resources required later
ABS Overview
I Executable modelsI Functional subsetI Object-orientedI Concurrency:
I Active objectsI Scheduling in Concurrent Object Groups (COGs)I RPC with message passing
Erlang Overview
I Concurrency:I Actor modelI Message passing
I Fault-tolerantI Functional
Garbage Collection OverviewAutomatic memory management for dynamically allocatedmemory.
I Reference countingI Tracing
I Object graph traversalI De-allocation of unmarked objects
Active Object Collection
I Kept alive by associated processI Must kill the processI High-level implementation
Choice of Algorithm
I Reference counting cannot collect cyclic garbage.I Killing processes, not collecting memory,
i.e. no moving collectorsI Greater risk with non-standard collection algorithms
Mark and sweep that stops the world implemented.
Stopping the World
I No interruptsI Stop by messagingI Tasks and scheduling must stop
Blocking Operations
I Blocking on futuresI Blocking on object instantiationI Inappropriate COG state during block
COG State Machine (Before)
Not running
Running None found
[runnable task found]token,task error
[no runnable task found]new task,task state changed,task error
new task,task state changed
COG State Machine (After)
Not0running
Stopped
Resume0state
Running
None0found
Blocked
Blocked0task
ok
stop0world
[runnable0task0found]token,task0error
[no0runnable0task0found]
new0task,task0state0changed,task0error,inc/dec0reference0count
stop0world
resume0world[reference0count0!=00,Resume0state0=0Not0running]
stop0world
new0task,task0state0change,inc/dec0reference0count
stop0world
running0task0statechanged0to0blocked
blocked0task0statechanged0to0runnable
stop0worldresume0world[reference0count0!=00,Resume0state0=0Blocked]
new0task,task0state0change,inc/dec0reference0count
get0references,inc/dec0reference0count
resume0world[reference0count0=00]
Stopping Running Tasks
I Will reduce waitingI COG messages taskI Task then blocks
Asynchronous Method Calls onInactive Objects
I Task initialization is synchronousI Waits until object is activatedI Respond to GC messages while waiting
Tasks CreatedWhile the World is Stopping
I Task is created by a futureI Stopped COGs do not receive task creation messagesI Futures temporarily become roots and keep parameters
Future State Machine (Before)
Loop
Result
init
Await_start
Parameters
Await_completion
Started
Taskcompleted
Taskerror
Future State Machine (After)
Wait
Loop
Result
init
Await_start
Parametersget_references
Started
Startedget_references
Taskcomplete
Taskerror
die
A Complete Viewof COGs and FuturesWhy is it required?
Marking Phase
I Messages all grays in parallelI BN+1 = BN ∪ GN
I GN+1 = References(GN) \ BN+1
I Sweeps when gray set is empty
Sweeping
I Identifies white objectsI Identifies white futuresI Sends message to kill themI Resume message sent to COGs
Collecting COGs with ReferenceCounting
I Counts objects left in groupI COG stops if world resumes and it is emptyI Messages garbage collector
Static analysis
I Types that may contain referencesI Reaching unawaited futuresI Other potential analyses:
I Loops with synchronization pointsI Deep functions
Triggering Garbage CollectionWhen should garbage be collected:
I On time?I Number of objects and futures?I Process ratio?
Data Collection
I Garbage collector state changesI Number of objects and futures sweptI Memory usage and number of objects, futures and COGs
Test Cases
I Ping pongBasic test with cyclic garbage
I SequencesUnstoppable loop
I Prime SieveLong-running tasks
I MapReduce - IndexingReal-world test case
Ping Pong - Time
Collection scheme Real time Relative Relative without idle
Before GC 1274 ms 100.0 % 100.0 %Never collect 1257 ms 98.6 % 93.5 %Always collect 1854 ms 145.5 % 311.3 %Collect on count 1293 ms 101.4 % 106.7 %Collect on time 1261 ms 98.9 % 94.9 %+ Stop running 1265 ms 99.2 % 96.5 %Collect on either 1290 ms 101.2 % 105.5 %+ Stop running 1298 ms 101.8 % 108.4 %
Ping Pong - No GC
0
250
500
750
14
16
18
TotalM
emory in M
iB
0 ms 100 ms 200 ms 300 ms 400 ms 500 msTime
variable
cogs
objects
futures
Ping Pong - Always collecting
0
100
200
300
12.5
15.0
17.5
20.0
22.5
TotalM
emory in M
iB
0 ms 1220 ms 2440 ms 3660 ms 4880 ms 6100 msTime
variable
cogs
objects
futures
Ping Pong - By count
0
20
40
60
13.5
14.0
14.5
15.0
15.5
TotalM
emory in M
iB
0 ms 140 ms 280 ms 420 ms 560 ms 700 msTime
variable
cogs
objects
futures
Ping Pong - Timed
0
50
100
150
200
13.5
14.0
14.5
15.0
15.5
16.0
16.5
TotalM
emory in M
iB
0 ms 100 ms 200 ms 300 ms 400 ms 500 msTime
variable
cogs
objects
futures
Infinite Ping Pong - No GC
0
50000
100000
150000
0
300
600
900
1200
TotalM
emory in M
iB
0 ms 31120 ms 62240 ms 93360 ms 124480 ms 155600 msTime
variable
cogs
objects
futures
Infinite Ping Pong - Timed
0
2000
4000
6000
8000
50
100
TotalM
emory in M
iB
0 ms 11900 ms 23800 ms 35700 ms 47600 ms 59500 msTime
variable
cogs
objects
futures
Sequences - Time
Collection scheme Real time Relative Relative without idle
Before GC 1395 ms 100.0 % 100.0 %Never collect 1439 ms 103.2 % 111.2 %Always collect 39159 ms 2807.2 % 9662.1 %Collect on count 16801 ms 1204.4 % 4001.0 %Collect on time 1622 ms 116.3 % 157.5 %+ Stop running 1530 ms 109.7 % 134.1 %Collect on either 17054 ms 1222.6 % 4065.1 %+ Stop running 12614 ms 904.3 % 2940.8 %
Sequences - No GC
0
1000
2000
3000
4000
5000
20
30
40
50
60
70
TotalM
emory in M
iB
0 ms 720 ms 1440 ms 2160 ms 2880 ms 3600 msTime
variable
cogs
objects
futures
Sequences - Timed
0
1000
2000
3000
4000
5000
20
30
40
50
60
TotalM
emory in M
iB
0 ms 860 ms 1720 ms 2580 ms 3440 ms 4300 msTime
variable
cogs
objects
futures
Sequences -Timed stopping running processes
0
200
400
600
16
20
24Total
Mem
ory in MiB
0 ms 700 ms 1400 ms 2100 ms 2800 ms 3500 msTime
variable
cogs
objects
futures
Prime Sieve - Time
Collection scheme Real time Relative Relative without idle
Before GC 1448 ms 100.0 % 100.0 %Never collect 1453 ms 100.3 % 101.1 %Always collect 1903 ms 131.4 % 201.6 %Collect on count 1482 ms 102.4 % 107.6 %Collect on time 1454 ms 100.4 % 101.3 %+ Stop running 1542 ms 106.5 % 120.9 %Collect on either 1472 ms 101.6 % 105.3 %+ Stop running 1545 ms 106.7 % 121.6 %
Indexing - Time
Collection scheme Real time Relative Relative without idle
Before GC 1356 ms 100.0 % 100.0 %Never collect 1373 ms 101.2 % 104.7 %Always collect 5043 ms 371.8 % 1134.2 %Collect on count 1408 ms 103.9 % 114.7 %Collect on time 1398 ms 103.1 % 111.7 %+ Stop running 1405 ms 103.6 % 113.9 %Collect on either 1410 ms 104.0 % 115.3 %+ Stop running 1434 ms 105.7 % 121.9 %
ConclusionTimed trigger gives acceptable results.
I Acceptably fastI Mark and sweep is completeI Measures taken stopping the world to ensure correctness
DiscussionTesting with infinite loops have discouraging results. Futurework to lower overhead may be needed:
I Optimize stopping the worldI Optimize markingI Concurrent collectionI Change of algorithm