<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
reference	B-General_Concept
counting	I-General_Concept
is	O
a	O
programming	O
technique	O
of	O
storing	O
the	O
number	O
of	O
references	O
,	O
pointers	O
,	O
or	O
handles	O
to	O
a	O
resource	O
,	O
such	O
as	O
an	O
object	O
,	O
a	O
block	O
of	O
memory	O
,	O
disk	O
space	O
,	O
and	O
others	O
.	O
</s>
<s>
In	O
garbage	B-General_Concept
collection	I-General_Concept
algorithms	O
,	O
reference	B-General_Concept
counts	I-General_Concept
may	O
be	O
used	O
to	O
deallocate	O
objects	O
that	O
are	O
no	O
longer	O
needed	O
.	O
</s>
<s>
The	O
main	O
advantage	O
of	O
the	O
reference	B-General_Concept
counting	I-General_Concept
over	O
tracing	B-General_Concept
garbage	I-General_Concept
collection	I-General_Concept
is	O
that	O
objects	O
are	O
reclaimed	O
as	O
soon	O
as	O
they	O
can	O
no	O
longer	O
be	O
referenced	O
,	O
and	O
in	O
an	O
incremental	O
fashion	O
,	O
without	O
long	O
pauses	O
for	O
collection	O
cycles	O
and	O
with	O
clearly	O
defined	O
lifetime	O
of	O
every	O
object	O
.	O
</s>
<s>
Reference	B-General_Concept
counting	I-General_Concept
is	O
also	O
among	O
the	O
simplest	O
forms	O
of	O
memory	O
management	O
to	O
implement	O
.	O
</s>
<s>
It	O
also	O
allows	O
for	O
effective	O
management	O
of	O
non-memory	O
resources	O
such	O
as	O
operating	O
system	O
objects	O
,	O
which	O
are	O
often	O
much	O
scarcer	O
than	O
memory	O
(	O
tracing	B-General_Concept
garbage	I-General_Concept
collection	I-General_Concept
systems	O
use	O
finalizers	B-Application
for	O
this	O
,	O
but	O
the	O
delayed	O
reclamation	O
may	O
cause	O
problems	O
)	O
.	O
</s>
<s>
Weighted	O
reference	B-General_Concept
counts	I-General_Concept
are	O
a	O
good	O
solution	O
for	O
garbage	O
collecting	O
a	O
distributed	O
system	O
.	O
</s>
<s>
Tracing	B-General_Concept
garbage	I-General_Concept
collection	I-General_Concept
cycles	O
are	O
triggered	O
too	O
often	O
if	O
the	O
set	O
of	O
live	O
objects	O
fills	O
most	O
of	O
the	O
available	O
memory	O
;	O
it	O
requires	O
extra	O
space	O
to	O
be	O
efficient	O
.	O
</s>
<s>
Reference	B-General_Concept
counting	I-General_Concept
performance	O
does	O
not	O
deteriorate	O
as	O
the	O
total	O
amount	O
of	O
free	O
space	O
decreases	O
.	O
</s>
<s>
Reference	B-General_Concept
counts	I-General_Concept
are	O
also	O
useful	O
information	O
to	O
use	O
as	O
input	O
to	O
other	O
runtime	O
optimizations	O
.	O
</s>
<s>
For	O
example	O
,	O
systems	O
that	O
depend	O
heavily	O
on	O
immutable	B-Application
objects	I-Application
such	O
as	O
many	O
functional	B-Language
programming	I-Language
languages	I-Language
can	O
suffer	O
an	O
efficiency	O
penalty	O
due	O
to	O
frequent	O
copies	O
.	O
</s>
<s>
However	O
,	O
if	O
the	O
compiler	O
(	O
or	O
runtime	B-Device
system	I-Device
)	O
knows	O
that	O
a	O
particular	O
object	O
has	O
only	O
one	O
reference	O
(	O
as	O
most	O
do	O
in	O
many	O
systems	O
)	O
,	O
and	O
that	O
the	O
reference	O
is	O
lost	O
at	O
the	O
same	O
time	O
that	O
a	O
similar	O
new	O
object	O
is	O
created	O
(	O
as	O
in	O
the	O
string	O
append	O
statement	O
str	O
←	O
str	O
+	O
"	O
a	O
"	O
)	O
,	O
it	O
can	O
replace	O
the	O
operation	O
with	O
a	O
mutation	O
on	O
the	O
original	O
object	O
.	O
</s>
<s>
Reference	B-General_Concept
counting	I-General_Concept
in	O
naive	O
form	O
has	O
three	O
main	O
disadvantages	O
over	O
the	O
tracing	B-General_Concept
garbage	I-General_Concept
collection	I-General_Concept
,	O
both	O
of	O
which	O
require	O
additional	O
mechanisms	O
to	O
ameliorate	O
:	O
</s>
<s>
While	O
tracing	B-General_Concept
garbage	I-General_Concept
collectors	I-General_Concept
can	O
impact	O
efficiency	O
severely	O
via	O
context	B-Operating_System
switching	I-Operating_System
and	O
cache	B-General_Concept
line	O
faults	O
,	O
they	O
collect	O
relatively	O
infrequently	O
,	O
while	O
accessing	O
objects	O
is	O
done	O
continually	O
.	O
</s>
<s>
Also	O
,	O
less	O
importantly	O
,	O
reference	B-General_Concept
counting	I-General_Concept
requires	O
every	O
memory-managed	O
object	O
to	O
reserve	O
space	O
for	O
a	O
reference	B-General_Concept
count	I-General_Concept
.	O
</s>
<s>
In	O
tracing	B-General_Concept
garbage	I-General_Concept
collectors	I-General_Concept
,	O
this	O
information	O
is	O
stored	O
implicitly	O
in	O
the	O
references	O
that	O
refer	O
to	O
that	O
object	O
,	O
saving	O
space	O
,	O
although	O
tracing	B-General_Concept
garbage	I-General_Concept
collectors	I-General_Concept
,	O
particularly	O
incremental	O
ones	O
,	O
can	O
require	O
additional	O
space	O
for	O
other	O
purposes	O
.	O
</s>
<s>
A	O
mechanism	O
relying	O
purely	O
on	O
reference	B-General_Concept
counts	I-General_Concept
will	O
never	O
consider	O
cyclic	O
chains	O
of	O
objects	O
for	O
deletion	O
,	O
since	O
their	O
reference	B-General_Concept
count	I-General_Concept
is	O
guaranteed	O
to	O
stay	O
nonzero	O
(	O
cf	O
.	O
</s>
<s>
Methods	O
for	O
dealing	O
with	O
this	O
issue	O
exist	O
but	O
can	O
also	O
increase	O
the	O
overhead	O
and	O
complexity	O
of	O
reference	B-General_Concept
counting	I-General_Concept
—	O
on	O
the	O
other	O
hand	O
,	O
these	O
methods	O
need	O
only	O
be	O
applied	O
to	O
data	O
that	O
might	O
form	O
cycles	O
,	O
often	O
a	O
small	O
subset	O
of	O
all	O
data	O
.	O
</s>
<s>
One	O
such	O
method	O
is	O
the	O
use	O
of	O
weak	B-General_Concept
references	I-General_Concept
,	O
while	O
another	O
involves	O
using	O
a	O
mark-sweep	O
algorithm	O
that	O
gets	O
called	O
infrequently	O
to	O
clean	O
up	O
.	O
</s>
<s>
In	O
a	O
concurrent	O
setting	O
,	O
all	O
updates	O
of	O
the	O
reference	B-General_Concept
counts	I-General_Concept
and	O
all	O
pointer	O
modifications	O
must	O
be	O
atomic	B-General_Concept
operations	I-General_Concept
,	O
which	O
incurs	O
an	O
additional	O
cost	O
.	O
</s>
<s>
There	O
are	O
three	O
reasons	O
for	O
the	O
atomicity	B-General_Concept
requirements	O
.	O
</s>
<s>
First	O
,	O
a	O
reference	B-General_Concept
count	I-General_Concept
field	O
may	O
be	O
updated	O
by	O
multiple	O
threads	O
,	O
and	O
so	O
an	O
adequate	O
atomic	B-General_Concept
instruction	I-General_Concept
,	O
such	O
as	O
a	O
(	O
costly	O
)	O
compare-and-swap	O
,	O
must	O
be	O
used	O
to	O
update	O
the	O
counts	O
.	O
</s>
<s>
Second	O
,	O
it	O
must	O
be	O
clear	O
which	O
object	O
loses	O
a	O
reference	O
so	O
that	O
its	O
reference	B-General_Concept
count	I-General_Concept
can	O
be	O
adequately	O
decremented	O
.	O
</s>
<s>
Finally	O
,	O
there	O
exists	O
a	O
subtle	O
race	O
in	O
which	O
one	O
thread	O
gains	O
a	O
pointer	O
to	O
an	O
object	O
,	O
but	O
before	O
it	O
increments	O
the	O
object	O
's	O
reference	B-General_Concept
count	I-General_Concept
,	O
all	O
other	O
references	O
to	O
this	O
object	O
are	O
deleted	O
concurrently	O
by	O
other	O
threads	O
and	O
the	O
object	O
is	O
reclaimed	O
,	O
causing	O
the	O
said	O
thread	O
to	O
increment	O
a	O
reference	B-General_Concept
count	I-General_Concept
of	O
a	O
reclaimed	O
object	O
.	O
</s>
<s>
In	O
addition	O
to	O
these	O
,	O
if	O
the	O
memory	O
is	O
allocated	O
from	O
a	O
free	O
list	O
,	O
reference	B-General_Concept
counting	I-General_Concept
suffers	O
from	O
poor	O
locality	O
.	O
</s>
<s>
Reference	B-General_Concept
counting	I-General_Concept
alone	O
cannot	O
move	O
objects	O
to	O
improve	O
cache	B-General_Concept
performance	O
,	O
so	O
high	O
performance	O
collectors	O
implement	O
a	O
tracing	B-General_Concept
garbage	I-General_Concept
collector	I-General_Concept
as	O
well	O
.	O
</s>
<s>
Most	O
implementations	O
(	O
such	O
as	O
the	O
ones	O
in	O
PHP	B-Application
and	O
Objective-C	O
)	O
suffer	O
from	O
poor	O
cache	B-General_Concept
performance	O
since	O
they	O
do	O
not	O
implement	O
copying	O
objects	O
.	O
</s>
<s>
When	O
dealing	O
with	O
garbage	B-General_Concept
collection	I-General_Concept
schemes	O
,	O
it	O
is	O
often	O
helpful	O
to	O
think	O
of	O
the	O
reference	O
graph	B-Application
,	O
which	O
is	O
a	O
directed	O
graph	B-Application
where	O
the	O
vertices	O
are	O
objects	O
and	O
there	O
is	O
an	O
edge	O
from	O
an	O
objectA	O
to	O
an	O
objectB	O
if	O
A	O
holds	O
a	O
reference	O
toB	O
.	O
</s>
<s>
We	O
also	O
have	O
a	O
special	O
vertex	O
or	O
vertices	O
representing	O
the	O
local	O
variables	O
and	O
references	O
held	O
by	O
the	O
runtime	B-Device
system	I-Device
,	O
and	O
no	O
edges	O
ever	O
go	O
to	O
these	O
nodes	O
,	O
although	O
edges	O
can	O
go	O
from	O
them	O
to	O
other	O
nodes	O
.	O
</s>
<s>
In	O
this	O
context	O
,	O
the	O
simple	O
reference	B-General_Concept
count	I-General_Concept
of	O
an	O
object	O
is	O
the	O
in-degree	O
of	O
its	O
vertex	O
.	O
</s>
<s>
The	O
connected	O
component	O
containing	O
the	O
special	O
vertex	O
contains	O
the	O
objects	O
that	O
ca	O
n't	O
be	O
collected	O
,	O
while	O
other	O
connected	O
components	O
of	O
the	O
graph	B-Application
only	O
contain	O
garbage	O
.	O
</s>
<s>
If	O
a	O
reference-counting	O
garbage	B-General_Concept
collection	I-General_Concept
algorithm	O
is	O
implemented	O
,	O
then	O
each	O
of	O
these	O
garbage	O
components	O
must	O
contain	O
at	O
least	O
one	O
cycle	O
;	O
otherwise	O
,	O
they	O
would	O
have	O
been	O
collected	O
as	O
soon	O
as	O
their	O
reference	B-General_Concept
count	I-General_Concept
(	O
i.e.	O
,	O
the	O
number	O
of	O
incoming	O
edges	O
)	O
dropped	O
to	O
zero	O
.	O
</s>
<s>
Incrementing	O
and	O
decrementing	O
reference	B-General_Concept
counts	I-General_Concept
every	O
time	O
a	O
reference	O
is	O
created	O
or	O
destroyed	O
can	O
significantly	O
impede	O
performance	O
.	O
</s>
<s>
Not	O
only	O
do	O
the	O
operations	O
take	O
time	O
,	O
but	O
they	O
damage	O
cache	B-General_Concept
performance	O
and	O
can	O
lead	O
to	O
pipeline	B-General_Concept
bubbles	I-General_Concept
.	O
</s>
<s>
Even	O
read-only	O
operations	O
like	O
calculating	O
the	O
length	O
of	O
a	O
list	O
require	O
a	O
large	O
number	O
of	O
reads	O
and	O
writes	O
for	O
reference	O
updates	O
with	O
naive	O
reference	B-General_Concept
counting	I-General_Concept
.	O
</s>
<s>
The	O
Deutsch-Bobrow	O
method	O
of	O
reference	B-General_Concept
counting	I-General_Concept
capitalizes	O
on	O
the	O
fact	O
that	O
most	O
reference	B-General_Concept
count	I-General_Concept
updates	O
are	O
in	O
fact	O
generated	O
by	O
references	O
stored	O
in	O
local	O
variables	O
.	O
</s>
<s>
It	O
ignores	O
these	O
references	O
,	O
only	O
counting	O
references	O
in	O
data	O
structures	O
,	O
but	O
before	O
an	O
object	O
with	O
reference	B-General_Concept
count	I-General_Concept
zero	O
can	O
be	O
deleted	O
,	O
the	O
system	O
must	O
verify	O
with	O
a	O
scan	O
of	O
the	O
stack	O
and	O
registers	O
that	O
no	O
other	O
reference	O
to	O
it	O
still	O
exists	O
.	O
</s>
<s>
Another	O
technique	O
devised	O
by	O
Henry	O
Baker	O
involves	O
deferred	O
increments	O
,	O
in	O
which	O
references	O
which	O
are	O
stored	O
in	O
local	O
variables	O
do	O
not	O
immediately	O
increment	O
the	O
corresponding	O
reference	B-General_Concept
count	I-General_Concept
,	O
but	O
instead	O
defer	O
this	O
until	O
it	O
is	O
necessary	O
.	O
</s>
<s>
They	O
introduce	O
the	O
update	O
coalescing	O
method	O
which	O
coalesces	O
many	O
of	O
the	O
redundant	O
reference	B-General_Concept
count	I-General_Concept
updates	O
.	O
</s>
<s>
A	O
reference	B-General_Concept
counting	I-General_Concept
algorithm	O
would	O
typically	O
execute	O
rc(O1 )	O
--	O
,	O
rc(O2 )	O
++	O
,	O
rc(O2 )	O
--	O
,	O
rc(O3 )	O
++	O
,	O
rc(O3 )	O
--	O
,	O
...	O
,	O
rc(On )	O
++	O
.	O
</s>
<s>
In	O
order	O
to	O
have	O
the	O
reference	B-General_Concept
count	I-General_Concept
properly	O
evaluated	O
at	O
the	O
end	O
of	O
the	O
interval	O
it	O
is	O
enough	O
to	O
perform	O
rc(O1 )	O
--	O
and	O
rc(On )	O
++	O
.	O
</s>
<s>
Levanoni	O
and	O
Petrank	O
showed	O
in	O
2001	O
how	O
to	O
use	O
such	O
update	O
coalescing	O
in	O
a	O
reference	B-General_Concept
counting	I-General_Concept
collector	O
.	O
</s>
<s>
Interestingly	O
,	O
update	O
coalescing	O
also	O
eliminates	O
the	O
need	O
to	O
employ	O
atomic	B-General_Concept
operations	I-General_Concept
during	O
pointer	O
updates	O
in	O
a	O
concurrent	O
setting	O
,	O
this	O
solving	O
reference	B-General_Concept
counting	I-General_Concept
issues	O
in	O
a	O
concurrent	O
setting	O
.	O
</s>
<s>
Therefore	O
,	O
update	O
coalescing	O
solves	O
the	O
third	O
problem	O
of	O
naive	O
reference	B-General_Concept
counting	I-General_Concept
(	O
i.e.	O
,	O
a	O
costly	O
overhead	O
in	O
a	O
concurrent	O
setting	O
)	O
.	O
</s>
<s>
Blackburn	O
and	O
McKinley	O
's	O
ulterior	O
reference	B-General_Concept
counting	I-General_Concept
method	O
in	O
2003	O
combines	O
deferred	O
reference	B-General_Concept
counting	I-General_Concept
with	O
a	O
copying	O
nursery	O
,	O
observing	O
that	O
the	O
majority	O
of	O
pointer	O
mutations	O
occur	O
in	O
young	O
objects	O
.	O
</s>
<s>
This	O
algorithm	O
achieves	O
throughput	O
comparable	O
with	O
the	O
fastest	O
generational	O
copying	O
collectors	O
with	O
the	O
low	O
bounded	O
pause	O
times	O
of	O
reference	B-General_Concept
counting	I-General_Concept
.	O
</s>
<s>
A	O
system	O
may	O
explicitly	O
forbid	O
reference	O
cycles	O
;	O
file	O
systems	O
with	O
hard	B-Application
links	I-Application
often	O
do	O
this	O
.	O
</s>
<s>
Judicious	O
use	O
of	O
"	O
weak	O
"	O
(	O
non-counted	O
)	O
references	O
may	O
also	O
help	O
avoid	O
retain	O
cycles	O
;	O
the	O
Cocoa	B-Operating_System
framework	O
,	O
for	O
instance	O
,	O
recommends	O
using	O
"	O
strong	O
"	O
references	O
for	O
parent-to-child	O
relationships	O
and	O
"	O
weak	O
"	O
references	O
for	O
child-to-parent	O
relationships	O
.	O
</s>
<s>
This	O
technique	O
can	O
be	O
automated	O
by	O
creating	O
an	O
"	O
owner	O
"	O
object	O
that	O
does	O
the	O
tearing-down	O
when	O
it	O
is	O
destroyed	O
;	O
for	O
instance	O
,	O
a	O
Graph	B-Application
object	O
's	O
destructor	O
could	O
delete	O
the	O
edges	O
of	O
its	O
GraphNodes	O
,	O
breaking	O
the	O
reference	O
cycles	O
in	O
the	O
graph	B-Application
.	O
</s>
<s>
Computer	B-General_Concept
scientists	I-General_Concept
have	O
also	O
discovered	O
ways	O
to	O
detect	O
and	O
collect	O
reference	O
cycles	O
automatically	O
,	O
without	O
requiring	O
changes	O
in	O
the	O
data	O
structure	O
design	O
.	O
</s>
<s>
One	O
simple	O
solution	O
is	O
to	O
periodically	O
use	O
a	O
tracing	B-General_Concept
garbage	I-General_Concept
collector	I-General_Concept
to	O
reclaim	O
cycles	O
;	O
since	O
cycles	O
typically	O
constitute	O
a	O
relatively	O
small	O
amount	O
of	O
reclaimed	O
space	O
,	O
the	O
collector	O
can	O
be	O
run	O
much	O
less	O
often	O
than	O
with	O
an	O
ordinary	O
tracing	B-General_Concept
garbage	I-General_Concept
collector	I-General_Concept
.	O
</s>
<s>
Bacon	O
describes	O
a	O
cycle-collection	O
algorithm	O
for	O
reference	B-General_Concept
counting	I-General_Concept
with	O
similarities	O
to	O
tracing	O
collectors	O
,	O
including	O
the	O
same	O
theoretical	O
time	O
bounds	O
.	O
</s>
<s>
It	O
is	O
based	O
on	O
the	O
observation	O
that	O
a	O
cycle	O
can	O
only	O
be	O
isolated	O
when	O
a	O
reference	B-General_Concept
count	I-General_Concept
is	O
decremented	O
to	O
a	O
nonzero	O
value	O
.	O
</s>
<s>
It	O
knows	O
it	O
has	O
found	O
a	O
cycle	O
that	O
can	O
be	O
collected	O
when	O
decrementing	O
all	O
the	O
reference	B-General_Concept
counts	I-General_Concept
on	O
a	O
cycle	O
of	O
references	O
brings	O
them	O
all	O
down	O
to	O
zero	O
.	O
</s>
<s>
Although	O
it	O
is	O
possible	O
to	O
augment	O
simple	O
reference	B-General_Concept
counts	I-General_Concept
in	O
a	O
variety	O
of	O
ways	O
,	O
often	O
a	O
better	O
solution	O
can	O
be	O
found	O
by	O
performing	O
reference	B-General_Concept
counting	I-General_Concept
in	O
a	O
fundamentally	O
different	O
way	O
.	O
</s>
<s>
Here	O
we	O
describe	O
some	O
of	O
the	O
variants	O
on	O
reference	B-General_Concept
counting	I-General_Concept
and	O
their	O
benefits	O
and	O
drawbacks	O
.	O
</s>
<s>
In	O
weighted	O
reference	B-General_Concept
counting	I-General_Concept
,	O
each	O
reference	O
is	O
assigned	O
a	O
weight	O
,	O
and	O
each	O
object	O
tracks	O
not	O
the	O
number	O
of	O
references	O
referring	O
to	O
it	O
,	O
but	O
the	O
total	O
weight	O
of	O
the	O
references	O
referring	O
to	O
it	O
.	O
</s>
<s>
Since	O
the	O
total	O
weight	O
does	O
not	O
change	O
,	O
the	O
object	O
's	O
reference	B-General_Concept
count	I-General_Concept
does	O
not	O
need	O
to	O
be	O
updated	O
.	O
</s>
<s>
The	O
property	O
of	O
not	O
needing	O
to	O
access	O
a	O
reference	B-General_Concept
count	I-General_Concept
when	O
a	O
reference	O
is	O
copied	O
is	O
particularly	O
helpful	O
when	O
the	O
object	O
's	O
reference	B-General_Concept
count	I-General_Concept
is	O
expensive	O
to	O
access	O
,	O
for	O
example	O
because	O
it	O
is	O
in	O
another	O
process	O
,	O
on	O
disk	O
,	O
or	O
even	O
across	O
a	O
network	O
.	O
</s>
<s>
It	O
can	O
also	O
help	O
increase	O
concurrency	O
by	O
avoiding	O
many	O
threads	O
locking	O
a	O
reference	B-General_Concept
count	I-General_Concept
to	O
increase	O
it	O
.	O
</s>
<s>
Thus	O
,	O
weighted	O
reference	B-General_Concept
counting	I-General_Concept
is	O
most	O
useful	O
in	O
parallel	O
,	O
multiprocess	O
,	O
database	O
,	O
or	O
distributed	O
applications	O
.	O
</s>
<s>
The	O
primary	O
problem	O
with	O
simple	O
weighted	O
reference	B-General_Concept
counting	I-General_Concept
is	O
that	O
destroying	O
a	O
reference	O
still	O
requires	O
accessing	O
the	O
reference	B-General_Concept
count	I-General_Concept
,	O
and	O
if	O
many	O
references	O
are	O
destroyed	O
,	O
this	O
can	O
cause	O
the	O
same	O
bottlenecks	O
we	O
seek	O
to	O
avoid	O
.	O
</s>
<s>
Some	O
adaptations	O
of	O
weighted	O
reference	B-General_Concept
counting	I-General_Concept
seek	O
to	O
avoid	O
this	O
by	O
transferring	O
weight	O
from	O
a	O
dying	O
reference	O
to	O
an	O
active	O
reference	O
.	O
</s>
<s>
Weighted	O
reference	B-General_Concept
counting	I-General_Concept
was	O
independently	O
devised	O
by	O
Bevan	O
and	O
Watson	O
&	O
Watson	O
in	O
1987	O
.	O
</s>
<s>
In	O
indirect	O
reference	B-General_Concept
counting	I-General_Concept
,	O
it	O
is	O
necessary	O
to	O
keep	O
track	O
of	O
the	O
reference	O
's	O
source	O
.	O
</s>
<s>
This	O
means	O
that	O
two	O
references	O
are	O
kept	O
to	O
the	O
object	O
:	O
a	O
direct	O
one	O
which	O
is	O
used	O
for	O
invocations	O
;	O
and	O
an	O
indirect	O
one	O
which	O
forms	O
part	O
of	O
a	O
diffusion	O
tree	O
,	O
such	O
as	O
in	O
the	O
Dijkstra	B-Operating_System
–	I-Operating_System
Scholten	I-Operating_System
algorithm	I-Operating_System
,	O
which	O
allows	O
a	O
garbage	B-General_Concept
collector	I-General_Concept
to	O
identify	O
dead	O
objects	O
.	O
</s>
<s>
As	O
a	O
collection	O
algorithm	O
,	O
reference	B-General_Concept
counting	I-General_Concept
tracks	O
,	O
for	O
each	O
object	O
,	O
a	O
count	O
of	O
the	O
number	O
of	O
references	O
to	O
it	O
held	O
by	O
other	O
objects	O
.	O
</s>
<s>
If	O
an	O
object	O
's	O
reference	B-General_Concept
count	I-General_Concept
reaches	O
zero	O
,	O
the	O
object	O
has	O
become	O
inaccessible	O
,	O
and	O
can	O
be	O
destroyed	O
.	O
</s>
<s>
When	O
an	O
object	O
is	O
destroyed	O
,	O
any	O
objects	O
referenced	O
by	O
that	O
object	O
also	O
have	O
their	O
reference	B-General_Concept
counts	I-General_Concept
decreased	O
.	O
</s>
<s>
A	O
common	O
modification	O
allows	O
reference	B-General_Concept
counting	I-General_Concept
to	O
be	O
made	O
incremental	O
:	O
instead	O
of	O
destroying	O
an	O
object	O
as	O
soon	O
as	O
its	O
reference	B-General_Concept
count	I-General_Concept
becomes	O
zero	O
,	O
it	O
is	O
added	O
to	O
a	O
list	O
of	O
unreferenced	O
objects	O
,	O
and	O
periodically	O
(	O
or	O
as	O
needed	O
)	O
one	O
or	O
more	O
items	O
from	O
this	O
list	O
are	O
destroyed	O
.	O
</s>
<s>
Simple	O
reference	B-General_Concept
counts	I-General_Concept
require	O
frequent	O
updates	O
.	O
</s>
<s>
Whenever	O
a	O
reference	O
is	O
destroyed	O
or	O
overwritten	O
,	O
the	O
reference	B-General_Concept
count	I-General_Concept
of	O
the	O
object	O
it	O
references	O
is	O
decremented	O
,	O
and	O
whenever	O
one	O
is	O
created	O
or	O
copied	O
,	O
the	O
reference	B-General_Concept
count	I-General_Concept
of	O
the	O
object	O
it	O
references	O
is	O
incremented	O
.	O
</s>
<s>
Reference	B-General_Concept
counting	I-General_Concept
is	O
also	O
used	O
in	O
file	O
systems	O
and	O
distributed	O
systems	O
,	O
where	O
full	O
non-incremental	O
tracing	B-General_Concept
garbage	I-General_Concept
collection	I-General_Concept
is	O
too	O
time-consuming	O
because	O
of	O
the	O
size	O
of	O
the	O
object	O
graph	B-Application
and	O
slow	O
access	O
speed	O
.	O
</s>
<s>
Microsoft	O
's	O
Component	B-Application
Object	I-Application
Model	I-Application
(	O
COM	B-Application
)	O
and	O
WinRT	O
makes	O
pervasive	O
use	O
of	O
reference	B-General_Concept
counting	I-General_Concept
.	O
</s>
<s>
In	O
fact	O
,	O
two	O
of	O
the	O
three	O
methods	O
that	O
all	O
COM	B-Application
objects	I-Application
must	O
provide	O
(	O
in	O
the	O
IUnknown	B-Application
interface	O
)	O
increment	O
or	O
decrement	O
the	O
reference	B-General_Concept
count	I-General_Concept
.	O
</s>
<s>
Much	O
of	O
the	O
Windows	B-Application
Shell	I-Application
and	O
many	O
Windows	O
applications	O
(	O
including	O
MS	B-Application
Internet	I-Application
Explorer	I-Application
,	O
MS	B-Application
Office	I-Application
,	O
and	O
countless	O
third-party	O
products	O
)	O
are	O
built	O
on	O
COM	B-Application
,	O
demonstrating	O
the	O
viability	O
of	O
reference	B-General_Concept
counting	I-General_Concept
in	O
large-scale	O
systems	O
.	O
</s>
<s>
One	O
primary	O
motivation	O
for	O
reference	B-General_Concept
counting	I-General_Concept
in	O
COM	B-Application
is	O
to	O
enable	O
interoperability	O
across	O
different	O
programming	O
languages	O
and	O
runtime	B-Device
systems	I-Device
.	O
</s>
<s>
A	O
client	O
need	O
only	O
know	O
how	O
to	O
invoke	O
object	O
methods	O
in	O
order	O
to	O
manage	O
object	O
life	O
cycle	O
;	O
thus	O
,	O
the	O
client	O
is	O
completely	O
abstracted	O
from	O
whatever	O
memory	O
allocator	O
the	O
implementation	O
of	O
the	O
COM	B-Application
object	I-Application
uses	O
.	O
</s>
<s>
As	O
a	O
typical	O
example	O
,	O
a	O
Visual	B-Language
Basic	I-Language
program	O
using	O
a	O
COM	B-Application
object	I-Application
is	O
agnostic	O
towards	O
whether	O
that	O
object	O
was	O
allocated	O
(	O
and	O
must	O
later	O
be	O
deallocated	O
)	O
by	O
a	O
C++	O
allocator	O
or	O
another	O
Visual	B-Language
Basic	I-Language
component	O
.	O
</s>
<s>
However	O
,	O
by	O
the	O
same	O
token	O
,	O
C++	O
provides	O
native	O
ways	O
for	O
users	O
to	O
opt-into	O
such	O
functionality	O
:	O
C++11	B-Language
provides	O
reference	O
counted	O
smart	B-Language
pointers	I-Language
,	O
via	O
the	O
class	O
,	O
enabling	O
automatic	O
shared	O
memory-management	O
of	O
dynamically	O
allocated	O
objects	O
.	O
</s>
<s>
Programmers	O
can	O
use	O
this	O
in	O
conjunction	O
with	O
weak	B-General_Concept
pointers	I-General_Concept
(	O
via	O
)	O
to	O
break	O
cyclic	O
dependencies	O
.	O
</s>
<s>
In	O
addition	O
,	O
11	O
's	O
move	O
semantics	O
further	O
reduce	O
the	O
extent	O
to	O
which	O
reference	B-General_Concept
counts	I-General_Concept
need	O
to	O
be	O
modified	O
by	O
removing	O
the	O
deep	O
copy	O
normally	O
used	O
when	O
a	O
function	O
returns	O
an	O
object	O
,	O
as	O
it	O
allows	O
for	O
a	O
simple	O
copy	O
of	O
the	O
pointer	O
of	O
said	O
object	O
.	O
</s>
<s>
Apple	O
's	O
Cocoa	B-Operating_System
and	O
Cocoa	B-Operating_System
Touch	I-Operating_System
frameworks	O
(	O
and	O
related	O
frameworks	O
,	O
such	O
as	O
Core	B-Operating_System
Foundation	I-Operating_System
)	O
use	O
manual	O
reference	B-General_Concept
counting	I-General_Concept
,	O
much	O
like	O
COM	B-Application
.	O
</s>
<s>
Traditionally	O
this	O
was	O
accomplished	O
by	O
the	O
programmer	O
manually	O
sending	O
retain	O
and	O
release	O
messages	O
to	O
objects	O
,	O
but	O
Automatic	B-Language
Reference	I-Language
Counting	I-Language
,	O
a	O
Clang	B-Application
compiler	O
feature	O
that	O
automatically	O
inserts	O
these	O
messages	O
as	O
needed	O
,	O
was	O
added	O
in	O
iOS	B-Application
5	O
and	O
Mac	B-Application
OS	I-Application
X	I-Application
10.7	I-Application
.	O
</s>
<s>
Mac	B-Application
OS	I-Application
X	I-Application
10.5	I-Application
introduced	O
a	O
tracing	B-General_Concept
garbage	I-General_Concept
collector	I-General_Concept
as	O
an	O
alternative	O
to	O
reference	B-General_Concept
counting	I-General_Concept
,	O
but	O
it	O
was	O
deprecated	O
in	O
OS	B-Operating_System
X	I-Operating_System
10.8	I-Operating_System
and	O
removed	O
from	O
the	O
Objective-C	O
runtime	B-Library
library	I-Library
in	O
macOS	B-Operating_System
Sierra	I-Operating_System
.	O
</s>
<s>
iOS	B-Application
has	O
never	O
supported	O
a	O
tracing	B-General_Concept
garbage	I-General_Concept
collector	I-General_Concept
.	O
</s>
<s>
Delphi	B-Language
is	O
mostly	O
not	O
a	O
garbage	B-General_Concept
collected	I-General_Concept
language	O
,	O
in	O
that	O
user-defined	O
types	O
must	O
still	O
be	O
manually	O
allocated	O
and	O
deallocated	O
,	O
however	O
it	O
does	O
provide	O
automatic	O
collection	O
using	O
reference	B-General_Concept
counting	I-General_Concept
for	O
a	O
few	O
built-in	O
types	O
,	O
such	O
as	O
strings	O
,	O
dynamic	B-Data_Structure
arrays	I-Data_Structure
,	O
and	O
interfaces	O
,	O
for	O
ease	O
of	O
use	O
and	O
to	O
simplify	O
the	O
generic	O
database	O
functionality	O
.	O
</s>
<s>
It	O
is	O
up	O
to	O
the	O
programmer	O
to	O
decide	O
whether	O
to	O
use	O
the	O
built-in	O
types	O
;	O
Delphi	B-Language
programmers	O
have	O
complete	O
access	O
to	O
low-level	O
memory	O
management	O
like	O
in	O
C/C	O
++	O
.	O
</s>
<s>
So	O
all	O
potential	O
cost	O
of	O
Delphi	B-Language
's	O
reference	B-General_Concept
counting	I-General_Concept
can	O
,	O
if	O
desired	O
,	O
be	O
easily	O
circumvented	O
.	O
</s>
<s>
Some	O
of	O
the	O
reasons	O
reference	B-General_Concept
counting	I-General_Concept
may	O
have	O
been	O
preferred	O
to	O
other	O
forms	O
of	O
garbage	B-General_Concept
collection	I-General_Concept
in	O
Delphi	B-Language
include	O
:	O
</s>
<s>
The	O
general	O
benefits	O
of	O
reference	B-General_Concept
counting	I-General_Concept
,	O
such	O
as	O
prompt	O
collection	O
.	O
</s>
<s>
The	O
overhead	O
in	O
code	O
size	O
required	O
for	O
reference	B-General_Concept
counting	I-General_Concept
is	O
very	O
small	O
(	O
on	O
native	O
x86	O
,	O
typically	O
a	O
single	O
LOCK	O
INC	O
,	O
LOCK	O
DEC	O
or	O
LOCK	O
XADD	O
instruction	O
,	O
which	O
ensures	O
atomicity	B-General_Concept
in	O
any	O
environment	O
)	O
,	O
and	O
no	O
separate	O
thread	O
of	O
control	O
is	O
needed	O
for	O
collection	O
as	O
would	O
be	O
needed	O
for	O
a	O
tracing	B-General_Concept
garbage	I-General_Concept
collector	I-General_Concept
.	O
</s>
<s>
The	O
reference	B-General_Concept
count	I-General_Concept
of	O
a	O
string	O
is	O
checked	O
before	O
mutating	O
a	O
string	O
.	O
</s>
<s>
This	O
allows	O
reference	B-General_Concept
count	I-General_Concept
1	O
strings	O
to	O
be	O
mutated	O
directly	O
whilst	O
higher	O
reference	B-General_Concept
count	I-General_Concept
strings	O
are	O
copied	O
before	O
mutation	O
.	O
</s>
<s>
Because	O
garbage-collection	O
is	O
only	O
done	O
on	O
built-in	O
types	O
,	O
reference	B-General_Concept
counting	I-General_Concept
can	O
be	O
efficiently	O
integrated	O
into	O
the	O
library	O
routines	O
used	O
to	O
manipulate	O
each	O
datatype	O
,	O
keeping	O
the	O
overhead	O
needed	O
for	O
updating	O
of	O
reference	B-General_Concept
counts	I-General_Concept
low	O
.	O
</s>
<s>
Moreover	O
,	O
a	O
lot	O
of	O
the	O
runtime	B-Library
library	I-Library
is	O
in	O
hand-optimized	O
assembler	O
.	O
</s>
<s>
This	O
is	O
important	O
since	O
both	O
Delphi	B-Language
and	O
FPC	O
implement	O
their	O
RTL	O
in	O
Pascal	O
.	O
</s>
<s>
The	O
GObject	B-Language
object-oriented	O
programming	O
framework	O
implements	O
reference	B-General_Concept
counting	I-General_Concept
on	O
its	O
base	O
types	O
,	O
including	O
weak	B-General_Concept
references	I-General_Concept
.	O
</s>
<s>
Reference	O
incrementing	O
and	O
decrementing	O
uses	O
atomic	B-General_Concept
operations	I-General_Concept
for	O
thread	O
safety	O
.	O
</s>
<s>
A	O
significant	O
amount	O
of	O
the	O
work	O
in	O
writing	O
bindings	O
to	O
GObject	B-Language
from	O
high-level	O
languages	O
lies	O
in	O
adapting	O
GObject	B-Language
reference	B-General_Concept
counting	I-General_Concept
to	O
work	O
with	O
the	O
language	O
's	O
own	O
memory	O
management	O
system	O
.	O
</s>
<s>
The	O
Vala	B-Language
programming	I-Language
language	I-Language
uses	O
GObject	B-Language
reference	B-General_Concept
counting	I-General_Concept
as	O
its	O
primary	O
garbage	B-General_Concept
collection	I-General_Concept
system	O
,	O
along	O
with	O
copy-heavy	O
string	O
handling	O
.	O
</s>
<s>
Perl	B-Language
also	O
uses	O
reference	B-General_Concept
counting	I-General_Concept
,	O
without	O
any	O
special	O
handling	O
of	O
circular	O
references	O
,	O
although	O
(	O
as	O
in	O
Cocoa	B-Operating_System
and	O
C++	O
above	O
)	O
,	O
Perl	B-Language
does	O
support	O
weak	B-General_Concept
references	I-General_Concept
,	O
which	O
allows	O
programmers	O
to	O
avoid	O
creating	O
a	O
cycle	O
.	O
</s>
<s>
PHP	B-Application
uses	O
a	O
reference	B-General_Concept
counting	I-General_Concept
mechanism	O
for	O
its	O
internal	O
variable	O
management	O
.	O
</s>
<s>
Since	O
PHP	B-Application
5.3	O
,	O
it	O
implements	O
the	O
algorithm	O
from	O
Bacon	O
's	O
above	O
mentioned	O
paper	O
.	O
</s>
<s>
PHP	B-Application
allows	O
you	O
to	O
turn	O
on	O
and	O
off	O
the	O
cycle	O
collection	O
with	O
user-level	O
functions	O
.	O
</s>
<s>
Python	B-Language
also	O
uses	O
reference	B-General_Concept
counting	I-General_Concept
and	O
offers	O
cycle	O
detection	O
as	O
well	O
(	O
and	O
can	O
reclaim	O
them	O
)	O
.	O
</s>
<s>
Rust	B-Application
uses	O
declared	O
lifetimes	O
in	O
the	O
code	O
to	O
free	O
memory	O
.	O
</s>
<s>
Rust	B-Application
has	O
a	O
Rc	O
and	O
Arc	O
struct	B-Language
.	O
</s>
<s>
Squirrel	B-Language
uses	O
reference	B-General_Concept
counting	I-General_Concept
with	O
cycle	O
detection	O
.	O
</s>
<s>
This	O
tiny	O
language	O
is	O
relatively	O
unknown	O
outside	O
the	O
video	O
game	O
industry	O
;	O
however	O
,	O
it	O
is	O
a	O
concrete	O
example	O
of	O
how	O
reference	B-General_Concept
counting	I-General_Concept
can	O
be	O
practical	O
and	O
efficient	O
(	O
especially	O
in	O
realtime	O
environments	O
)	O
.	O
</s>
<s>
Tcl	B-Operating_System
8	O
uses	O
reference	B-General_Concept
counting	I-General_Concept
for	O
memory	O
management	O
of	O
values	O
(	O
Tcl	B-Operating_System
Obj	O
structs	B-Language
)	O
.	O
</s>
<s>
Since	O
Tcl	B-Operating_System
's	O
values	O
are	O
immutable	B-Application
,	O
reference	O
cycles	O
are	O
impossible	O
to	O
form	O
and	O
no	O
cycle	O
detection	O
scheme	O
is	O
needed	O
.	O
</s>
<s>
Operations	O
that	O
would	O
replace	O
a	O
value	O
with	O
a	O
modified	O
copy	O
are	O
generally	O
optimized	O
to	O
instead	O
modify	O
the	O
original	O
when	O
its	O
reference	B-General_Concept
count	I-General_Concept
indicates	O
that	O
it	O
is	O
not	O
shared	O
.	O
</s>
<s>
Xojo	B-Application
also	O
uses	O
reference	B-General_Concept
counting	I-General_Concept
,	O
without	O
any	O
special	O
handling	O
of	O
circular	O
references	O
,	O
although	O
(	O
as	O
in	O
Cocoa	B-Operating_System
and	O
C++	O
above	O
)	O
,	O
Xojo	B-Application
does	O
support	O
weak	B-General_Concept
references	I-General_Concept
,	O
which	O
allows	O
programmers	O
to	O
avoid	O
creating	O
a	O
cycle	O
.	O
</s>
<s>
Many	O
file	O
systems	O
maintain	O
reference	B-General_Concept
counts	I-General_Concept
to	O
any	O
particular	O
block	O
or	O
file	O
,	O
for	O
example	O
the	O
inode	B-Application
link	O
count	O
on	O
Unix-style	B-Application
file	I-Application
systems	I-Application
,	O
which	O
are	O
usually	O
known	O
as	O
hard	B-Application
links	I-Application
.	O
</s>
<s>
While	O
references	O
can	O
still	O
be	O
made	O
from	O
directories	B-Application
,	O
some	O
Unixes	O
only	O
allow	O
references	O
from	O
live	O
processes	O
,	O
and	O
there	O
can	O
be	O
files	O
that	O
exist	O
outside	O
the	O
file	O
system	O
hierarchy	O
.	O
</s>
