📄 page424.html
字号:
<HTML><HEAD><TITLE>Why Reference Counting Does Not Work</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6060" HREF="page425.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6058" HREF="page422.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6054" HREF="page423.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6062" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0013220000000000000000">Why Reference Counting Does Not Work</A></H2><P>So far, reference counting looks like a good idea.However, the reference counting does not always work.Consider a circular, singly-linked listsuch as the one shown in Figure <A HREF="page424.html#figgarbage4"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> (a).In the figure, the variable <tt>head</tt> refers to the head of the linked listand the last element of the linked list also refers to the head.Therefore, the reference count on the first list element is two;whereas, the remaining list elements each has a reference count of one.<P><P><A NAME="30678"> </A><A NAME="figgarbage4"> </A> <IMG WIDTH=575 HEIGHT=220 ALIGN=BOTTOM ALT="figure30386" SRC="img1694.gif" ><BR><STRONG>Figure:</STRONG> Why reference counting fails.<BR><P><P>Consider what happens when we assign the value <tt>None</tt> to the<tt>head</tt> variable.This results in the situation shown in Figure <A HREF="page424.html#figgarbage4"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> (b).The reference count on the first list element has been decreased by onebecause the <tt>head</tt> variable no longer refers to it.However, its reference count is not zero,because the tail of the list still refers to the head.<P>We now have a problem.The reference counts on all the lists elements are non-zero.Therefore, they are not considered to be garbage by a reference countinggarbage collector.On the other hand,no external references to the linked-list elements remain.Therefore, the list elements are indeed garbage.<P>This example illustrates the Achilles' heel of reference counting--circular data structures.In general, reference counting will fail to work wheneverthe data structure contains a cycle of references.Python does not prevent the creation of cyclic structures.Therefore, reference counting by itself is not a suitablegarbage collection scheme for arbitrary objects.Nevertheless, it is an extremely useful technique for dealingwith simple objects that don't refer to other objects,such as <tt>int</tt>s and <tt>floats</tt>s.<P><HR><A NAME="tex2html6060" HREF="page425.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6058" HREF="page422.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6054" HREF="page423.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html6062" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -