⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page416.html

📁 Data Structures And Algorithms With Object-Oriented Design Patterns In Python (2003) source code and
💻 HTML
字号:
<HTML><HEAD><TITLE>What is Garbage?</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="tex2html5971" HREF="page417.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5969" HREF="page415.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5963" HREF="page415.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5973" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H1><A NAME="SECTION0013100000000000000000">What is Garbage?</A></H1><P>While Python provides the means to create an object,the language does not provide the meansto destroy an object <em>explicitly</em><A NAME="tex2html752" HREF="footnode.html#29828"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/foot_motif.gif"></A>.As long as a program contains a reference to some object instance,the Python virtual machine is required to ensure that the object exists.If the Python language provided the means to destroy objects,it would be possible for a program to destroy an objecteven when a reference to that object still existed.This situation is unsafe because the program could attempt laterto invoke a method on the destroyed object,leading to unpredictable results.<P>The situation which arises when a program contains a reference (or pointer)to a destroyed object is called a<em>dangling reference</em><A NAME=29800>&#160;</A>(or dangling pointer<A NAME=29801>&#160;</A>).By disallowing the explicit destruction of objects,Python eliminates the problem of dangling references.<P>Languages that support the explicit destruction of objectstypically require the program to keep track of all the objects it createsand to destroy them explicitly when they are not longer needed.If a program somehow loses track of an object it has createdthen that object cannot be destroyed.And if the object is never destroyed,the memory occupied by that objectcannot be used again by the program.<P>A program that loses track of objects before it destroys themsuffers from a <em>memory leak</em><A NAME=29803>&#160;</A>.If we run a program that has a memory leak for a very long time,it is quite possible that it will exhaust all the available memoryand eventually fail because no new objects can be created.<P>It would seem that by disallowing the explicit destruction of objects,a Python program is doomed to eventual failure due to memory exhaustion.Indeed this would be the case,were it not for the fact that the Python language specification requiresthe Python virtual machine to be able to find unreferenced objectsand to reclaim the memory locations allocated to those objects.An unreferenced object is called <em>garbage</em><A NAME=29805>&#160;</A>and the process of finding all the unreferenced objectsand reclaiming the storage is called<em>garbage collection</em><A NAME=29807>&#160;</A>.<P>Just as the Python language does not specify precisely how objectsare to be represented in the memory of a Python virtual machine,the language specification also does not stipulate how the garbage collectionis to be implemented or when it should be done.Garbage collection is usually invoked when the total amountof memory allocated to a Python program exceeds some threshold.Typically, the program is suspended while the garbage collection is done.<P>In the analyses presented in the preceding chapters weassume that the running time to create a new objectis a fixed constant,  <IMG WIDTH=29 HEIGHT=14 ALIGN=MIDDLE ALT="tex2html_wrap_inline58033" SRC="img126.gif"  >,and we completely ignore the garbage collection overhead.In reality, neither assumption is valid.Even if sufficient memory is available,the time required by the Python virtual machine to locatean unused region of memory depends very much on the data structuresused to keep track of the memory regions allocated to a programas well as on the way in which a program uses the objects it creates.Furthermore, creating a new object may trigger thegarbage collection process.The running time for garbage collection can be a significant fractionof the total running time of a program.<P><BR> <HR><UL> <LI> <A NAME="tex2html5974" HREF="page417.html#SECTION0013110000000000000000">Reduce, Reuse, Recycle</A><LI> <A NAME="tex2html5975" HREF="page421.html#SECTION0013120000000000000000">Helping the Garbage Collector</A></UL><HR><A NAME="tex2html5971" HREF="page417.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html5969" HREF="page415.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html5963" HREF="page415.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html5973" 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 &#169; 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 + -